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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:22:13+00:00 2026-05-30T17:22:13+00:00

I have a DLL from which I need to P/Invoke the following C method:

  • 0

I have a DLL from which I need to P/Invoke the following C method:

int DAOpen(HANDLE *hOpen, UNIT *flags, void *callback, void *userData)

Using the P/Invoke Assistant, I’ve come up with the following signature:

[DllImportAttribute("<libName>", EntryPoint="DAOpen")]  
    static extern  int DAOpen(  
    out IntPtr hOpen,  
    ref uint flags,  
    IntPtr callback,  
    IntPtr userData);

The callback parameter has must correspond to the following C signature:

void CallBack(  
    int event,  
    int socket,  
    struct _iComStructure *plcom,  
    void *eventData,  
    void *myData)

which translates to

delegate void CallBack(  
    int @event,  
    int socket,  
    IntPtr plcom,  
    IntPtr eventData,  
    IntPtr myData);

Now, I’m doing the call in the following way:

void MyCallBack(int @event, int socket, IntPtr plcom, IntPtr eventData, IntPtr myData)  
{  
    // does nothing for now  
}

IntPtr hOpen;  
uint flags = 0;  
CallBack callBack = MyCallBack;  
StringBuilder userData = new StringBuilder(userData.ToString());


int rc = DAOpen(
out hOpen,    
ref flags,  
Marshal.GetFunctionPointerForDelegate(callBack),  Marshal.StringToHGlobalAnsi(userData.ToString()));

The problem I’m having with this code is that it either crashes or I get a FatalExecutionEngineError thrown when I perform a call to DAOpen. I know it must be a marshalling error, but I can’t figure it out…

Thanks for your help!

UPDATE

As was suggested, I tried to pin the callback in the following way:

GCHandle callbackHandle = GCHandle.Alloc(callBack, GCHandleType.Pinned);

I get an ArgumentException with this message:

Object contains non-primitive or non-blittable data

It seems to work fine if I don’t specify the GCHandleType (i.e. I get a handle), but of course the original problem remains.

Is there any reason why I can’t pin the delegate?

Thanks for your input!

  • 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-30T17:22:14+00:00Added an answer on May 30, 2026 at 5:22 pm

    Have you taken any steps to ensure the delegate is not garbage collected?

    A gen 0 collection could be triggered during your call.
    Try either creating a local variable in the calling function,
    or manually pinning the delegate in a GC handle.

    Update:

    Also, look at the calling convention
    of the callback in the native API. Is it stdcall?
    If not, you can’t use GetFunctionPointerForDelegate, as it
    returns a pointer to a native function expecting stdcall arguments.

    In that case, instead of us an intptr, use the delegate type and put an “UnmanagedFunctionPointerAttribute” on the delegate definition that specifies the native calling convention.

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

Sidebar

Related Questions

I have a DLL from which I need to P/Invoke the following C method:
I have a DLL from which I need to P/Invoke the following C method:
I have an unmanaged (C/C++) DLL which I need to call from a C#
I have a DLL file from which I need the memory address of a
I have a .NET dll which needs to read it's config settings from it's
I have a method I want to import from a DLL and it has
We have inherited VB6 dll which we need to make changes to. We have
I have a c++ dll which I need to debug. Due to the circumstances
I have an Asp.net application which require a third-part .dll from other department(In fact,it
I have dll which accepts Pointer to array of bytes from C++ and try

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.