I have written an article about the three methods of using COM components.Article: Three mechanisms for starting the COM component. Later, I added a non-registration-free method for using the out-of-process COM component, because it is only a text supplement and has no instances, you may not be very clear about how to implement it. You may receive emails from some students one after another, hoping to give an example. So I just want to add an article and I will review it myself.
The steps are as follows:
I have a COM component outside the com.exe process, which exposes an itestobject interface. Currently, customizedway.exe calls this COM component, but does not want to use the registry. Although Microsoft provides the Registry-free COM component mechanism, it currently only supports intra-process COM components (For details, refer to the previous article). However, since we know the ins and outs of a COM component being called, we can skip the MS set and complete the job by yourself (if you want to write a database dedicated to registry-free COM is not a problem ). This processCodeIs:
Hresult createmyremotehost (itestobject ** Ppresult)
{
// Get the COM server's full path (assume they are in the same folder)
Wchar_t file [max_path] = { 0 };
If ( ! Getmodulefilename (null, file, max_path ))
Return E_fail;
Wchar_t drive [_ max_drive] = { 0 };
Wchar_t dir [_ max_dir] = { 0 };
_ Wsplitpath_s (file, drive, _ max_drive, Dir, _ max_dir, null, 0 , Null, 0 );
_ Wmakepath_s (file, max_path, drive, Dir, l " Com " , L " EXE " );
// Start Process (it will insert its class factory object into a global table held by OS)
Startupinfo Si;
Process_information PI;
Si. CB = Sizeof (Startupinfo );
Si. lpreserved = NULL;
Si. lptitle = NULL;
Si. lpdesktop = NULL;
Si. dwx = Si. dwy = Si. dwysize = Si. dwxsize = 0 ;
Si. dwflags = Startf_useshowwindow;
Si. wshowwindow = Sw_hide;
Si. lpreserved2 = NULL;
Si. cbreserved2 = 0 ;
Bool RET = CreateProcess (file, null, false, 0 , Null, null, & Si, & Pi );
If ( ! RET) Return E_fail;
Waitforinputidle (PI. hprocess, 30000 );
// Get class factory (from class table)
Ccomptr < Iclassfactory > Pfactory;
Hresult HR = Cogetclassobject (clsid_testobject, clsctx_local_server, null, _ uuidof (pfactory), reinterpret_cast < Void **> ( & Pfactory ));
If (Failed (HR )) Return HR;
// Query interface by class factory
Return Pfactory -> Createinstance (null, _ uuidof ( * Ppresult), reinterpret_cast < Void **> (Ppresult ));
}
Note the following points in this process:
- Create a manifest
we need to create a manifest to contain information about the interface and TLB, because the com pointer needs to be rolled alling during interaction between two processes, this is done through TLB. Run the following command to create a TLB:
mt.exe-TLB: COM. TLB-dll: com.exe-out: com.exe. manifest
for the generated manifest, we need to make some modifications: The file generated by the handler is a single line by default, and manual line breaks are required for ease of reading.
- integration of manifest
after all, you need to embed this manifestfile into com.exeand customizedway.exe because both parties need to know how to restore alling. If you use the following command:
mt.exe-manifest com.exe. manifest-outputresource: com.exe
mt.exe-manifest com.exe. manifest-outputresource: mizmizedway.exe
when you start the Program , the system prompts that the following information cannot be found: ATL ##. DLL and msvcr ##. DLL, because the default manifest content will be overwritten here, and those content will contain information about some DLL of MFC and CRT. The two manifesters mergemust first be embedded in the exe(mt.exe-manifest ...htm), but a simple extension is used to replace com.exe. manifest is directly added to two projects, so that the content of the manifest will be added during build-you can use vs to open the EXE file and view the rt_manifest items in it.
Download sample: registryfree_comexe
Refer:
- Three Mechanisms for starting COM components
- Mt.exe
- Assembly manifest