-
April 25th, 2012, 10:27 AM
#1
dll import
Hi all,
I have a c++ dll I have to call from c# using dll import but have no clue how to accomplish it. the c++ entry point is __declspec( dllexport ) UInt8 Update(unsigned char bForceUpdate). I am using this as a dll import
[DllImport("XbeeFW.dll", EntryPoint = "Update")]
public static extern bool fw(string c);
and call it as fw("TRUE") but get this error "A call to PInvoke function 'Puck Programmer!Puck.Main::fw' 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."
Can some one please help a newbie get this to work?
TIA,
ric32br.
-
April 25th, 2012, 12:50 PM
#2
Re: dll import
uint8 is an unsigned 8 bit integer, which in C# is a 'byte'. Unsigned char is the same as uint8 in this scenario, so the correct p/invoke signature is:
[DllImport("XbeeFW.dll", EntryPoint = "Update")]
public static extern byte fw(byte c);
There are alternative ways of implementing this, but this is the simplest and most direct. I assume a value of '0' is interpreted as false and anything else is true. Therefore you'd invoke it as:
Code:
byte result = fw (1); // passes 'true'
if (result == 0)
Console.WriteLine ("Failure!");
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
April 26th, 2012, 12:27 PM
#3
Re: dll import
You may also be running into a calling convention problem.
By default, your C++ apps will use the cdecl calling convention. However, C# defaults stdcall. So, check first, but if they aren't matched up you will need to change your native project or specify the calling convention in the C# signature:
Code:
[DllImport("XbeeFW.dll", EntryPoint = "Update", CallingConvention=CallingConvention.Cdecl)]
Originally Posted by Mutant_Fruit
uint8 is an unsigned 8 bit integer, which in C# is a 'byte'. Unsigned char is the same as uint8 in this scenario, so the correct p/invoke signature is...
To be pedantic, sizeof(char) is guaranteed to be 1 by the standard, but it is not guaranteed to be 8 bits. So, uint8 is not necessarily the same as unsigned char on all platforms You are of course correct about the signature.
Last edited by BigEd781; April 26th, 2012 at 12:29 PM.
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
-
April 27th, 2012, 07:52 AM
#4
Re: dll import
Thanks, that did the trick.
ric32br.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|