M
markb
Hi
My C# app is being called from a callback from an unmanaged DLL. One of
the parameters of the callback is of type BOOL. I am using PInvoke to
marshal this to a (managed) bool. The problem is that no matter if we
pass TRUE or FALSE, the bool is always marshalled as true.
// unmanaged code in dll
typedef bool (__stdcall *BoolCallBack)(short b);
extern "C" BOOL __declspec(dllexport) __stdcall
BoolCallback(BoolCallBack lpBoolCallBack, BOOL b)
{
return lpBoolCallBack(b);
}
// c#
private delegate bool BoolCallbackDelegate(bool b);
[DllImport("UnmanagedDll")]
private static extern bool BoolCallback(BoolCallbackDelegate
lpBoolCallBack, bool b);
private static bool BoolCallbackDelgate(bool b)
{
return b;
}
static void Main(string[] args)
{
bool b = BoolCallback(BoolCallbackDelgate, true);
Debug.Assert(b == true);
b = BoolCallback(BoolCallbackDelgate, false);
Debug.Assert(b == false);
}
The second Assert alwasys asserts because the callback is returning
true when it should be false.
I think that this is a bug somewhere in c#/clr/PInkove because this
doesnt happen in c++ clr. I think it was fixed for c++ clr under this
kb article http://support.microsoft.com/default.aspx?kbid=823071 but
not fixed for c#.
Any thoughts?
Mark
My C# app is being called from a callback from an unmanaged DLL. One of
the parameters of the callback is of type BOOL. I am using PInvoke to
marshal this to a (managed) bool. The problem is that no matter if we
pass TRUE or FALSE, the bool is always marshalled as true.
// unmanaged code in dll
typedef bool (__stdcall *BoolCallBack)(short b);
extern "C" BOOL __declspec(dllexport) __stdcall
BoolCallback(BoolCallBack lpBoolCallBack, BOOL b)
{
return lpBoolCallBack(b);
}
// c#
private delegate bool BoolCallbackDelegate(bool b);
[DllImport("UnmanagedDll")]
private static extern bool BoolCallback(BoolCallbackDelegate
lpBoolCallBack, bool b);
private static bool BoolCallbackDelgate(bool b)
{
return b;
}
static void Main(string[] args)
{
bool b = BoolCallback(BoolCallbackDelgate, true);
Debug.Assert(b == true);
b = BoolCallback(BoolCallbackDelgate, false);
Debug.Assert(b == false);
}
The second Assert alwasys asserts because the callback is returning
true when it should be false.
I think that this is a bug somewhere in c#/clr/PInkove because this
doesnt happen in c++ clr. I think it was fixed for c++ clr under this
kb article http://support.microsoft.com/default.aspx?kbid=823071 but
not fixed for c#.
Any thoughts?
Mark