Problem accessing a COM object in C #

I find it difficult to determine what I am doing wrong, so I thought that I would ask about it in SO. I am trying to automate the measurement task (Qualcomm QXDM), so I would like to access the COM interface exposed by the measurement tool. I wrote the following python code with works fine:

from comtypes.client import CreateObject QXDM = CreateObject("QXDM.Application") IQXDM2 = QXDM.GetIQXDM2 ... 

Now I am trying to rewrite this C # due to some specific requirements that I have. Here is what I tried:

 using QXDM; QXDM2Class IQXDM = new QXDM2Class(); 

But when I try to run this, I get:

 Retrieving the COM class factory for component with CLSID {6777AAE0-D9D2-4EA7-996B-0EECC68F97D8} failed due to the following error: 80040154. 

What am I doing wrong? I see all the methods and interfaces provided by QXDM in the object browser in Visual Studio.

Change Late binding seems to be the only way to do this, as Hans suggested. I changed the code to the following:

 Type QXDM = Type.GetTypeFromProgID("QXDM.Application"); Object QXDMObject = Activator.CreateInstance(QXDM); 

It works. The only problem is that I need to know which methods and classes are exposed to QXDM, which, I think, I could figure out using the object browser. Thanks everyone!

+4
source share
5 answers

Your code is just not the same. In Python code, you explicitly use the generic Application object. In many automation objects, the object from which you create others. Like IQXDM2 from the GetIQXDM2 () method.

Your C # code seems to be trying to create a class that implements IQXDM2 directly. It is impossible to work, you need to go through the application interface. As in the error message, there is no "class factory" for this object, this creates an application interface. Whatever the name, it almost always has an β€œapp” in the name. Use the "Object Browser" on the interop link to have a look. Look at the one that has the GetIQXDM2 method.

If you don’t see anything like it, you may add the wrong DLL. Look in the registry for the correct name. Run Regedit.exe and look at HKCR \ QXDM.Application. You will find the CLSID key with the instructor. Then look at HKCR\Clsid\{guid} , where {guid} is the manual you found. The key InprocServer32 has the name DLL. An off-process server uses the LocalServer32 key.

If this does not turn off, the COM server may be intended for use in scripting languages ​​only. Very unusual, but it can happen. In this case, you will have to use it at the end of the C # code. This is easy to do with the dynamic keyword C # 4.0 or VB.NET

+2
source

It may be the fact that the DLL depends on other DLLs. Debugging with ProcExp (http://www.sysinternals.com/) may give some light

+1
source

I think the COM objects are registered OK, or your Python code will fail and you will not see them on the machine. If so, your problem is accessing COM objects from C #. Try the described approach here .

If the library is already registered, you can follow these steps to have Visual Studio generate an interop assembly for you

+1
source

HRESULT 0x80040154 means that the class COM is not registered. Try running regsvr32 in the dll COM.

0
source

Perhaps you are building on 64 bit. Check the target platform. Make sure it is 32 bit.

0
source

Source: https://habr.com/ru/post/1335280/


All Articles