A
Andrew Falanga
Hello,
I have a solution that's purpose is to ultimately build a C# class
library for other programs to use. The C# class library must make use
of two unmanaged dlls being build in the same solution. I have post
build events for those two projects which copy the dll into the
%windows%/system32 directory.
Then using platform invoke I bring in the appropriate functions from
the unmanaged libraries using DllImport. I do this thusly:
[DllImport("Comparison.dll", CharSet = CharSet.Ansi)]
private static extern bool CheckStrings(String arg1, String arg2);
The in the code I check the return value for true or false:
bool returnVal;
if(returnVal = CheckStrings("String 1", "String 2"))
Console.WriteLine("the return was true");
else
Console.WriteLine("the return was false");
Since the dlls are being built in my solution I've turned on the
ability to debug unmanaged code. Here's where things get dicey. I
set a break point at the point where TheFunction is called and then
step into that function. I step my way through that function and then
hover over the return value variable before executing that line of
code. The return value is "false." Then, back in managed space, I
hover over "returnVal" and to my astonishment it's "true."
I can see using the debugger that the unmanaged code is going to
return "false" but then in managed space the value "true" gets
assigned. The only idea I've come up with at this point is that I'm
not marshaling something correctly. I do know that a bool in C# is 4
bytes whereas a bool in C++ is one byte. I'm wondering about this
being the issue. I've not been able to find any reasonable help in
MSDN as yet. The links I have found do talk about marshaling data, in
fact I had to "decorate" some values in structures used by these
functions with the
[MarshalAs(Unmanaged.uint8)
bool thisBool;
However, I've not found anything about how to marshal return values.
Am I on the right path with this? This just doesn't make sense to me.
If there isn't a means of marshaling a functions return value
correctly, would I want to redefine the function to return an int and
then cast the boolean value to a 4-byte wide data type?
Thanks for any help,
Andy
I have a solution that's purpose is to ultimately build a C# class
library for other programs to use. The C# class library must make use
of two unmanaged dlls being build in the same solution. I have post
build events for those two projects which copy the dll into the
%windows%/system32 directory.
Then using platform invoke I bring in the appropriate functions from
the unmanaged libraries using DllImport. I do this thusly:
[DllImport("Comparison.dll", CharSet = CharSet.Ansi)]
private static extern bool CheckStrings(String arg1, String arg2);
The in the code I check the return value for true or false:
bool returnVal;
if(returnVal = CheckStrings("String 1", "String 2"))
Console.WriteLine("the return was true");
else
Console.WriteLine("the return was false");
Since the dlls are being built in my solution I've turned on the
ability to debug unmanaged code. Here's where things get dicey. I
set a break point at the point where TheFunction is called and then
step into that function. I step my way through that function and then
hover over the return value variable before executing that line of
code. The return value is "false." Then, back in managed space, I
hover over "returnVal" and to my astonishment it's "true."
I can see using the debugger that the unmanaged code is going to
return "false" but then in managed space the value "true" gets
assigned. The only idea I've come up with at this point is that I'm
not marshaling something correctly. I do know that a bool in C# is 4
bytes whereas a bool in C++ is one byte. I'm wondering about this
being the issue. I've not been able to find any reasonable help in
MSDN as yet. The links I have found do talk about marshaling data, in
fact I had to "decorate" some values in structures used by these
functions with the
[MarshalAs(Unmanaged.uint8)
bool thisBool;
However, I've not found anything about how to marshal return values.
Am I on the right path with this? This just doesn't make sense to me.
If there isn't a means of marshaling a functions return value
correctly, would I want to redefine the function to return an int and
then cast the boolean value to a 4-byte wide data type?
Thanks for any help,
Andy