473,395 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem with C++ DLL returning bool

Hi there,

we have this C++ DLL we wrote some time ago to do some Win32 native
stuff for us.

The function we're calling in the DLL returns a boolean value (type
bool) indicating success of the operation.

We declare the import in C# like follows:

DllImport("xyz.dll")]
private static extern bool DoSomething(...);

We call the function like this:

bool result = DoSomething(...);

Now: we're using this in Visual Studio 2008. When we set the target
framework version to 2.0 everything works fine - the function returns
false, the result-variable has the value false.

As soon as we switch the target framework version to 3.5 (no other
changes to the project), the C++ function still returns false, but the
result-variable is set to true!

How could that be?

As a workaround we've change the result type from bool to int, returning
defined values for success or failure, but this is not very satisfactory...

Any advice?

Thanks
Thorsten
Mar 20 '08 #1
1 1821
"Thorsten Dittmar" <td******@altenbrand.dewrote in message
news:O1**************@TK2MSFTNGP03.phx.gbl...
Hi there,

we have this C++ DLL we wrote some time ago to do some Win32 native stuff
for us.

The function we're calling in the DLL returns a boolean value (type bool)
indicating success of the operation.

We declare the import in C# like follows:

DllImport("xyz.dll")]
private static extern bool DoSomething(...);

We call the function like this:

bool result = DoSomething(...);

Now: we're using this in Visual Studio 2008. When we set the target
framework version to 2.0 everything works fine - the function returns
false, the result-variable has the value false.

As soon as we switch the target framework version to 3.5 (no other changes
to the project), the C++ function still returns false, but the
result-variable is set to true!

How could that be?

As a workaround we've change the result type from bool to int, returning
defined values for success or failure, but this is not very
satisfactory...

Any advice?

Thanks
Thorsten

If you are sure that the C function returns a bool, and not a BOOL, then it
should work, else you should marshal the return value using:

[return: MarshalAs(UnmanagedType.Bool)]
Willy.
Mar 20 '08 #2

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

Similar topics

2
by: Philippe F. Bertrand | last post by:
Summary - What should a C# bool map to in C++? Are return types different? I have lots of methods declared with bool parameters and bool return type which appear to work without any problems...
0
by: John Constant | last post by:
Using the MS C++ Example http://support.microsoft.com/default.aspx?scid=kb;en-us;261003 I've successfully managed to trap and log Script Errors that are generated by the WebBrowser (IE 6) which is...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.