PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSRegFreeCOMClient/ReadMe.txt

#
Plain Text | 381 lines | 293 code | 88 blank | 0 comment | 0 complexity | e85fcbef14119be2d715e136726be841 MD5 | raw file
  1. ========================================================================
  2. LIBRARY APPLICATION : CSRegFreeCOMClient Project Overview
  3. ========================================================================
  4. /////////////////////////////////////////////////////////////////////////////
  5. Summary:
  6. Registration-free COM is a mechanism available on the Microsoft Windows XP
  7. (SP2 for .NET Framework-based components), Microsoft Windows Server 2003 and
  8. newer platforms. As the name suggests, the mechanism enables easy (e.g.
  9. XCOPY) deployment of COM components to a machine without the need to register
  10. them.
  11. The CSRegFreeCOMClient sample demonstrates how to create a registration-free
  12. COM from the aspect of .NET Framework based client, so that the client can
  13. consume existing COM server as if the COM server is Registration-free.
  14. /////////////////////////////////////////////////////////////////////////////
  15. Sample Relation:
  16. (Relation of the current sample and other samples in
  17. Microsoft All-In-One Code Framework http://cfx.codeplex.com)
  18. CSRegFreeCOMClient -> ATLDllCOMServer
  19. /////////////////////////////////////////////////////////////////////////////
  20. Creation:
  21. A. The client automatically searches manifest file of its dependency,
  22. and use the manifest file to Activate the activation context.
  23. Step1.
  24. Add a reference to the ATLDllCOMServer COM component by right-clicking
  25. the project, selecting "Add Reference...", turning to the COM tab, and
  26. adding ATLDllCOMServer Type Library. Visual Studio will automatically
  27. generate an interop assembly "Interop.ATLDllCOMServerLib.dll". If the
  28. ATLDllCOMServer Type Library is not in the list, please build and setup
  29. the ATLDllCOMServer sample first.
  30. Step2.
  31. Create a thread whose thread apartement is set to STA, because we are
  32. going to demonstrate the instantiation and use of a STA COM object:
  33. ATLDllCOMServer.SimpleObject.
  34. Step3.
  35. Create method ConsumeCOMComponent which contains logic of consuming COM
  36. server.
  37. Step4.
  38. Add app.manifest file by right-clicking the project, selecting "Add"
  39. --> "New Item" --> "General" --> "Application Manifest File" and click
  40. Add button.
  41. Step5.
  42. Open the app.manifest file, find assemblyIdentity element, modify its
  43. Name property to CSRegFreeCOMClient:
  44. <assemblyIdentity version="1.0.0.0" name="CSRegFreeCOMClient"/>
  45. Step6.
  46. Add dependency element immediate after assemblyIdentity node:
  47. <dependency>
  48. <dependentAssembly>
  49. <assemblyIdentity
  50. type="win32"
  51. name="ATLDllCOMServer.X"
  52. version="1.0.0.0" />
  53. </dependentAssembly>
  54. </dependency>
  55. Step7.
  56. Open property page of the app.manifest file by right-clicking the file
  57. and selecting Property, change the value of Build Action dropdown to
  58. Embedded Resource, and rebuild the application.
  59. Step8.
  60. Open the application's output folder (Debug or Release folder), put a
  61. copy of ATLDllCOMServer.dll to this folder, create a txt file and rename
  62. it (including file extention) to ATLDllCOMServer.X.manifest, input the
  63. following content:
  64. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  65. <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  66. <assemblyIdentity
  67. type="win32"
  68. name="ATLDllCOMServer.X"
  69. version="1.0.0.0" />
  70. <file name = "ATLDllCOMServer.dll">
  71. <comClass clsid="{92FCF37F-F6C7-4F8A-AA09-1A14BA118084}"
  72. threadingModel = "Apartment" />
  73. <typelib tlbid="{9B23EFED-A0C1-46B6-A903-218206447F3E}"
  74. version="1.0" helpdir=""/>
  75. </file>
  76. <comInterfaceExternalProxyStub
  77. name="ISimpleObject"
  78. iid="{830F85D0-91B9-406D-A273-BC33133DD44B}"
  79. proxyStubClsid32="{00020424-0000-0000-C000-000000000046}"
  80. baseInterface="{00000000-0000-0000-C000-000000000046}"
  81. tlbid = "{9B23EFED-A0C1-46B6-A903-218206447F3E}" />
  82. </assembly>
  83. Step9.
  84. Unregistry the ATLDllCOMServer.dll, then run CSRegFreeCOMClient.exe.
  85. the output is:
  86. ------Activate activation context automatically------
  87. Call HelloWorld => HelloWorld
  88. Press any key to continue...
  89. B. The client activates activation context by specifying manifest file
  90. manually.
  91. Step1.
  92. Add a reference to the ATLDllCOMServer COM component by right-clicking
  93. the project, selecting "Add Reference...", turning to the COM tab, and
  94. adding ATLDllCOMServer Type Library. Visual Studio will automatically
  95. generate an interop assembly "Interop.ATLDllCOMServerLib.dll". If the
  96. ATLDllCOMServer Type Library is not in the list, please build and setup
  97. the ATLDllCOMServer sample first.
  98. Step2.
  99. Create a thread whose thread apartement is set to STA, because we are
  100. going to demonstrate the instantiation and use of a STA COM object:
  101. ATLDllCOMServer.SimpleObject.
  102. Step3.
  103. Create method ConsumeCOMComponent which contains logic of consuming COM
  104. server.
  105. Step4.
  106. Create the ActivateActivationContext method which is used to activate
  107. the activation context according to given manifest file.
  108. Step5.
  109. Add app.manifest file by right-clicking the project, selecting "Add"
  110. --> "New Item" --> "General" --> "Application Manifest File" and click
  111. "Add" button.
  112. Step6.
  113. Open the app.manifest file, find assemblyIdentity element, modify its
  114. Name property to CSRegFreeCOMClient:
  115. <assemblyIdentity version="1.0.0.0" name="CSRegFreeCOMClient"/>
  116. Step7.
  117. Add dependency element immediate after assemblyIdentity node:
  118. <dependency>
  119. <dependentAssembly>
  120. <assemblyIdentity
  121. type="win32"
  122. name="ATLDllCOMServer.X"
  123. version="1.0.0.0" />
  124. </dependentAssembly>
  125. </dependency>
  126. Step8.
  127. Open property page of the app.manifest file by right-clicking the file
  128. and select Property, change the value of Build Action dropdown to
  129. Embedded Resource, and rebuild the application.
  130. Step9.
  131. Create a new folder, for example, "D:\regfreecom", put a copy of
  132. ATLDllCOMServer.dll to this folder, create a txt file and rename it
  133. (including file extention) to ATLDllCOMServer.X.manifest, input following
  134. content:
  135. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  136. <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  137. <assemblyIdentity
  138. type="win32"
  139. name="ATLDllCOMServer.X"
  140. version="1.0.0.0" />
  141. <file name = "ATLDllCOMServer.dll">
  142. <comClass clsid="{92FCF37F-F6C7-4F8A-AA09-1A14BA118084}"
  143. threadingModel = "Apartment" />
  144. <typelib tlbid="{9B23EFED-A0C1-46B6-A903-218206447F3E}"
  145. version="1.0" helpdir=""/>
  146. </file>
  147. <comInterfaceExternalProxyStub
  148. name="ISimpleObject"
  149. iid="{830F85D0-91B9-406D-A273-BC33133DD44B}"
  150. proxyStubClsid32="{00020424-0000-0000-C000-000000000046}"
  151. baseInterface="{00000000-0000-0000-C000-000000000046}"
  152. tlbid = "{9B23EFED-A0C1-46B6-A903-218206447F3E}" />
  153. </assembly>
  154. Step10.
  155. Unregistry the ATLDllCOMServer.dll, launch cmd.exe and navigate to the
  156. folder where CSRegFreeCOMClient.exe exists, start CSRegFreeCOMClient.exe
  157. with command:
  158. CSRegFreeCOMClient.exe someargs
  159. then, you will be required to input manifest file path:
  160. ------ Activate activation context manually ------
  161. Please input the full path of manifest file:
  162. in this case, since we put the manifest file at D:\regfreecom, so we
  163. input:
  164. D:\regfreecom\ATLDllCOMServer.X.manifest
  165. the full output is:
  166. ------ Activate activation context manually ------
  167. Please input the full path of manifest file:
  168. D:\regfreecom\ATLDllCOMServer.X.manifest
  169. Call HelloWorld => HelloWorld
  170. Press any key to continue...
  171. /////////////////////////////////////////////////////////////////////////////
  172. FAQ:
  173. 1. How can I get the CLSID, tlbID, iid etc if it is a third-party dll?
  174. To build a reg-free version for a given com dll, the key point it to
  175. construct a correct manifest file for the com dll, here is some tip for you:
  176. 1. Make sure that the dll has no embedded manifest, if there is, a
  177. separate manifest file will no take effect, manifest view can help to
  178. verify whether a dll has embedded manifest.( but you can modify the
  179. embedded manifest using some tools, anyway, it is not suggested).
  180. 2. Create a manifest file for the dll, the file usually looks like:
  181. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  182. <assembly xmlns="urn:schemas-microsoft-com:asm.v1"
  183. manifestVersion="1.0">
  184. <assemblyIdentity
  185. type="win32"
  186. name=" mycomdll.x"
  187. version="1.0.0.0" />
  188. <file name = "mycomdll.dll">
  189. <comClass
  190. clsid="{[CLSID_ MyComClass]}"
  191. threadingModel = "Apartment" />
  192. <typelib tlbid="{[LIBID_ MyComClass]}"
  193. version="1.0" helpdir=""/>
  194. </file>
  195. <comInterfaceExternalProxyStub
  196. name=" ISomeInterface "
  197. iid="{[IID_I SomeInterface]}"
  198. proxyStubClsid32="{00020424-0000-0000-C000-000000000046}"
  199. baseInterface="{00000000-0000-0000-C000-000000000046}"
  200. tlbid = "{[LIBID_ MyComClass ]}" />
  201. </assembly>
  202. 3. To get values for placeholders in the manifest, we can use OLE/COM
  203. ObjectViewer (Oleview.exe) tool.
  204. Open Oleview.exe ( on Vista or Win7, you may run it as administrator),
  205. click File -> View TypeLib --> select target com dll and click OK,
  206. let's open ATLDllCOMServer.dll, and you will see:
  207. // Generated .IDL file (by the OLE/COM Object Viewer)
  208. //
  209. // typelib filename: ATLDllCOMServer.dll
  210. [
  211. uuid(9B23EFED-A0C1-46B6-A903-218206447F3E),
  212. version(1.0),
  213. helpstring("ATLDllCOMServer 1.0 Type Library"),
  214. custom(DE77BA64-517C-11D1-A2DA-0000F8773CE9, 117441012),
  215. custom(DE77BA63-517C-11D1-A2DA-0000F8773CE9, 1269332262),
  216. custom(DE77BA65-517C-11D1-A2DA-0000F8773CE9, "Created by MIDL version 7.00.0500 at Tue Mar 23 16:17:41 2010
  217. ")
  218. ]
  219. library ATLDllCOMServerLib
  220. {
  221. // TLib : // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
  222. importlib("stdole2.tlb");
  223. // Forward declare all types defined in this typelib
  224. dispinterface _ISimpleObjectEvents;
  225. interface ISimpleObject;
  226. [
  227. uuid(87AD6FBC-8735-407C-9758-C80B48C78E7C),
  228. helpstring("_ISimpleObjectEvents Interface")
  229. ]
  230. dispinterface _ISimpleObjectEvents {
  231. properties:
  232. methods:
  233. [id(0x00000001), helpstring("method FloatPropertyChanging")]
  234. void FloatPropertyChanging(
  235. [in] single NewValue,
  236. [in, out] VARIANT_BOOL* Cancel);
  237. };
  238. [
  239. uuid(92FCF37F-F6C7-4F8A-AA09-1A14BA118084),
  240. helpstring("SimpleObject Class")
  241. ]
  242. coclass SimpleObject {
  243. [default] interface ISimpleObject;
  244. [default, source] dispinterface _ISimpleObjectEvents;
  245. };
  246. [
  247. odl,
  248. uuid(830F85D0-91B9-406D-A273-BC33133DD44B),
  249. helpstring("ISimpleObject Interface"),
  250. dual,
  251. nonextensible,
  252. oleautomation
  253. ]
  254. interface ISimpleObject : IDispatch {
  255. [id(0x00000001), propget, helpstring("property FloatProperty")]
  256. HRESULT FloatProperty([out, retval] single* pVal);
  257. [id(0x00000001), propput, helpstring("property FloatProperty")]
  258. HRESULT FloatProperty([in] single pVal);
  259. [id(0x00000002), helpstring("method HelloWorld")]
  260. HRESULT HelloWorld([out, retval] BSTR* pRet);
  261. [id(0x00000003), helpstring("method GetProcessThreadID")]
  262. HRESULT GetProcessThreadID(
  263. [out] long* pdwProcessId,
  264. [out] long* pdwThreadId);
  265. };
  266. };
  267. 6. With above information, we can fill in the manifest as following:
  268. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  269. <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  270. <assemblyIdentity
  271. type="win32"
  272. name="ATLDllCOMServer.X"
  273. version="1.0.0.0" />
  274. <file name = "ATLDllCOMServer.dll">
  275. <comClass clsid="{92FCF37F-F6C7-4F8A-AA09-1A14BA118084}"
  276. threadingModel = "Apartment" />
  277. <typelib tlbid="{9B23EFED-A0C1-46B6-A903-218206447F3E}"
  278. version="1.0" helpdir=""/>
  279. </file>
  280. <comInterfaceExternalProxyStub
  281. name="ISimpleObject"
  282. iid="{830F85D0-91B9-406D-A273-BC33133DD44B}"
  283. proxyStubClsid32="{00020424-0000-0000-C000-000000000046}"
  284. baseInterface="{00000000-0000-0000-C000-000000000046}"
  285. tlbid = "{9B23EFED-A0C1-46B6-A903-218206447F3E}" />
  286. </assembly>
  287. /////////////////////////////////////////////////////////////////////////////
  288. References:
  289. Registration-Free Activation of COM Components: A Walkthrough
  290. http://msdn.microsoft.com/en-us/library/ms973913.aspx
  291. Registration-Free Activation of .NET-Based Components: A Walkthrough
  292. http://msdn.microsoft.com/en-us/library/ms973915.aspx
  293. /////////////////////////////////////////////////////////////////////////////