473,799 Members | 3,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

P/Invoke returning and passing bool's between C# and C++

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 but one method in
particular seems to always return true.

The method is declared in C# as

[DllImport("my.d ll")] private static extern
bool T_Cursor_GetBoo lean(
int key, short columnID, ErrorCode *code
);

And in C++ as

extern "C" {
__declspec(dlle xport) bool T_Cursor_GetBoo lean(
int natKey, short cid, int *code
);
}

__declspec(dlle xport) bool T_Cursor_GetBoo lean(
int natKey, short cid, int *code
)
{
bool out = some stuff;
return out;
}

Stepping through the code in Visual Studio on the desktop, I see the
unmanaged code returning false and the managed code receiving true.

I need this code to work on both the .NET framework and the
CompactFramewor k.

Is there a way to globally change the marshalling of bool's and will
this work in the compact framework?

- Thanks
Philippe
Nov 15 '05 #1
2 8523
Philippe,
Summary - What should a C# bool map to in C++? Are return types
different?


Depends on your marshaling settings.

The default is a 32-bit Win32 BOOL.
With [MarshalAs(Unama ngedType.Varian tBool)] you get a 16-bit
VARAIANT_BOOL.
With [MarshalAs(Unama ngedType.I1)] you get a one byte C++ bool (or
Win32 BOOLEAN).

So in your case, I'd try to change the declaration to

[DllImport("my.d ll")]
[return: MarshalAs(Unman agedType.I1)]
private static extern bool T_Cursor_GetBoo lean( ...
That's how it works on the desktop CLR anyway. I'm not familiar enough
with .NET CF to say how it works there.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
In the CF there's no COM interop, so it's always 32-bits. Use an int.

--
Chris Tacke, eMVP
Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net

"Philippe F. Bertrand" <pf***@yahoo.ca > wrote in message
news:dd******** *************** ***@posting.goo gle.com...
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 but one method in
particular seems to always return true.

The method is declared in C# as

[DllImport("my.d ll")] private static extern
bool T_Cursor_GetBoo lean(
int key, short columnID, ErrorCode *code
);

And in C++ as

extern "C" {
__declspec(dlle xport) bool T_Cursor_GetBoo lean(
int natKey, short cid, int *code
);
}

__declspec(dlle xport) bool T_Cursor_GetBoo lean(
int natKey, short cid, int *code
)
{
bool out = some stuff;
return out;
}

Stepping through the code in Visual Studio on the desktop, I see the
unmanaged code returning false and the managed code receiving true.

I need this code to work on both the .NET framework and the
CompactFramewor k.

Is there a way to globally change the marshalling of bool's and will
this work in the compact framework?

- Thanks
Philippe

Nov 15 '05 #3

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

Similar topics

2
1896
by: Neil Fedin via .NET 247 | last post by:
I am writing a testing application that uses reflection to open assemblies and call methods. I have a function that looks like this... public void Method1 (int IDArray, bool SomethingElse){ ... } When I prepare the parameters for the above function, I create and integer array and give it some values
4
1680
by: mchoya | last post by:
I'm so frustrated. I'm beginning school next week and I have been working on a simple program for several days now without being able to complete it. (Though I have brushed up on a lot of C++ while trying.) I realize this code is long but, I'm not sure of any other way to display the issue. The header file and ?implementation file work. (This assignment was building on another in which I created the header and implementation with a...
5
3111
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the function below is returning a pointer to pointers who are GUID. I am trying to write a wrapper to use in my VB code and what I would prefer to do is be able to return an array of GUID. I remember (not sure) that the concept of arrays does not really...
3
1665
by: Tom J | last post by:
Why does the following cause an EntryPointNotFoundException when I do ExitWindows(0,0); ? >>>>>>>>>>>>>> static extern bool ExitWindows(ulong dwReserved, uint uReserved); <<<<<<<<<<<<<<<<<<
13
7437
by: Christian Westerlund | last post by:
Hi! I'm trying to use P/Invoke and a Method which takes an IntPtr where I am supposed to put an address to a method which the native method will use to communicate back to me. How do I convert a method to an IntPtr? / Christian
3
5507
by: Charles Denny | last post by:
I'm trying to call CertFindCertificateInStore to find all certificates in the store that have the Code Signing enhanced key usage. I'm running into problems marshalling the array of OIDs in _CTL_USAGE. I keep getting a "This type can not be marshalled as a structure field." Does anyone have any ideas as to what I'm doing wrong? Here's the code: namespace CertSignTest { using System;
8
9580
by: Charles Law | last post by:
Hi guys There's nothing much going on in Interop at the moment, so I'll post this here as well in case someone can give me a quick answer. The following is an extract from MSDN: <extract> *** Setting the Ambient User Mode on the WebBrowser Object ***
7
5406
by: stephan querengaesser | last post by:
hi ng, i try to invoke a webservice-method with an filter-object, that contains value types. if i don´t want to filter the return value of the method, i have to pass a new instance of the filter-object without setting any properties. but the value type-properties can´t be null and the filter is set to 0 (int) or false (bool). therefore i did implement the propertySpecified-pattern like this:
2
8420
by: rdilipk | last post by:
I am posting at the end of this post some code that P/Invoke's SetSystemTime to set the local system time. This call fails -- i.e the time is not set and the API returns false. However calling Marshal.GetLastWin32Error immediately afterwards returns an errorcode of 0 indicating that the operation somehow executed successfully. This is a plain vanilla console app written in C#. It runs on vista and has a manifest embedded in it that...
0
9543
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
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...
0
10257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10029
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
9077
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
7567
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
6808
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
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...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.