如何编写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

该如何处理??????
...全文
199 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?
1,ATLAux.zipA set of VC++ helpers and patterns to help automate some routine coding tasks.(35KB)2,CltnWizard.zipATL object wizard that creates a collection of other COM objects(37KB)3,gridctrlatl.zipA grid control for displaying tabular data, based on Chris Maunder's grid control(101KB)4,TempMonitor.zipA step by step tutorial on using ATL controls in MFC(47KB)5,SAWzip.zipAn ATL based control for reading and writing zip files(187KB)6,Property12.zipDescribes an ActiveX Control to use OLE Automation to set an object's properties.(222KB)7,AlxGrd.zipDescribes an ActiveX Control to use OLE Automation to set an object's properties.(215KB)8,ATLLog_demo.zipA simple logging utility to help debug your ATL applications(48KB)9,ATL_pp_wizard.zipA wizard that allows you to create an ATL Object Wizard Property Page(74KB)10,AtlSplitter.zipATL Splitter ActiveX control(94KB)11,Com_Atl.zipBeginner's Tutorial: COM/ATL Simple Project(19KB)12,ienum.zipA simplified method to enumerate a collection of objects.(137KB)13,ConExe_demo.zipAn article on ATL COM event connection point threading issues(72KB)14,RBDeskBand.zipAn ATL Object Wizard that creates an DeskBand COM Object implementation(149KB)15,instmsi.zipThis is an ATL Object Wizard that eases window development using CWindowImpl. It removes the redundant copy-n-paste of code between class implementations(1437KB)16,DrawingD.zipDrawing complex ATL/ActiveX controls at designtime(38KB)17,ATLDateLib.zipNon-MFC Date Routines in an ATL Component.(10KB)18,shellext.zipA wizard that allows you to create an ATL Property Page or Context Menu extensions(77KB)19,StructStg.zipA wrapper class for most common IStorage methods and API calls.(20KB)20,PropBrowser.zipAllows you to easily create property pages for ActiveX controls(59KB)21,Redirect.zipAn ATL-control for redirecting stdout/stdin(55KB)22,kPad.zipAn example of using the WTL library and RichEdit control(131KB)23,rsPrevFontcmb.zipA font combo box that previews the fonts in the dropdown view(30KB)24,ButtonMenu.zipUse WTL to create simple button that implements drop-down menu(19KB)25,DocViewWTL.zipA library that provides the easiest way to get loosely coupled components.(163KB)26,update_wtl.zipNotes on updating your WTL installation(2KB)27,sidebarmenu.zipAn article about changing the look of WTL icon menu(85KB)28,customdraw.zipHow to use WTL to create custom controls(20KB)29,WTLDocs1.zipBeginning Documentation on WTL(71KB)30,Toolbar.zipAn article on extending the tool bar control using the Window Template Library(64KB)31,propsheetddx.zipDemonstrates how to use DDX/DDV and tabbed property sheets with WTL(35KB)32,donut_demo.zipMDI and Tab WebBrowser(168KB)33,JCB.zipA Java Class Browser written in C++ using WTL(139KB)34,MenuBtn.zipUse of a Push button with a drop down menu(21KB)35,AutoATL.zipUsing ATL to Automate a MFC Application Or, Creating Hybrid MFC ATL COM Objects(41KB)36,atlcontain.zipUsing Containment In ATL - A Complete Application(202KB)37,atnt_src.zipSplitter window control(454KB)38,msform.exeActiveX script hosting - 2(123KB)39,axhost.exeActiveX script hosting(109KB)40,ATL_Script.zipImplementing Active Script Site with ATL(52KB)41,actvdbg.zipAdding Debug facilities to an Active Scripting Host(86KB)42,Gif89a.zipA Control for Displaying Animated GIF Images(221KB)43,proppagec.zipActiveX Control Property Page Container(40KB)44,qlistctrl.zipQListCtrl - a popup list box like that in Visual C++ 6.0(36KB)45,ncombo.zipAn Enhanced Combo Box Control.(194KB)46,XFloorWnd.zipFloor Window Control(341KB)47,propgrid.zipProperties Grid Control(239KB)48,dscdao.zipData Source ActiveX control for DAO(201KB)49,rnso.zipRNSO - A Remote Notifiers,Subjects and Observers Server over DCOM(197KB)50,multiarray.zipUsing a Multidimensional SAFEARRAY to pass data across from COM objects (50KB)51,safearray.zipPassing DYNAMIC Data QUICKLY using SafeArrays - 2(71KB)52,comcollec.zipCOM Collection Template Class(30KB)53,AXDocc.zipProgramming Active Document Containers(114KB)54,MBV_Demo.zipMBV Wizard and the COM Container Library(105KB55,WaveEdAt.zipA COM-based Automation Wave File Editor(124KB)56,bankaccount1.zipHandling Interdependent Objects in Automation An Example of a Bank Server Object(53KB)57,aspfile.zipAn ASP Component to Send Arbitary Large File from Server to Client(62KB)58,tlbrowser.zipWriting a simple Typelibrary browser(45KB)59,tiatut.zipBuilding a World Class Application with MFC and COM Automation(197KB)60,asyncpp.zipAsynchronous Pluggable Protocol(11KB)61,comarray.zipPassing arrays of structures in COM.(24KB)62,olefile.zipCOleFileManager - encapsulates the ugly details of implementing compound file support(4KB)63,VBCollecs.zipSimple STL Collections in ATL(14KB)64,hyperlinks.zipActiveX hyper linking(389KB)65,activex.zipActiveX Control Tutorial(89KB)66,atlexamp.zipLeveraging C++ Applications Using COM An Example Using ATL COM(52KB)67,docviewcom.zipLeveraging C++ Applications Using COM An Extension of the Document View Architecture(86KB)68,atlclienttut.zipATL Client Application Tutorial(51KB)69,wordauto.zipAutomation And Eventing With Microsoft Word(32KB)
1,pop3.zipCPop3Connection - an MFC Class to encapsulate the POP3 protocol(15KB)2,ipenum.zipIPEnum - an MFC class and console app to allow IP address enumeration(11KB)3,smtp.zipCSMTPConnection - an MFC Class to encapsulate the SMTP protocol(21KB)4,ping.zipCPing - an MFC class to encapsulate the PING protocol(13KB)5,mailslot.zipCServerMailslot & CClientMailslot - 2 MFC classes to support Win32 mailslots(29KB)6,rasmonitor.zipCRasMonitor - An MFC class to monitor your Dial-Up Networking Connections(12KB)7,npipe.zipCNamedPipe - An MFC class to encapsulate named pipes(24KB)8,apipe.zipCAnonymousPipe - An MFC class to encapsulate anonymous pipes(8KB)9,csntp.zipCSNTPClient - An MFC class to encapsulate the SNTP protocol(17KB)10,tracer.zipCTraceRoute - an MFC class to implement Trace Route functionality(13KB)11,popwatch.zipPopWatch - An application to monitor your POP3 mailbox(17KB)12,server.zipAn article about using I/O Completion Ports and Winsock to write robust and scalable Windows server applications(14KB)13,rasmonitor_src.zipA shareware application to monitor your Dial-Up Networking Connections(17KB)14,afdialer.zipA dial-up dialer for connection to Internet providers (337KB)15,dialup_demo.zipHow to open a new dialup connection using RasDial(10KB)16,webgrab.zipA simple class to ease the task of downloading files from the net(25KB)17,multicastsocket.zipA Multicast Wrapper Class(22KB)18,lantalk.zipA program that allows users across a homogeneous network to communicate with each other using the TCP/IP protocol (84KB)19,canonpige_src.zipA Freeware MFC class to encapsulate Anonymous Pipes (8KB)20,ccmc_src.zipAn MFC class to encapsulate sending mail using CMC. (12KB)21,ceqd_src.zipA Freeware MFC class to support retri

3,245

社区成员

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

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