十万火急:哪位高手知道如何在VC中调用VB编写的com组件,有相关内容吗?

yanjingshui 2003-09-24 03:13:29
用#import vehicleonline.DLL,为何工程编译时不能识别智能指针,用vb com组件访问数据库,有什么具体例子?
...全文
47 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
pingfan2003 2003-09-26
  • 打赏
  • 举报
回复
http://www.codeproject.net 上有一篇文章
“Handling VB ActiveX Events in Visual C++ client”

lookmai 2003-09-25
  • 打赏
  • 举报
回复
我不知贴了多少次了....
VC肯定能调用VB的com,用VB写的com已经包含有自动化接口,方便各种脚本语言的调用.
VC调用VB写的com用多种办法.我只介绍其中两种(其实也是com通用的调用方法,一样可以调用ATL写的).
首先Create the Visual Basic Server.

Create a "testcom1" ActiveX DLL project. create a "testget" class
Add the following code to "testget":

Public Function loadPO(ByVal one As String, ByVal two As String) As String
loadPO = one & " and " & two
End Function

Compile the DLL as d:\testcom1.dll and exit Visual Basic.


一、前绑定,相当VB的Dim obj As New testcom1.testget
1.Visual C++6.0:File-->new-->Projects-->Win32 Application(project name)-->testcom3,就选第一项"An empty project"......
2.File-->new-->C++ Source File-->testcom3.输入以下内容到testcom3.cpp
//
#include "windows.h"
#include "testcom3.h"
#include <comutil.h>
#pragma comment(lib, "comsupp.lib")

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
CoInitialize(NULL);
_testget *pClass = NULL;
HRESULT hr=CoCreateInstance(CLSID_testget, NULL, CLSCTX_INPROC_SERVER, IID__testget, (void**)&pClass);
BSTR One=SysAllocString(L"first");
BSTR Two=SysAllocString(L"second");
BSTR Three;
hr = pClass->loadPO(One,Two,&Three);
char *p = _com_util::ConvertBSTRToString(Three);
MessageBox(NULL,p,"Access Check",MB_OK | MB_ICONINFORMATION);
SysFreeString(One);
SysFreeString(Two);
SysFreeString(Three);
pClass->Release();
CoUninitialize();
return 0;
}
//
3.Click OLE/COM Object Viewer on the Tools menu. Select View Typelib from the File menu and choose the testcom1.dll you created earlier. Click Open to display the ITypeLib Viewer, which contains the .idl file for your DLL.
4.Copy the contents of your .idl file (contents of the right pane) to the Clipboard. Hold the SHIFT key down while paging or scrolling from the first character to the end of the text in the pane. Press Ctrl+C to copy the marked text to the Clipboard.
5.Click New on Visual C++ File menu. Select Text File on the New dialog box, name the file testcom3.idl, and click OK.
6.A blank text file appears. Paste the data from the Clipboard into it and save the file.
7.Select Settings from the Project menu, expand the testcom3 and Source Files nodes of the tree view, and select testcom3.idl. Click the MIDL tab, enter testcom3.h in the "Output header file name" box, and click OK.
8.打开testcom3.idl,执行Build-->Compile testcom3.idl,在当前目录下生在testcom3.h和testcom3_i.c两个文件.
9.将testcom3_i.c加到Source Files,testcom3.h加入Header Files.
10.run.

二、后绑定,相当VB的set obj=server.createObject("testcom1.testget").采用VB的自动化接口.
1.Visual C++6.0:File-->new-->Projects-->Win32 Application(project name)-->testcom1,就选第一项"An empty project"......
2.File-->new-->C++ Source File-->testcom1.输入以下内容到testcom1.cpp
//
#include "windows.h"
#include <comutil.h>
#pragma comment(lib, "comsupp.lib")

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
CoInitialize(NULL);
CLSID clsid;
CLSIDFromProgID(L"testcom1.testget", &clsid);
IDispatch* pIDispatch=NULL;
CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IDispatch, (void**)&pIDispatch);
DISPID dispid;
OLECHAR FAR* szMemberName = L"loadPO";
pIDispatch->GetIDsOfNames(IID_NULL,
&szMemberName,
1,
LOCALE_SYSTEM_DEFAULT,
&dispid);
DISPPARAMS dispparams;
dispparams.cArgs=2; //two arguments
dispparams.cNamedArgs=0; //no named arguments
dispparams.rgdispidNamedArgs=NULL;
dispparams.rgvarg=new VARIANTARG[2]; //the arg array contains two variants
//REMEMBER: arguments in the rgvarg array are reversed in order,
//so the last method argument is rgvarg[0] !
dispparams.rgvarg[0].vt=VT_BSTR; //a BSTR
dispparams.rgvarg[0].bstrVal=SysAllocString(L"second"); //allocate the BSTR
dispparams.rgvarg[1].vt=VT_BSTR; //a BSTR
dispparams.rgvarg[1].bstrVal=SysAllocString(L"first"); //allocate the BSTR
VARIANTARG Result;
Result.vt= VT_BSTR;
//Result.
EXCEPINFO excep;
//argument error information
UINT uArgErr;
pIDispatch->Invoke(dispid,
IID_NULL,
LOCALE_SYSTEM_DEFAULT,
DISPATCH_METHOD,
&dispparams,
&Result,
&excep,
&uArgErr);
//if(Result.bstrVal=="hello")
char *p = _com_util::ConvertBSTRToString(Result.bstrVal);
MessageBox(NULL,p,"Access Check",MB_OK | MB_ICONINFORMATION);
SysFreeString(dispparams.rgvarg[0].bstrVal);
//delete the argument array
delete[] dispparams.rgvarg;
CoUninitialize();
return 0;
}
//
1方法效率高,是主流的使用方法,方法2的Invoke接口比较耗时.

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧