Importing tlb file, and loading dll directly from file.

Hi.

I have a COM aware DLL written in C#, that can be called from Win32 programs via COM. I have managed to call the methods of the DLL by creating instance of it using CoCreateInstance. Here is the some code that i have used.

So I first import the dll's interface: "#import MyDll.tlb" named_guids". Then I create instance using CoCreateInstance. As I know, the DLL must be registered for this call to work I wish not to.

So here comes my guestion: Can I use TLB file to import the interface, and then create intances of objects that are in DLL that is NOT registered, so that I can simply define the direct path to the DLL

I'm making a plugin for a program that uses non registered DLLs, so they are just located inside a folder. I would do a native DLL that call Managed DLL, and they both should be in same folder or subfolder.


Answer this question

Importing tlb file, and loading dll directly from file.

  • gafferuk

    Here's an example that works with an XML COM object:

    http://www.perfectxml.com/articles/msxml/TipsAugust02.asp

    Note that DllGetClassObject is one of the 4 exports of the the COM object DLL, and you probably have the source code for it.  Typically this function returns the pointer to the class factory (IClassFactory), and you use it to create the actual COM objects.


  • themadprof

    You can call LoadLibrary followed by GetProcAddress on the function "DllGetClassObject" and party from there.
  • Jens-Christian Larsen

    The only problem with the MSDN C++ documentation is that you really have to already know what you are going to do, and what functions to call. I have no former experience in COM nor much with C++ either. The MSDN documentation really don't put the functions in any context, they just tell what function wants and what it returns.

    Ok, lets try another way. Do I proceed like this:

    1) Load the Dll using LoadLibrary
    2) Get the "QueryInterface" function pointer from dll handle using GetProcAddress
    3) use "QueryInterface" function pointer to get the object


    #EDIT: When trying to load Dll using LoadLibrary, I get error 126. I give correct path to the function, so does that mean that dll is not suitable to be loaded this way, or something
    HMODULE mod = LoadLibrary((LPCTSTR)"C:\\MyDll.dll");

  • billg51

    No example. I think you should be able to figure out the steps by searching the pieces in MSDN. it's not surprising that they say you shouldn't call DllGetClassObject directly--if you're going to use COM you shouldn't do it halfway and interfere with the OS implementation of COM support. But I believe if you're dealing with private dll's you can get the pointer to this function, instantiate your object, use QueryInterface, etc, and unload that DLL yourself when you know its safe to do so.

    An alternative is to call DllRegisterServer yourself, if needed. You first call CoCreateInstance, and if it fails, call this function and retry.

    Maybe you're trying too hard to not touch the registry in the first place.

    Brian


  • Henrik Dahl

    Do you have an example, I'm not quite sure how the get an object using those functions

    Btw, what's with this text in MSDN documentation: "You should not call DllGetClassObject directly." Is there a reason why the function should not be called

  • Importing tlb file, and loading dll directly from file.