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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:17:23+00:00 2026-06-17T18:17:23+00:00

I use PInvoke to access a C++ library from c# code. But I have

  • 0

I use PInvoke to access a C++ library from c# code. But I have problems using the following function:

UINT32 myfunc(IN char * var1, IN UINT32 var2, INOUT UINT16 * var3, OUT UINT8 * var4)

documentation says:

return parameter is error code

var1 is a string

var2 is an integer

var3 is a pointer to an integer (the value will be the size of var4, the value will be changed by the function)

var4 is a pointer to a buffer that will be filled by the function

When I use

[DllImport("mydll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int myfunc(string var1, int var2, out int var3, 
    out byte[] var4);

The function gives the following exception:

Managed Debugging Assistant ‘FatalExecutionEngineError’ has detected a problem in ‘C:\Users…\Documents\Projects…\bin\Debug…vshost.exe’.

Additional Information: The runtime has encountered a fatal error. The address of the error was at 0x74292e44, on thread 0x2edc. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

If there is a handler for this exception, the program may be safely continued.

I tried to work with parameters to see if I can get anything and I changed charset as below:

[DllImport("mydll.dll, CallingConvention = CallingConvention.Cdecl", CharSet = CharSet.Unicode)]
public static extern int myfunc(string var1, int var2, out int var3, out byte[] var4);

In this case, it does not give FatalExecutionEngineError, instead the function returns without error. But the return value indicates that an internal error hass occurred in the function. (I don’t know why the charset change prevents the FatalExecutionEngineError.)

I am not sure what I am doing wrong, please help me.

  • 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-17T18:17:24+00:00Added an answer on June 17, 2026 at 6:17 pm

    The p/invoke declaration should look like this:

    [DllImport("mydll.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int myfunc(
        string var1, 
        uint var2, 
        ref ushort var3, 
        byte[] var4
    );
    

    And then you call it like this:

    string var1 = "...";
    uint var2 = ...;
    ushort var3 = 1024;//use whatever length buffer you want to pass
    byte[] var4 = new byte[var3];
    int retval = myfunc(var1, var2, ref var3, var4);
    Array.Resize(ref var4, var3);
    

    The way that the native code works is that it expects the calling code to allocate the buffer, var4. The calling code says how long the buffer is in var3.

    Then when the native code returns, var3 contains the actual length of the buffer. Or perhaps the number of bytes that were copied. The former is more useful. Assuming the former then you could perhaps write it like this:

    string var1 = "...";
    uint var2 = ...;
    ushort var3 = 0;
    int retval = myfunc(var1, var2, ref var3, null);
    byte[] var4 = new byte[var3];
    int retval = myfunc(var1, var2, ref var3, var4);
    

    You should be checking return values etc.

    I can’t be 100% sure exactly how to call the function because there’s no enough information in your question. At least the p/invoke signature now matches your C++ code.

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

Sidebar

Related Questions

I am trying to use the function PathCanonicalize using DllImport (PInvoke) on a 64
I am trying to use some pinvoke code to call a C function. The
Using .NET (if I have to use pInvoke or managed C++, that's fine too),
How would I use DLLImport pinvoke to invoke a function i wrote in a
I want to use System.Numerics.Complex in an unmanaged PInvoke scenario. Using ILSpy, I noticed
Quick question. I have an app that use a native DLL through PInvoke, this
I'm using a native DLL. I'm not sure, but I think I can't use
I am using C# in Mono and I'm trying to use pinvoke to call
Is it possible to build custom mscorlib.dll without any use of native code (pinvoke
I am calling a DLL using PInvoke. The DLL's function returns a C string

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.