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

  • Home
  • SEARCH
  • 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 7771361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:38:00+00:00 2026-06-01T16:38:00+00:00

I am writing a C# binding for an unmanaged C dll. The dll provides

  • 0

I am writing a C# binding for an unmanaged C dll.

The dll provides 5 hooks to return several data:

typedef void (*t_libpd_printhook)(const char *recv);

and exports a field like:

 EXTERN t_libpd_printhook libpd_printhook;

now i was using Interop Assistant to generate the binding code, which gave me just a delegate definition:

public delegate void t_libpd_printhook([In] [MarshalAs(UnmanagedType.LPStr)] string recv);

So is there some magic Interop function call i can use, to set the t_libpd_printhook field in the DLL?

  • 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-06-01T16:38:02+00:00Added an answer on June 1, 2026 at 4:38 pm

    You can use LoadLibrary and GetProcAddress to obtain a pointer to the exported libpd_printhook variable. You can then use Marshal.WriteIntPtr and Marshal.GetFunctionPointerForDelegate to assign to the delegate.

    [DllImport("kernel32", SetLastError=true, CharSet=CharSet.Unicode)]
    static extern IntPtr LoadLibrary(string lpFileName);
    
    [DllImport("kernel32", CharSet=CharSet.Ansi, ExactSpelling=true, 
        SetLastError=true)]
    static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
    
    [DllImport("kernel32.dll", SetLastError=true)]
    static extern bool FreeLibrary(IntPtr hModule);
    
    .....
    
    IntPtr lib = LoadLibrary(@"mydll.dll");
    IntPtr plibpd_printhook = GetProcAddress(lib, "libpd_printhook");
    Marshal.WriteIntPtr(plibpd_printhook, 
        Marshal.GetFunctionPointerForDelegate(mydelegate));
    FreeLibrary(lib);
    

    You will want to add the error checking that I excised in the interests of a concise example.

    Now, if you are in control of the unmanaged library I would still recommend adding a function to encapsulate writing to this function pointer. That feels like a better interface to me.

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

Sidebar

Related Questions

Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
I'm writing a .NET wrapper for an unmanaged DLL. The original DLL is C++
I'm trying to get intellisense working in MonoDevelop when writing an ObjC binding project.
I am writing WCF service that uses wsHttpBinding binding, which is not hosted in
I'm writing a java binding for a C code and I'm not really familiar
I'm writing some test code to emulate unmanaged code calling my c# implementation of
I'm writing my own UserControl which displays data in a ListBox. I want to
I am writing a custom WCF Binding and I want to run some code
I'm writing a C binding, and the C structure that I'm wrapping has some
I am writing this in XAML <TextBlock Text={Binding Path=Test} Grid.Column=1 Margin=52,46,32,50 Name=textBlockCheck /> and

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.