Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 576459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:02:58+00:00 2026-05-13T14:02:58+00:00

I have a library that exports an unmanaged C routine that returns pointers to

  • 0

I have a library that exports an unmanaged C routine that returns pointers to COM IUnknown-based objects. The DLL is not registered, and is not server. I would like to use the COM types from C#.

I have already written C# interfaces for the COM types. What is the C# equivalent of calling LoadLibrary() and GetProcAddress()? How would I call the result of GetProcAddress() and then call it to load a COM interface pointer?

Here is a snippet of C++ that illustrates what I am after:

// Assume I have previously declared IMyType
HANDLE mylib = ::LoadLibrary("myfakecom.dll");
IUnknown* (*GetterFunc) getter;
getter = (GetterFunc)::GetProcAddress(mylib, "GetFactory");
IUnknown *unk = getter();
IMyType *mytype = unk->QueryInterface(IID_MYTYPE);

My gut says “Do it with C++/CLI”, though I am unsure of how I would do this for any generic type, and how I would coerce the raw IUnknown pointers into the manager pointer types I would declare using the [Guid] attributes on a managed interface.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:02:58+00:00Added an answer on May 13, 2026 at 2:02 pm

    You just need to use interop to describe your GetFactory function, something like this

    [DllImport("myfakecom.dll")]
    [return: MarshalAs(UnmanagedType.IUnknown)]
    static extern object GetFactory();
    

    Then once you have the object in managed code. A cast is the equivalent of QueryInterface

    void Foo(Object unk)
    {
        IMyType mytype = (IMyType)unk;
    } 
    

    You will need to duplicate your C++ interface definitions as C# interface definitions, possibly with [marshalas] attributes. But since you have already done that, the rest should be easy.

    I would suggest that you change your factory prototype from

    IUnknown * GetFactory();
    

    to

    HRESULT GetFactory([out] IUnknown ** ppunk);
    

    There seems to be a strong assumption by the COM iterop code that all COM methods return HRESULT and it will be simpler to get the marshalling to work if you go with the flow there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does Linux have a shared library that exports OS functions? msvcrt.dll -> libc.so.6 kernel32.dll
I have a java library that I am accessing in VB.NET via COM. The
I have a 3rd party library that's written in C. It exports all of
I have a native/unmanaged C++ library with a number of classes that I would
I have a library that I would like to license and distribute. I know
I have a library that interacts with our phone system, ie, Hey phone, call
I have a library that does I/O. There are a couple of external knobs
I have a library that I created with some business logic that includes writing
Whenever I have a library that use across different websites/ applications I've always just
Common scenario: I have a library that uses other libraries. For example, a math

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.