473,803 Members | 3,431 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Marshalling a Callback with BOOL parameter

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(dlle xport) __stdcall
BoolCallback(Bo olCallBack lpBoolCallBack, BOOL b)
{
return lpBoolCallBack( b);
}

// c#
private delegate bool BoolCallbackDel egate(bool b);

[DllImport("Unma nagedDll")]
private static extern bool BoolCallback(Bo olCallbackDeleg ate
lpBoolCallBack, bool b);

private static bool BoolCallbackDel gate(bool b)
{
return b;
}

static void Main(string[] args)
{
bool b = BoolCallback(Bo olCallbackDelga te, true);
Debug.Assert(b == true);

b = BoolCallback(Bo olCallbackDelga te, 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

Nov 16 '06 #1
3 6326


"markb" <ma************ *@gmail.comwrot e in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
| 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(dlle xport) __stdcall
| BoolCallback(Bo olCallBack lpBoolCallBack, BOOL b)
| {
| return lpBoolCallBack( b);
| }
|
| // c#
| private delegate bool BoolCallbackDel egate(bool b);
|
| [DllImport("Unma nagedDll")]
| private static extern bool BoolCallback(Bo olCallbackDeleg ate
| lpBoolCallBack, bool b);
|
| private static bool BoolCallbackDel gate(bool b)
| {
| return b;
| }
|
| static void Main(string[] args)
| {
| bool b = BoolCallback(Bo olCallbackDelga te, true);
| Debug.Assert(b == true);
|
| b = BoolCallback(Bo olCallbackDelga te, 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
|
A BOOL is a typedef'd DWORD in C or an int, while a 'bool' is a single byte
in C# (and C++)
So you need to declare the return as int or you need to tag the return like
this:

[DllImport("Unma nagedDll")]
[return: MarshalAs(Unman agedType.Bool)]
private static extern bool BoolCallback(Bo olCallbackDeleg ate

Willy.
Nov 16 '06 #2
Willy Denoyette [MVP] wrote:
"markb" <ma************ *@gmail.comwrot e in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
| 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(dlle xport) __stdcall
| BoolCallback(Bo olCallBack lpBoolCallBack, BOOL b)
| {
| return lpBoolCallBack( b);
| }
|
| // c#
| private delegate bool BoolCallbackDel egate(bool b);
|
| [DllImport("Unma nagedDll")]
| private static extern bool BoolCallback(Bo olCallbackDeleg ate
| lpBoolCallBack, bool b);
|
| private static bool BoolCallbackDel gate(bool b)
| {
| return b;
| }
|
| static void Main(string[] args)
| {
| bool b = BoolCallback(Bo olCallbackDelga te, true);
| Debug.Assert(b == true);
|
| b = BoolCallback(Bo olCallbackDelga te, 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
|
A BOOL is a typedef'd DWORD in C or an int, while a 'bool' is a single byte
in C# (and C++)
So you need to declare the return as int or you need to tag the return like
this:

[DllImport("Unma nagedDll")]
[return: MarshalAs(Unman agedType.Bool)]
private static extern bool BoolCallback(Bo olCallbackDeleg ate

Willy.
Thanks for your reply Willy

Perhaps my example was unclear. The problem does not lie with passing
the bool to the unmanaged dll nor does it lie with with the return from
the unmanaged dll, it lies with the unmanaged dll calling a managed
callback with a BOOL as a parameter. (FYI it doesn't matter if you use
a c++ bool, a char, DWORD or whatever - the problem is still the same)

If you step through the code, the unmanaged dll, cleary calls the
callback with TRUE but it appears as false in the csharp code. Even
changing the delegate prototype to the following does not solve the
problem:

private static extern bool BoolCallback(Bo olCallbackDeleg ate
lpBoolCallBack,[MarshalAs(Unman agedType.Bool)]bool b);

The c++ clr version (below) of my csharp example works so it looks like
a problem with csharp?!?

ref class Program
{
private:
delegate bool BoolCallbackDel gate(bool b);

static bool GetBool(bool b)
{
return b;
}

public:
static void Main()
{
BoolCallbackDel gate^ fp = gcnew BoolCallbackDel gate(GetBool);
GCHandle gch = GCHandle::Alloc (fp);
IntPtr ip = Marshal::GetFun ctionPointerFor Delegate(fp);
BoolCallBack cb = static_cast<Boo lCallBack>(ip.T oPointer());
bool b = BoolCallback(cb , true);
Debug::Assert(b == true);

b = BoolCallback(cb , false);
Debug::Assert(b == false);
}
};

Mark

Nov 17 '06 #3
"markb" <ma************ *@gmail.comwrot e in message
news:11******** *************@j 44g2000cwa.goog legroups.com...
Willy Denoyette [MVP] wrote:
>"markb" <ma************ *@gmail.comwrot e in message
news:11******* *************** @m73g2000cwd.go oglegroups.com. ..
| 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(dlle xport) __stdcall
| BoolCallback(Bo olCallBack lpBoolCallBack, BOOL b)
| {
| return lpBoolCallBack( b);
| }
|
| // c#
| private delegate bool BoolCallbackDel egate(bool b);
|
| [DllImport("Unma nagedDll")]
| private static extern bool BoolCallback(Bo olCallbackDeleg ate
| lpBoolCallBack, bool b);
|
| private static bool BoolCallbackDel gate(bool b)
| {
| return b;
| }
|
| static void Main(string[] args)
| {
| bool b = BoolCallback(Bo olCallbackDelga te, true);
| Debug.Assert(b == true);
|
| b = BoolCallback(Bo olCallbackDelga te, 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
|
A BOOL is a typedef'd DWORD in C or an int, while a 'bool' is a single byte
in C# (and C++)
So you need to declare the return as int or you need to tag the return like
this:

[DllImport("Unma nagedDll")]
[return: MarshalAs(Unman agedType.Bool)]
private static extern bool BoolCallback(Bo olCallbackDeleg ate

Willy.

Thanks for your reply Willy

Perhaps my example was unclear. The problem does not lie with passing
the bool to the unmanaged dll nor does it lie with with the return from
the unmanaged dll, it lies with the unmanaged dll calling a managed
callback with a BOOL as a parameter. (FYI it doesn't matter if you use
a c++ bool, a char, DWORD or whatever - the problem is still the same)

If you step through the code, the unmanaged dll, cleary calls the
callback with TRUE but it appears as false in the csharp code. Even
changing the delegate prototype to the following does not solve the
problem:

private static extern bool BoolCallback(Bo olCallbackDeleg ate
lpBoolCallBack,[MarshalAs(Unman agedType.Bool)]bool b);

The c++ clr version (below) of my csharp example works so it looks like
a problem with csharp?!?

ref class Program
{
private:
delegate bool BoolCallbackDel gate(bool b);

static bool GetBool(bool b)
{
return b;
}

public:
static void Main()
{
BoolCallbackDel gate^ fp = gcnew BoolCallbackDel gate(GetBool);
GCHandle gch = GCHandle::Alloc (fp);
IntPtr ip = Marshal::GetFun ctionPointerFor Delegate(fp);
BoolCallBack cb = static_cast<Boo lCallBack>(ip.T oPointer());
bool b = BoolCallback(cb , true);
Debug::Assert(b == true);

b = BoolCallback(cb , false);
Debug::Assert(b == false);
}
};

Mark

typedef bool (__stdcall *BoolCallBack)( short b);
should be:
typedef bool (__stdcall *BoolCallBack)( BOOL b);

Willy.

Nov 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
10871
by: prettysmurfed | last post by:
Hi all I have a bit of a problem, the subject of this post is almost selfexplaing. But here goes: Heres an example of the code I want to implement, its all nice and simple, but the flaw is I can't seem to get the adress of the member function stated properly in the DialogBox function. I thought it was enough to write it as Dialog::DlgProc, if it was a normal situation and DlgProc was a normal function (not member of anything) then you...
2
1796
by: MR | last post by:
help! I have an unmanaged DLL that I do not have the source code, so i can't recompile or make changes. the DLL requires a callback function. I would like to implement the callback method in a managed class. The unmanaged DLL gets the address of the callback in a struct (someStruct below) that is passed as a parameter. I can't seem to figure out how to pass the "unmanaged" address of a managed method to be uses as a callback. I would...
12
3684
by: Darwin Lalo | last post by:
I have a lot of code like this: VOID CALLBACK TimerRoutine(PVOID lpParam) { long nTaskid = (long)lpParam; GObj *obj; if( mapThreadSafe.find( nTaskid, obj )) // mapThreadSafe is a hash_map, thread safe {
4
2828
by: Kyku | last post by:
Hello all, I'm in a process of writing a small Linux C++ program for discovering repeaded files over a filesystem. One part of this task is traversing a directory structure. This is done by the following recursive function. Since I'd like it to be general enough to handle function pointers and function objects I've made it a template. template <typename CallBack> void Traverse(const std::string& path, CallBack& callback) throw
3
3070
by: ryan.mitchley | last post by:
Hi all I have a class (cPort) that is designed to receive objects and, depending on the type, call a handler (callback) in any descendant of a cProcessBlock class. Callback functions take a shared_ptr<cBaseas a parameter, and return void. The code was working fine, although I have encountered problems (under a Microsoft compiler, of course - VC 8.0) when I attempt to add callbacks to a class with multiple inheritance. I hate multiple
2
5649
by: Roshan | last post by:
Hi List, I am trying to call a method from a native DLL which takes a PWSTR as an input param. From my managed code (C#), I want to pass a string as a parameter. How do I convert a C# string to a PWSTR? Currently I am using public static extern bool SomeMethod(IntPtr fileName); //PWSTR
2
7363
by: MyCrystalGift | last post by:
Hi, I have an old C++ GUI Application CPPAPP.exe that calls a C DLL library RULE.DLL through a C++ class wrapper LoadRule.CPP. Now I need to call the C DLL RULE.DLL from C# GUI application CSAPP.exe. I need help to convert LoadRule.CPP to a C++ BridgeDLL, and I need help on how to convert the call to the BridgeDLL from C#.
4
2440
by: Beorne | last post by:
I have to call a c++ library funtion with the following signature: void ReadBool(bool* pb1) unsafe static extern void NicamPLCRead(out bool); but this does not work, the bool (not initialized) is not correctly returned.
10
7001
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK MyEnumWindowsProc(HWND hwnd, LPARAM lparam) {
0
10548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.