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 8627447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:16:58+00:00 2026-06-12T08:16:58+00:00

In writing P/Invoke wrapper for a native dll, I found myself with a lot

  • 0

In writing P/Invoke wrapper for a native dll, I found myself with a lot of code looking like this:

// Declare delegate types matching some callbacks in the dll
delegate void d1(T1, T1);
delegate void d2(T1,T2,T3);

// Some functions
void f1(T1 a, T2 b)
{
..
}

void f2(T1 a, T2 b, T3 c)
{
}

Then later,

// Marshal some instantiated delegates into IntPtrs to pass to native dll
IntPtr a = Marshal.GetFunctionPointerForDelegate(new d1(f1));
IntPtr b = Marshal.GetFunctionPointerForDelegate(new d2(f2));

So I end up with quite a lot of code looking like the above. I thought some refactoring using a generic function might be nice, something like this:

static void foo<T>(ref IntPtr ptr, T f)  where T: System.Delegate, new()
{
   ptr = Marshal.GetFunctionPointerForDelegate(new T(f));
}

Which would allow me to then write:

foo<d1>(a,f1);
foo<d2>(b,f2);

and so on. It doesn’t compile! I tried to add some type constraints onto the function declaration, but can’t get it to work. It’s not that important to me in this instance, as the re-factoring is hardly very important, but I’m just curious to know how I’d do something like this?

  • 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-12T08:17:00+00:00Added an answer on June 12, 2026 at 8:17 am

    Sadly, you cannot constraint a type to inherit from System.Delegate. This limitation has frustrated me many times. The only way around this for you is to constrain the delegate to be a reference type and then do a nasty-ish cast:

        static void foo<T>(out IntPtr ptr, T f) where T : class
        {
            ptr = Marshal.GetFunctionPointerForDelegate( (Delegate)(object)f );
        }
    

    You are not able to do a new T(f) because the T:new() constraint only allows a parameterless constructor. The good news is that this is unnecessary as T is already a delegate type. You would need to invoke it like:

        foo<d1>(out ptr, f1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been writing a thin wrapper around SQLite using P/Invoke, and I'd like to
In a function that I am writing myself, I would like to invoke the
Writing Classic ASP code in JScript has a lot going for it: more humane
I'm writing a routine to invoke methods, found by a name and an array
I'm writing a compiler that generates Jasmin code and want to invoke a method
I'm trying to call a Dll function which looks like this (in the C/C++
I'm writing a VisualC++ program which have code invoke ffmpeg.exe to convert video file.
I've recently been writing a lot of code in C and am now switching
is there a way to invoke functions in native libraries (Windows .dll's, Linux .so's),
I am writing a wrapper around C DLL in C++/CLI so that it can

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.