com_sample2

所属分类:ActiveX/DCOM/ATL
开发工具:Visual C++
文件大小:20KB
下载次数:11
上传日期:2006-04-09 10:25:16
上 传 者张启
说明:  < COM编程精彩实例>>一书的随书源代码光盘的代码,这是第二章的。
(lt; Lt; COM programming wonderful example gt; Gt; A book with the CD-ROM source code, which is the second chapter.)

文件列表:
C2\RESOURCE.H (684, 1999-11-14)
C2\StdAfx.cpp (208, 1999-11-14)
C2\StdAfx.h (1054, 1999-11-14)
C2\TestDlg.cpp (5561, 2000-02-21)
C2\TestDlg.h (1353, 2000-02-21)
C2\Tester.cpp (2110, 2000-02-21)
C2\Tester.dsp (4186, 2000-02-21)
C2\Tester.dsw (907, 2000-02-21)
C2\Tester.h (1324, 1999-11-14)
C2\Tester.rc (5485, 1999-12-13)
C2\Wizard (0, 2006-04-09)
C2\Server\Errors.h (565, 1999-12-04)
C2\Server\RESOURCE.H (499, 1999-12-04)
C2\Server\Server.cpp (1940, 1999-12-04)
C2\Server\Server.def (220, 1999-12-04)
C2\Server\Server.dsp (12813, 1999-12-04)
C2\Server\Server.h (5931, 2000-02-21)
C2\Server\Server.idl (784, 1999-12-04)
C2\Server\Server.rc (2835, 1999-12-04)
C2\Server\Serverps.def (248, 1999-12-04)
C2\Server\Serverps.mk (442, 1999-12-04)
C2\Server\StdAfx.cpp (315, 1999-12-04)
C2\Server\StdAfx.h (908, 1999-12-04)
C2\Server\WZD.H (680, 1999-12-04)
C2\Server\Wzd.cpp (256, 1999-12-04)
C2\Server\Wzd.rgs (582, 1999-12-04)
C2\Server (0, 2006-04-09)
C2\RES\Tester.ico (1078, 1999-11-14)
C2\RES\Tester.rc2 (398, 1999-11-14)
C2\RES (0, 2006-04-09)
C2 (0, 2006-04-09)

///////////////////////////////////////////////////////////////////// // How to use the COM object. ///////////////////////////////////////////////////////////////////// 1) The client should call the following to initialize the COM DLL from the main process _and_ from any thread that will be using COM: ::CoInitializeEx( NULL, //always NULL COINIT_APARTMENTTHREADED //see book about threading models ); NOTE: You also need to add _WIN32_DCOM to your project settings under "Preprocessor definitions" in order to get the prototype definition for ::CoInitializeEx() included in your compile. 2) Import the COM class's DLL, EXE, TLB, or OCX file into the source file that will be using the object: //Using the following requires you to specify a scope for every reference //to the COM class (i.e. SERVERLib::IWzdPtr) (This scope is the name of the //name used in the Library statement in the IDL file.) #import "server\server.tlb" //You don't need the scope operator with the no_namespace designation: #import "server\server.tlb" no_namespace //Using the following keeps the compiler from adding extra error checking //code around every method call you make to the imported COM object. If //you want to customize the way you handle errors, use this: #import "server\server.tlb" raw_interfaces_only 3) To create a COM object, use this: IWzdPtr pPtr1( __uuidof(Wzd) //guid of DLL or EXE that contains class ); where the class name is the original interface name (ex: IWzd) with a "Ptr" suffix. 4) To create the object using a smart pointer's method instead of in its constructor so that you can get the error code, use: IWzdPtr pPtr2; HRESULT hr=pPtr2.CreateInstance( __uuidof(Wzd) //guid of DLL or EXE that contains class ); 5) To call it's member variables, use this: pPtr1->Method1(1234); Note that even though the class is created on the stack, the pointer operator is used. Smart pointers override the pointer operator to denote you are accessing the methods of the created object. To access the methods of the smart pointer class (ex: QueryInterface()), use the dot syntax (pPtr.QueryInterface()). 6) To catch COM errors, use: try { } catch (_com_error &err) { AfxMessageBox(err.ErrorMessage()); return; } 7) Unless you use the raw_interfaces_only attribute when importing a COM class's type library, method calls will check to make sure a object is created and will also throw a _com_error structure if the method returns a failure. If no error occurs, the method call will also make any retval argument into a returned value: HRESULT hr=pPtr1->Method1(&retval); becomes: long retval=pPtr->Method1(); ///////////////////////////////////////////////////////////////////// // From: COM Programming by Example by John E. Swanke // Copyright (C) 2000 jeswanke. All rights reserved. /////////////////////////////////////////////////////////////////////

近期下载者

相关文件


收藏者