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?

0

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

6 Comments

Nope. I'm using it like this: Type t = Type.GetTypeFromProgID(progID); dynamic obj = Activator.CreateInstance(t); And it's always t == null;
So is it T that is null or OBJ?
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)
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.
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." (msdn.microsoft.com/en-us/library/hss5hw09.aspx)
|
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.

Comments

-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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.