A call to PInvoke function 'ABC' has unbalanced the stack. - pinvoke

I have a C# application (x86 .net 3.5 on Windows 7 vs2010) which call a C function
when I upgrade it to .net 4, I got the following message:
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.
C signature:
BOOL ABC( UDF_HANDLE handle, char* pQualifiedName )
//#define UDF_HANDLE void*
C# side:
[SecuritySafeCritical]
[DllImport(dllPath, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, ThrowOnUnmappableChar = true)]
internal static extern int ABC(
[In] IntPtr handle,
[MarshalAs(UnmanagedType.LPStr)] string qualifiedName);
Any help appreciated.

I redefined ABC first parameter to be same as the first parameter in the function.

The C++ code uses the cdecl calling convention. But you've specified stdcall in your p/invoke. Change that in the C# code and your two declarations will match.

Related

Object Marshaling for C++/CLI Managed to Unmanaged Call

I'm writing a C++/CLI extension, and am having issues getting the typing to work out. I'd appreciate your help and hope that I'm making a simple mistake with casting or passing parameters.
Firstly, here is the definition of the unmanaged function I'm trying to call (and which I can't change):
int getResponse(const RequestObject& requestObj, ResponseObject& responseObj);
Second, my unmanaged C++ RequestObject has a definition like so (which I also can't change):
class RequestObject
{
public:
RequestObject();
void addElement(int value, int age);
}
Now, in my managed C++/CLI code (using the 'IJW' interop ability), I want to call into this function. You'll note that I have unmanagedClassInstance (which holds the method given by the above getResponse function definition), and ManagedRequestObject reqs (which is just the managed version of what I want to put into the RequestObject).
RequestObject* unRequest = new RequestObject();
// Here I'm taking things from a managed version of RequestObject and making
// the unmanaged instances:
for each (ManagedRequestObject^ req in reqs) {
unRequest->addElement(marshal_as<int>(req->getValue()),
marshal_as<int>(req->getAge()));
}
// Call into the unmanaged object to get the request processed:
ResponseObject* unResponse = new ResponseObject();
int result = unmanagedClassInstance->getResponse(unRequest, unResponse);
Can you please help me understand how to correctly pass unRequest and unResponse into the getResponse() function?
If you keep using pointers for your unmanaged objects (which you might want to reconsiderate), you should be able to call the unmanaged function like this:
int result = unmanagedClassInstance->getResponse(*unRequest, *unResponse);
But unless strictly necessary, I'd suggest you do this instead:
RequestObject unRequest;
// Here I'm taking things from a managed version of RequestObject and making
// the unmanaged instances:
for each (ManagedRequestObject^ req in reqs) {
unRequest.addElement(marshal_as<int>(req->getValue()),
marshal_as<int>(req->getAge()));
}
// Call into the unmanaged object to get the request processed:
ResponseObject unResponse;
int result = unmanagedClassInstance->getResponse(unRequest, unResponse);

Only first character is passed in pinvoke string

I have a c++ dll that I am using using Pinvoke.
The method signature is as follow:
c++:
EXTERN_C BOOL std_call MyCppMethod(LPCSTR param1, LPCSTR param2);
C#:
[DllImport("MyDll.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool MyCppMethod(
/*[MarshalAs(UnmanagedType.LPWStr)]*/ string param1,
/*[MarshalAs(UnmanagedType.LPWStr)]*/ string param2
);
for technical reasons I replaced the dll with a lib, and wrapped the lib with a dll that uses EXPORTS.def file and EXPORTS declaration to export the method from the lib.
Since I have dine that I see a weird behavior. instead of getting the string in the method implementation I get only the first character.
I had tried to replace the calling convention, to use marshalAs LPCWSTR, and also tried to replace the lpcstr in the c++ decleration with char*.
non of the above helped me to solve the problem and I still get only the first character.
Why this is happening and how can I solve this problem?
LPCSTR is a pointer to null-terminated array of char, and UnmanagedType.LPWStr is a pointer to null-terminated array of wchar_t. So there is a simple mis-match.
The reason why you only received a single character is that ASCII characters, when represented as two byte UTF-16 characters, have the ASCII value in one byte, and a zero in the other byte. When interpreted as null-terminated array of char, this is a string of length one. The zero byte is interpreted as the null-terminator.
You can fix it by changing either:
LPCSTR to LPCWSTR on the native side, or
UnmanagedType.LPWStr to UnmanagedType.LPStr on the managed side.
Frankly, to me it makes more sense to use Unicode these days so I would go for option 1. Indeed, since you specified CharSet.Unicode then there is no need for the MarshalAs at all. The code would look like this:
C++
BOOL std_call MyCppMethod(LPCWSTR param1, LPCWSTR param2);
C#
[DllImport("MyDll.dll", CharSet = CharSet.Unicode)]
public static extern bool MyCppMethod(string param1, string param2);
Note that I am also sceptical of your setting SetLastError to true. Does your function really call SetLastError?

Dumpbin shows strange method name (generating exporting function in MS Visual C++)

I have created new Win32 project in my VS and have selected Dynamic Library ( *.dll ) for this aim.
I have defined some exporting function in the main file:
__declspec(dllexport)
int TestCall(void)
{
int value = 4 / 2;
std::cout << typeid(value).name() << std::endl;
return value;
}
__declspec(dllexport)
void SwapMe(int *first, int *second)
{
int tmp = *first;
*first = *second;
*second = tmp;
}
When I've looked at the dumpin /exports, I've got:
ordinal hint RVA name
1 0 00001010 ?SwapMe##YAXPEAH0#Z
2 1 00001270 ?TestCall##YAHXZ
I'm calling in the C# version like this:
[DllImport(#"lib1.dll", EntryPoint = "?TestCall##YAHXZ",
CallingConvention = CallingConvention.Cdecl)]
static extern int TestCall();
It's not the correct form of using exported methods. Where did I fail with generating such names for exporting-methods in the C++ dll project?
This is normal, the C++ compiler applies name decoration to functions. The C++ language supports function overloading, much like C# does. So you could write a Foo(int) and a Foo(double) function. Clearly they cannot both be exported as a function named "Foo", the client code wouldn't know which one to call. So the extra characters encode the name so that it is unique for the overload.
You can turn that off by declaring the function extern "C", the C language doesn't support overloading so doesn't require the same kind of decoration.
But it is actually better if you don't. Because it is also an excellent way to catch mistakes. Like changing the function declaration in your C++ code but forgetting to modify the pinvoke declaration in your C# code. You will now get an easy to diagnose "Entrypoint not found" exception instead of a non-descriptive and very hard to diagnose AccessViolationException. Which doesn't necessarily have to be raised in the C++ code, the stack imbalance can also crash your C# code. Looking up the decorated name is a bit painful however, improve that by asking the linker to create a map file (/MAP option).
use extern "C" to specify the linkage to avoid the name mangling:
extern "C" __declspec(dllexport) int TestCall(void);
extern "C" __declspec(dllexport) void SwapMe(int *first, int *second);

What does the C++/CLI Object^% (caret percent-sign) declaration mean?

This apparently is a Google-proof term since I can't get any search engines to not throw away the "extra" characters. I did also look on MSDN in the C++ reference but I can't seem to find the C++/CLI reference because there is nothing in the declarations section on it.
It means "pass by reference":
void bar::foo(Object^% arg) {
arg = gcnew Object; // Callers argument gets updated
}
Same thing in C++:
void foo(Object** arg) {
*arg = new Object;
}
or C#:
void foo(out object arg) {
arg = new Object();
}
C++/CLI doesn't distinguish between ref and out, it does not have the definite assignment checking feature that the C# language has so no need to distinguish between the two. Same in VB.NET, ByRef vs ByVal.
% is a tracking reference.
It is similar to a native reference (Object&), but a tracking reference can reference a CLR object while a native reference cannot. The distinction is necessary because the garbage collector can move CLR objects around, so a CLR-object's memory address may change.
The ^ is a handle. This simply means it is managed. See MSDN and also this SO post.
Essentially, it's the "managed" version of Object*&, and equivalent to ref or out on a reference type in C#.
This is a managed pointer by reference. So if you had something like:
void DoSomething(System::String^% stringObject)
in C# it would look like:
void DoSomething(ref System.String stringObject)
This is a C++/CLI Tracking Reference. This is kind of like a C++ reference, but to a managed object.

C++/CLI managed wrapper around C static library

Help!
I'm totally exhausted/frustrated with what seems to be a reasonably easy task. I’m not sure what I'm doing wrong; let alone if I'm doing it correct. I'm "required" to use an existing library (a C static library – over 100,000 lines of straight C code) in developing a WPF application (VS 2010, C# 4.0). Oh, and I can't touch the existing C code - use it as is!
I've read so many postings (advanced topics, how-to, etc), yet I'm so new to C++/CLI that it's just not making sense. From what I've read the best approach is to wrap the C static library as follows:
Unmanaged C static library <---> C++/CLI managed wrapper DLL <--->
managed WPF application
This is the stripped down C header file:
/* Call this function to execute a command. */
int issue_command(int command, long param1, long param2);
/* Completion call back function; you must supply a definition. */
extern int command_completed(int command, long param1, long param2);
struct struct_command_str
{
char command_str[10];
char param1_st[2];
char param2_st[2];
char success;
};
/* You must supply definitions to the following extern items. */
extern int command_status;
extern struct struct_command_str command_str;
The problem(s):
What I can’t seem to do correctly is provide a C++/CLI implementation for the call back functions, and the two extern items (command_status and struct command_str).
Can someone provide a sample C++/CLI implementation for the above missing call back functions and externs?
Thanks in advance for your assistance.
in your C++/CLI managed wrapper project, add 2 files :
a .c file :
extern void doSomething();
int command_status = 0;
struct_command_str command_str = { "command1", "p1", "p2", 't' };
int command_completed(int command, long param1, long param2) {
...
command_status = 1;
...
doSomething();
...
command_status = 2;
...
return 3;
}
a cpp file
void doSomethingManagedWrapper() {
...
call managed code
...
}
void doSomething() {
doSomethingManagedWrapper();
}
when you implement these in your c++/cli module, use the same signature shown in the c header file,but prefixed with extern "C".
also put an extern "C" block around the #include of the C header file.