如何编写MFC Com(non-ATL)的Client程序?

a_gan 2000-06-19 09:33:00
如何获得COM中Server的界面和CLSID,IID,通过头文件吗?
CLSID clsid;
LPCLASSFACTORY pClf;
LPUNKNOWN pUnk;
INavDbProject* pProject;

HRESULT hr;
if((hr=::CLSIDFromProgID(L"Spatial",&clsid))!=NOERROR)
{
TRACE("Unable to find Program ID---error = %x\n",hr);
return;
}

if((hr=::CoGetClassObject(clsid,CLSCTX_INPROC_SERVER,NULL,IID_IClassFactory,(void**)&pClf))!=NOERROR)
{
TRACE("Unable to find CLSID---error = %x\n",hr);
return;
}
pClf->CreateInstance(NULL,IID_IUnknown,(void**)&pUnk);
pUnk->QueryInterface(IID_INavDbProject,(void**)&pProject);

pClf->Release();
pUnk->Release();
pProject->Release();

编译时出错:
E:\Study\MFC\Navigator\NavigatorView.cpp(112) : error C2065: 'INavDbProject' : undeclared identifier
E:\Study\MFC\Navigator\NavigatorView.cpp(112) : error C2065: 'pProject' : undeclared identifier
E:\Study\MFC\Navigator\NavigatorView.cpp(112) : warning C4552: '*' : operator has no effect; expected operator with side-effect
E:\Study\MFC\Navigator\NavigatorView.cpp(127) : error C2065: 'IID_INavDbProject' : undeclared identifier
E:\Study\MFC\Navigator\NavigatorView.cpp(135) : error C2227: left of '->Release' must point to class/struct/union

该如何处理??????
...全文
198 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
blackjack 2000-09-07
  • 打赏
  • 举报
回复

举个例子吧,Com对象的.IDL如下:

import "oaidl.idl";
import "ocidl.idl";
[
object,
uuid(0B463456-8499-11D4-95B2-5254ABDD134B),
dual,
helpstring("Inc Interface"),
pointer_default(unique)
]
interface Inc : IDispatch
{
[id(1), helpstring("method DoShow")] HRESULT DoShow([in]BSTR bstrMessage);
[propget, id(2), helpstring("property IntValue")] HRESULT IntValue([out, retval] long *pVal);
[propput, id(2), helpstring("property IntValue")] HRESULT IntValue([in] long newVal);
};

[
uuid(0B463441-8499-11D4-95B2-5254ABDD134B),
version(1.0),
helpstring("cp 1.0 Type Library")
]
library CPLib
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");

[
uuid(0B463457-8499-11D4-95B2-5254ABDD134B),
helpstring("nc Class")
]
coclass nc
{
[default] interface Inc;
};
};

以上.IDL仅功参考,com调用时不一定要,对象调用如下:

#include <windows.h>
#include <comdef.h>

#import "D:\vc++\cp\Debug\cp.dll"//对象的DLL的路径

int main(){
CoInitialize(NULL);
try {
CPLib::IncPtr/*接口指针*/ pInc(__uuidof(CPLib::nc/*类工厂名*/));

_bstr_t bstrMessage=L"Hello";

pInc->put_IntValue(007L);
pInc->DoShow(bstrMessage);
}
catch(const _com_error &e){
MessageBox(0,
(LPCTSTR)e.ErrorMessage(),
(LPCTSTR)"Com error",
MB_OK);
}

CoUninitialize();

return 0;
}
mytulip 2000-08-28
  • 打赏
  • 举报
回复
tulip
Tender 2000-07-22
  • 打赏
  • 举报
回复
查看你的COM Server的类型库,获得你要的接口ID值{DWORD,WORD,WORD,{BYTE*8}},然后定义一个IID常量=你得到的接口ID.
其他方法:#import "*.dll" //你的com dll路径,此时产生.tlh和.tli两个文件,然后利用智能指针(smart pointer),可以像在VB中一样简单调用接口的方法
endlessing 2000-07-17
  • 打赏
  • 举报
回复
i have a similiar problem and following is my idea:
CLSIDFromProgID(...) ----- get component class id
CoCreateInstance( ... ) ----- get relative component dispatch interface
pDispatch->GetIDsofNames( ... ) --- get id of some method
... construct parameters and others
pDispatch->Invoke( id of method,...,parameter,...) ----calling relative method & return necessary result.

I'm trying, but i don't know if it works.
you can't call a method directly here, and an id has to be accessed first, then you can call the method by the id. but you can get the instance by calling createobject("ProgID"), so there must be a similiar answer for it. but createobject in VC returns a IUnknown pointer, since you can't access the declare & implement for your COM component, thus you can't cast your component from the IUnknown pointer. so what can i do for it?

3,245

社区成员

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

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