Win32 api call via C# fails!
        Posted  
        
            by 
                user434186
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by user434186
        
        
        
        Published on 2011-01-03T11:38:21Z
        Indexed on 
            2011/01/03
            13:53 UTC
        
        
        Read the original article
        Hit count: 375
        
Hi. I have a C++ function exported as api like this:
#define WIN322_API __declspec(dllexport)
WIN322_API char* Test(LPSTR str);
WIN322_API char* Test(LPSTR str)
{
 return "hello";
}
the function is exported as API correctly by the .DEF file, cause i can see it in Dependency Walker tool. Now i have a C# tester program:
[DllImport("c:\\win322.dll")]
public static extern string Test([MarshalAs(UnmanagedType.LPStr)] String str);
private void Form1_Load(object sender, EventArgs e)
        {
string _str = "0221";
Test(_str); // runtime error here!
}
on calling the Test() method i get the error:
"A call to PInvoke function 'MyClient!MyClient.Form1::Test' 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."
i tried many other data types and marshalings, but got nothing! plz help me!
© Stack Overflow or respective owner