0

I am developing some code that uses a com-library. I'm trying to create an instance of the class through

Type t = Activator.CreateInstance("TypeID"); 

But I can not get the type it's all the time = null. progid already looked through the Ole / COM Wever, it seems that I did enter it correctly. In what may be prolem?

korovaisdead
  • 5,971
  • 4
  • 23
  • 30

3 Answers3

5

Are you passing the Type ID string? Try getting the type object from ProgId first:

Type t = Type.GetTypeFromProgID(progID);
object obj = Activator.CreateInstance(t);
Strillo
  • 2,902
  • 11
  • 13
  • Nope. I'm using it like this: Type t = Type.GetTypeFromProgID(progID); dynamic obj = Activator.CreateInstance(t); And it's always t == null; – korovaisdead Mar 20 '12 at 13:43
  • So is it T that is null or OBJ? – Strillo Mar 20 '12 at 13:46
  • Is the COM library registered with the system and are you sure you're not in a mixed 64/32 bit environment (for example: application is 64bit or "both" and COM library is 32bit) – Thorsten Dittmar Mar 20 '12 at 13:54
  • Yes, it's registered and i'm able to work with objects from it without Ativator. But i need to work with them right with Activator. – korovaisdead Mar 20 '12 at 14:25
  • Well according to the MSDN it GetTypeFromProgID returns "the type associated with the specified ProgID, if progID is a valid entry in the registry and a type is associated with it; otherwise, null." (http://msdn.microsoft.com/en-us/library/hss5hw09.aspx) – Strillo Mar 20 '12 at 15:31
  • Happy 2019, still using COM ¯\\_(ツ)_/¯ – Overlord Zurg Jan 02 '19 at 17:02
0

I would like to append to Strillo's answer, but I don't have the reputation required to add a comment.

I was getting the same behavior as shtpavel, until I manually registered the COM dll. Once I registered the COM dll, Strillo's answer worked for me.

regasm /tlb /codebase project.dll

Regasm can be found at:

C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/RegAsm.exe

The Build tab of the project settings view, under the Output title there is a "Register for COM interop" checkbox. I have proved to myself this does register the COM dll, but again Strillo's answer did not work for me until I manually registered the dll using the above command.

RegAsm - When is the /codebase option applicable? states that Visual Studio's "Register for COM interop" checkbox is the same thing as my command line solution. But my experience is, initially the "Register for COM interop" was not enough. Don't know why.

Community
  • 1
  • 1
Kevin Marshall
  • 191
  • 2
  • 7
-2

The problem is that Activator can not be used to instantiate a COM object that way (EDIT: I'm now emphasizing this as I understood from your question that you're passing the GUID directly to Activator.CreateInstance).

Usually you add a reference to the registered COM class (through the "Add Reference" dialog, "COM" tab). Then you can use the COM class like any other class.

Or you try what Strillo said :-)

However, doing I the way I suggested has the advantage of still remaining strongly typed.

Thorsten Dittmar
  • 52,871
  • 8
  • 78
  • 129
  • I understood from the OPs question that he's passing the COM classes GUID to `Activator.CreateInstance` - that why I wrote he can not use it *that way*. I didn't mean to say that `Activator` can't be used to create COM objects *at all*. – Thorsten Dittmar Mar 20 '12 at 13:57