Mangled name even after extern c - c#

I have included a C++ library in my C# project and i am calling one of it's method.
Earlier I was having the mangling problem then read about extern c and applied it to C++ method.
Then tried calling it like below:
[DllImport(#"F:\bin\APIClient.dll")]
public static extern IntPtr logIn2(IntPtr a, IntPtr b, IntPtr c, IntPtr d, IntPtr e, IntPtr f, int g);
But still I am getting Entry Point exception.
C++:
APICLIENT_API char* logIn2(const char* a, const char* b,const char* c,const char* d,const char* e,const char* f, int g);
And if i use entryPoint in DLLImport then it works fine:
[DllImport(#"F:\bin\APIClient.dll", EntryPoint = "?logIn2#CAPIClient#API##QAEPADPBD00000H#Z", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr logIn2(IntPtr a, IntPtr b, IntPtr c, IntPtr d, IntPtr e, IntPtr f, int g);
Why even after using extern c I have to give this Entry point to make things working.

The decorated C++ name is not a problem. It is actually very desirable, it automatically saves you from having to diagnose a very difficult runtime crash when the C++ code is changed and the function signature is altered. Since there now will be a mismatch and you get an easy "Procedure not found" error message instead of a corrupted call stack that is quite undiagnosable.
The much bigger problem, and the reason that extern "C" doesn't work, is that this is an instance method of the CAPIClient class. It uses the __thiscall calling convention, required to pass a valid this pointer. You can't get that from pinvoke, it requires allocating memory for the C++ object and calling the CAPIClient constructor. Only a C++ compiler knows how to do that correctly, only it knows the correct amount of memory to allocate. So you can't pinvoke, you have to write a C++/CLI wrapper.
The normal mishap when you pinvoke an instance method of a C++ class is a hard crash, typically reported as an AccessViolationException. Triggered when the instance method tries to access another other instance member of the C++ class through the invalid this pointer. Only a static C++ function can be correctly pinvoked. Since you didn't seem to have triggered an exception (yet), there's some hint that the function should have been static in the first place.

Related

FatalExecutionEngineError during DllImport

Here's the line I get the execution error:
NimbieImportedFunctions.BS_Robots_Connect(
out robotCount, out robotIDs, out robotTypes, out robotReadys);
And the DllImport itself:
[DllImport("BSRobots20.DLL", CallingConvention = CallingConvention.Cdecl)]
public static extern UInt32 BS_Robots_Connect(
out int nRobotCount,
out int[] pnRobotIDS,
out int[] pnRobotTypes,
out bool[] pbRobotReadys);
And from the header file:
extern "C" __declspec(dllexport) DWORD BS_Robots_Connect(
int *nRobotCount,
int *pnRobotID,
int *pnRobotType,
bool *pnRobotReady
);
//
// Description:
// Function to connect all online robots.
//
// Parameters:
// nRobotCount [out] Pointer to an integer that receives the number of robots.
// pnRobotID [out] Pointer to an array of integer that receives the robot IDs.
// pnRobotType [out] Pointer to an array of integer that receives the robot types.
//
// Return:
// BS_ROBOTS_OK If the function succeeds with no error.
// BS_ROBOTS_ERROR If the function fails with any error occurred.
//
///////////////////////////////////////////////////////////////////////////////
And the error I get:
The runtime has encountered a fatal error. The address of the error was
at 0x72c4898e, on thread 0xdf0. 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.
Sometimes, I also get an AccessViolationException. I'm so incredibly unsure as to what's going on. Please help me SO!
Your p/invoke is incorrect. It should, perhaps, read:
[DllImport("BSRobots20.DLL", CallingConvention = CallingConvention.Cdecl)]
public static extern uint BS_Robots_Connect(
out int nRobotCount,
[Out] int[] pnRobotIDS,
[Out] int[] pnRobotTypes,
[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U1)]
[Out] bool[] pbRobotReadys
);
It is plausible that nRobotCount should be ref rather than out. Determine that based on whether or not you need to pass information into the function. And are you sure that pbRobotReadys is an array? If not then use out bool or ref bool.
So perhaps what you need is:
[DllImport("BSRobots20.DLL", CallingConvention = CallingConvention.Cdecl)]
public static extern uint BS_Robots_Connect(
ref int nRobotCount,
[Out] int[] pnRobotIDS,
[Out] int[] pnRobotTypes,
[MarshalAs(UnmanagedType.U1)]
out bool pbRobotReadys
);
To get to the bottom of this you'll need to study the documentation a little more, and perhaps refer to any C++ example code that you can find.
You will need to allocate arrays before calling the function. I cannot tell from here how exactly you need to allocate the arrays. Presumably you know how large they need to be.
Why was your version wrong? Well, the way to think about this is that a C# array is already a reference. By passing the array using out you are passing a pointer to a pointer. In other words, one level of indirection too far.
Another way to think about this is that you were asking the unmanaged code to create managed arrays. And that is something that it manifestly cannot do.

Accessing c++ dll library from c# [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
pinvokestackimbalance — how can I fix this or turn it off?
I need to access a c++ dll library (I don't have the source code) from c# code.
for example the following functions:
UINT32 myfunc1()
UINT32 myfunc2(IN char * var1)
UINT32 myfunc3(IN char * var1, OUT UINT32 * var2)
For myfunc1 I have no problems when I use the following code:
[DllImport("mydll.dll")]
public static extern int myfunc1();
On the other hand I was unable to use myfunc2 and myfunc3.
For myfunc2 I tried the following: (and many others desperately)
[DllImport("mydll.dll")]
public static extern int myfunc2(string var1);
[DllImport("mydll.dll")]
public static extern int myfunc2([MarshalAs(UnmanagedType.LPStr)] string var1);
[DllImport("mydll.dll")]
public static extern int myfunc2(char[] var1);
But all of them gave the following error:
"Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Users\....\myproject\bin\Debug\myproj.vshost.exe'.
Additional Information: A call to PInvoke function 'myproject!myproject.mydll::myfunc2' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."
Please, guide on what I should do.
Your C++ functions use the cdecl calling convention, but the default calling convention for DllImport is stdcall. This calling convention mismatch is the most common cause of the stack imbalanced MDA error.
You fix the problem by making the calling conventions match. The easiest way to do that is to change the C# code to specify cdecl like this:
[DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int myfunc2(string var1);
It might just be a character set mismatch try this
[DllImport("mydll.dll", CharSet = CharSet.Ansi)]
public static extern int SendText([MarshalAs(UnmanagedType.LPStr)] string var1);
Nicked from:
DLL import char * pointer from C#

Trying to marshal C++ pointer to unsigned long into a C# ulong

I have a C++ DLL that I am trying to interface with using C#. I do not have access to the DLL source code so it cannot be changed. One of the methods I need to call contains a pointer to an unsigned long:
int __stdcall Foo(FooHandle messageHandle, unsigned long* tag, char* part, char* value)
I have created a test DLL project with the same method signature so I can try to figure this out but I have not successfully been able to have the DLL change the value of the unsigned long that the pointer is referencing and get that value back on the C# side. The C# side declaration is:
[DllImport("Foo.dll", EntryPoint = "Foo", CallingConvention = CallingConvention.StdCall)]
public static extern int Foo(int messageHandle, IntPtr tag, StringBuilder part, StringBuilder value);
When I make the call from C# and then try to convert the IntPtr to Int32 or Int64 I get a zero. I have searched on here and the net in general as well as MSDN and can't seem to find an explaination of how to do this. I figured the char* would be the hard part but the StringBuilder just works.
How can you use a C++ DLL that changes the value of an unsigned long that is passed by pointer?
Any help would be appreciated. Thanks.
Just use a uint passed by ref:
public static extern int Foo(int messageHandle, ref uint tag, StringBuilder part, StringBuilder value);

Passing StringBuilder to PInvoke function

In one of the post Titled "Call a c++ method that returns a string, from c#"
Its said that , to make the following Pinvoke to work change the C++ signature to as
extern "C" REGISTRATION_API void calculate(LPSTR msg)
C++ code
extern "C" REGISTRATION_API void calculate(char* msg)
C# code
[DllImport("thecpp.dll", CharSet=CharSet.Ansi)]
static extern void calculate(StringBuilder sMsg);
How can stringBuilder which is a class ,convertd to long ptr to string .(but this is the accepted answer)
Shouldnt we use use IntPtr as below ?
extern "C" REGISTRATION_API void calculate(Intptr msg)
Look for the section marked "Passing Strings", the marshaler has got some added smarts to do this trick.
http://msdn.microsoft.com/en-us/library/aa446536.aspx
To solve this problem (since many of
the Win32 APIs expect string buffers)
in the full .NET Framework, you can,
instead, pass a
System.Text.StringBuilder object; a
pointer will be passed by the
marshaler into the unmanaged function
that can be manipulated. The only
caveat is that the StringBuilder must
be allocated enough space for the
return value, or the text will
overflow, causing an exception to be
thrown by P/Invoke.

C# SendMessage to C++ WinProc

I need to send a string from C# to a C++ WindowProc. There are a number of related questions on SO related to this, but none of the answers have worked for me. Here's the situation:
PInvoke:
[DllImport("user32", CharSet = CharSet.Auto)]
public extern static int SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, string lParam);
C#:
string lparam = "abc";
NativeMethods.User32.SendMessage(handle, ConnectMsg, IntPtr.Zero, lparam);
C++:
API LRESULT CALLBACK HookProc (int code, WPARAM wParam, LPARAM lParam)
{
if (code >= 0)
{
CWPSTRUCT* cwp = (CWPSTRUCT*)lParam;
...
(LPWSTR)cwp->lParam <-- BadPtr
...
}
return ::CallNextHookEx(0, code, wParam, lParam);
}
I've tried a number of different things, Marshalling the string as LPStr, LPWStr, also tried creating an IntPtr from unmanaged memory, and writing to it with Marshal.WriteByte.
The pointer is the correct memory location on the C++ side, but the data isn't there. What am I missing?
For C++ LPWSTR or LPSTR parameters you need to use the C# StringBuilder in your DllImport.
For C++ LPCWSTR or LPCSTR parameters you need to use the C# string in your DllImport.
Make sure that your SendMessage call is occurring in the expected, synchronous manner and that your NativeMethods class maps the proper Win32 call (Send vs. PostMessage.) If this isn't correct it's possible that by the time your message is processed on the C++ end, you've left the scope of your C# method and any local variables created on the stack are gone resulting in your bad pointer.
Stack and heap considerations for cross-thread calls: Threads have their own stacks but share the heap. Stack-allocated variables in one thread will not be visible in another. The string type is an odd duck in .NET. It is an Object-derived, reference type but made to look and feel like a value type in code. So perhaps passing a pointer to heap-allocated data ought to work. That's where StringBuilder, as a heap-allocated reference type, comes in.

Categories