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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:27:36+00:00 2026-05-31T00:27:36+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, char *userData)

I’ve come up with the following C# signature:

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

Assuming the native code keeps a reference to all parameters for longer than the duration of the P/Invoke call:

  1. Aside from keeping an instance of the hOpen, should I also pin it?

  2. Should I keep a reference of the flags variable? Should I also pin it since it’s passed as a reference in this particular case?

  3. I’m assigning my callback delegate in the following way:

    private IntPtr callBackOnNativeEvents;
    ...
    this.callBackOnNativeEvents = Marshal.GetFunctionPointerForDelegate(
    new CallBack(this.CallBackOnNativeEvents));

    Should I keep a reference to the delegate in itself (not only the pointer)? Should I also pin it?

  4. Finally, I’m defining the userData parameter in the following way:

    private IntPtr userData;
    ...
    string userName = "test";
    this.userData = Marshal.StringToHGlobalAnsi(userName);

    Should I keep a reference to the string? Should I also pin it? The API documentation states that it copies the string content to unmanaged memory, but I’m not sure if it copies the content of the reference.

  • 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-31T00:27:37+00:00Added an answer on May 31, 2026 at 12:27 am
    1. No need to pin hOpen, it has value type semantics.
    2. If the DLL writes to the address pointed to by flags, and does so after the original function returns, then you need to pin it one way or another (as well as keeping it alive and safe from the clutches of the GC).
    3. The callback function pointer is already effectively pinned. You need to keep a reference to the delegate alive, but you don’t need to pin it because the native thunk is allocated from the unmanged heap.
    4. You don’t need to do anything special here because you are passing an IntPtr and the memory behind that is pinned. You don’t need to to keep a reference to the string alive because it is completely disconnected from the IntPtr returned by StringToHGlobalAnsi. It just has a copy of the contents of the string at the point of call to StringToHGlobalAnsi.

    I have to say that I am still incredulous that this DLL really could be doing what you say it is doing. I suspect that something else is going wrong which you are mis-diagnosing as the DLL holding onto pointer parameters from one call and then modifying their contents during subsequent calls. I find that exceptionally hard to believe, but of course only you can really know. If I was in your position I would simply ask the question of the DLL vendor.

    • 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.