DllGetClassObject() in C# COM DLL ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: DllGetClassObject() in C# COM DLL ?

  1. #1
    Join Date
    Jan 2006
    Posts
    4

    DllGetClassObject() in C# COM DLL ?

    Hi,

    I am working on a project where I have a simple COM DLL written in C# and I want my unmanaged modules (C++) to be able to access the COM class in this C# DLL. And it has to be the LoadLibrary()/DllGetClassObject() (COM's unregistering route). Does this means I should explicitly implement iClassFactory's functions to maintain this COM class's references in the C# DLL? I am a novice in C# and if that's the case, I wonder if that's a safe practice (for a novice, I mean).

    Anyway, I have no idea how/if this can be accomplished. I am aware of the COM registration route (regasm.exe/tlb import) and have practiced on that. However, unregistering this COM is what's best for the run time environment in my project. Any idea this can be done?

    Thanks,
    Mic

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,518

    Re: DllGetClassObject() in C# COM DLL ?

    If the C# code is exposed as COM, why not access it from C++ using its COM interface?

  3. #3
    Join Date
    Jan 2006
    Posts
    4

    Re: DllGetClassObject() in C# COM DLL ?

    Sorry I realized my previous post was not clear... This C# Dll currently contains a simple implementation similar to the following (excerpt from MS example):

    namespace CSharpServer
    {
    [Guid("DBE0E8C4-1C61-41f3-B6A4-4E2F353D3D05")]
    public interface IManagedInterface
    {
    int PrintHi(string name);
    }

    [Guid("C6659361-1625-4746-931C-36014B146679")]
    public class InterfaceImplementation : IManagedInterface
    {
    public int PrintHi(string name)
    {
    Console.WriteLine("Hello, {0}!", name);
    return 33;
    }
    }
    }

    That basically is the skeleton of my C# DLL. To be able to call InterfaceImplementation::PrintHi(..), currently I will need to go through gacutil.exe/regasm.exe to register this C# DLL. And as I understood, mscoree.dll will be the proxy from now on to handle the COM activation calls to this C# DLL... i.e., such as CoCreateInstance(..) in C++.

    I have tried the route above. However, that's not an ideal solution for the run-time environment of this project. Preferably, I'd like having the C# COM DLL to expose the DLLGetClassObject(..) and DLLCanUnloadNow(..) functions to the unmanaged C++. And internally in this C# COM DLL, it would need to implement IClassFactory.. and such to facilitate those COM activation calls. Just wondering, if this sounds right and feasible? And I have searched, perhaps not hard enough, looking for explanation or examples of that, but eventually ended up here.

    Thanks,
    Mic

  4. #4
    Join Date
    Jan 2006
    Posts
    4

    Re: DllGetClassObject() in C# COM DLL ?

    Re-posting the C# code with proper indention...

    namespace CSharpServer
    {
    [Guid("DBE0E8C4-1C61-41f3-B6A4-4E2F353D3D05")]
    public interface IManagedInterface
    {
    int PrintHi(string name);
    }

    [Guid("C6659361-1625-4746-931C-36014B146679")]
    public class InterfaceImplementation : IManagedInterface
    {
    public int PrintHi(string name)
    {
    Console.WriteLine("Hello, {0}!", name);
    return 33;
    }
    }
    }

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,518

    Re: DllGetClassObject() in C# COM DLL ?

    I guess I don't understand what you are trying to do. I mean if you create a COM dll using c or c++ and you want to use the dll, you'll register it as a COM dll, and use the COM sub-system to invoke and access it. If you didn't want to use COM to call the dll, then you wouldn't make the dll COM. This is kind of obvious.

    I don't understand why it would be any different if you made a COM dll in C# (or VB6 or VB.Net).

  6. #6
    Join Date
    Jan 2006
    Posts
    4

    Re: DllGetClassObject() in C# COM DLL ?

    I think I have found the answer here: https://blogs.msdn.microsoft.com/jun...m-net-interop/

    Thanks,
    Mic

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)