Non-registered COM communication "original" for out-of-process component communication

Source: Internet
Author: User


Recently in the process outside the components of communication, wrote a demo, free registration, has not been transferred, in fact, is two problems card for several days, also did not find useful information, tried for a few days finally solved, is simple record down, so that everyone with me go the same detour. The following COM-side program name abbreviated to Com.exe
Problems encountered:

1. Call CoGetClassObject, return hr = 0x80040154 No Registration Class (original: Registered version of unregistered, non-registered version of not running)
2. IClassFactory::CreateInstance (), return hr = 0X80029C4A An error occurred while loading the type library/dll. (two exe is not in the same directory or inconsistent with the path specified in manifest)

First, the implementation of the steps:
1. Need to add *.exe.manifest file, add into Project, Build.
Note: The COM side exe path that is filled in manifest must be the real storage path of Com.exe (default in the sibling directory), can be set relative path (such as Other\\comexedemo.exe), can be embedded in EXE, do not need to be placed in the EXE sibling directory.
Manifest file Example:
<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?>
<assembly xmlns= "urn:schemas-microsoft-com:asm.v1" manifestversion= "1.0" >
<file name= "Other\\com.exe" hashalg= "SHA1" >
<typelib tlbid= "{f8311364-981d-4237-a33e-6738dcd1457b}" version= "1.0" helpdir= "" flags= "HASDISKIMAGE" ></ Typelib>
</file>
<cominterfaceexternalproxystub name= "Imyevent" iid= "{871965eb-d4bc-4131-9f97-2714105f9e76}" tlbid= "{ f8311364-981d-4237-a33e-6738dcd1457b} "proxystubclsid32=" {00020424-0000-0000-c000-000000000046} "></ Cominterfaceexternalproxystub>
</assembly>

2. #import "*.tlb", the directory is arbitrary, do not need to be placed in the EXE sibling directory.

3. The client and the COM side must run with the same permissions. such as: the same as administrator rights or the same as general permissions, otherwise failed. You can use Creatprocess (other\\comexedemo.exe) on the caller to ensure that you have unified permissions.

4. The client must ensure that the Com.exe is in the running state before obtaining the COM port interface, the Com.exe of this operation must be the path specified in the manifest file (usually in the same class as the caller's EXE), and be unified with the call-side permissions.
Client Get Interface code example:

Second, the relevant code


//Initialize
AfxOleInit ();
Or:: CoInitialize (NULL);

//Start the COM-side process (Permissions unified OH)
//non-mandatory-----------------------------------------
Startupinfo si;
Process_information Pi;
:: ZeroMemory (&si,sizeof (startupinfo));
SI.CB = sizeof (STARTUPINFO);
Si.dwflags = Startf_useshowwindow;
Si.wshowwindow = sw_hide;
BOOL B =:: CreateProcess (_t ("E:\\codeexercise\\vs2005\\comdemo1client\\debug\\11\\comexedemo.exe"), Null,null, NULL,FALSE,0,NULL,NULL,&SI,&PI);
//non-mandatory-----------------------------------------

//Get interface
ccomptr<iclassfactory> pfactory;
HRESULT hr = CoGetClassObject (clsid_myevent, Clsctx_local_server, NULL, __uuidof (pfactory), reinterpret_cast<void* *> (&pfactory));
Imyeventptr PResult;
hr = pfactory->createinstance (NULL, __uuidof (imyevent), reinterpret_cast<void**> (&presult));

  In fact, the difference between the registered version and the registration-free version is one with the registry, one with manifest instead of the registry, The registered version of the build at the time of the Registry to write dead Com.exe absolute path (can modify the RGS file so that it does not register), but the registration-free version should pay attention to the manifest file to ensure that the com.exe path problem. Permissions issue both versions are the same, get unified!

Non-registered COM communication "original" for out-of-process component communication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.