Implementing Power class as Component Object Model (Local-Server) in C++
This is the second type of how the COM (Component Object Model) can be registered and used.
Here is an implementation of simple Power class and IPower interface as Component Object Model
that have only one function for raise a number to the power of two.
For the Inproc-Server COM type, we creating:
- one .exe project (Server)
- one .dll project (Proxy&Stub)
- second .exe project (Client).
- The CoCreateInstance call queries the operating system for a registered class object Factory with the specified CLSID.
- If the Factory is registered, we go straight to point 7; otherwise, it is searched for in the registry path to the executable.
- The operating system finds the path.
- The executable (server) is run with the /Embedding parameter.
- The executable creates a class Factory.
- The executable calls CoRegisterClassObject with a Factory as one of the arguments.
- The system calls the CreateInstance method from the registered factory.
- CreateInstance gets a pointer to the IPower interface from the Power object.
- CreateInstance returns a pointer to IPower
- CoCreateInstance returns to the client a pointer to IPower.
- The client uses the Power object via the IPower interface.
To run this project:
-
Create the Console Application project in Visual Studio for the Server
(you can do it by click "Windows Destop Wizard" and choose console app)
-
Add all .cpp and .h files from github (Server/...) to your project.
-
Next step is to register the server. You can write your .reg file with the following code:
REGEDIT4
[HKEY_CURRENT_USER\Software\Classes\Wow6432Node\CLSID\{YOUR_CLASS_CLSID}]
@="<class_name>"
[HKEY_CURRENT_USER\Software\Classes\Wow6432Node\CLSID\{YOUR_CLASS_CLSID}\LocalServer32]
@="<path_to_Server.exe"
"ServerExecutable"="<path_to_Server.exe"
- Write your CLSID in place of YOUR_CLASS_ID (you can generate CLSID in Visual Studio by clicking Tools->Create GUID.
- Provide the path to the execution server file. To register server double click for the .reg file.
- !DON'T FORGET TO CHANGE ALL GUID's IN PROJECT FILES TO YOURS!
- Add a new Dynamic-link library project to your Solution.
- Only that you need is the .idl file that automatically generates all needed files for Proxy and Stub.
import "oaidl.idl";
[object, uuid(YOU_INTERFACE_GUID)]
interface IPower : IUnknown {
HRESULT power([in,out]int *val);
};
You can run this code by clicking right-click to the .idl file in VS Solution Explorer and then "Compile".
All generated files (.h, .cpp) should be added to the Proxy project.
- Create definition file, for exporting functions.
LIBRARY Proxy.dll
EXPORTS
DllCanUnloadNow PRIVATE
DllGetClassObject PRIVATE
DllRegisterServer PRIVATE
DllUnregisterServer PRIVATE
Next you need to add this file in Proxy project properties in Module Definition File section.
And add this libraries: rpcns4.lib, rpcrt4.lib, uuid.lib in Additionals Dependences section.
- In Proxy properties go to the (C/C++)->Preprocessor->Prepocessor Definition and add this line:
REGISTER_PROXY_DLL
Also, you need to specify Proxy CLSID (before generate it).
In properties go to (C/C++)->Command Line and write this:
/D"PROXY_CLSID_IS={YOUR_PROXY_GUID}"
All this is required to automatically register your proxy and stub.
But optionally you can do it manually with the .reg file
REGEDIT4
[HKEY_CURRENT_USER\Software\Classes\Wow6432Node\Interface\{YOUR_INTERFACE_IID}]
@="IPower"
[HKEY_CURRENT_USER\Software\Classes\Wow6432Node\Interface\{YOUR_INTERFACE_IID}\ProxyStubClsid32]
@="{YOUR_PROXY_GUID}"
[HKEY_CURRENT_USER\Software\Classes\Wow6432Node\CLSID\{YOUR_PROXY_GUID}]
@="PSFactoryBuffer"
[HKEY_CURRENT_USER\Software\Classes\Wow6432Node\CLSID\{YOUR_PROXY_GUID}\InprocServer32]
@="<path_to_Proxy.dll>"
"ThreadingModel"="Both"