473,320 Members | 1,802 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,320 software developers and data experts.

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.dll")] private static extern
bool T_Cursor_GetBoolean(
int key, short columnID, ErrorCode *code
);

And in C++ as

extern "C" {
__declspec(dllexport) bool T_Cursor_GetBoolean(
int natKey, short cid, int *code
);
}

__declspec(dllexport) bool T_Cursor_GetBoolean(
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
CompactFramework.

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 8457
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(UnamangedType.VariantBool)] you get a 16-bit
VARAIANT_BOOL.
With [MarshalAs(UnamangedType.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.dll")]
[return: MarshalAs(UnmanagedType.I1)]
private static extern bool T_Cursor_GetBoolean( ...
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.google.c om...
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.dll")] private static extern
bool T_Cursor_GetBoolean(
int key, short columnID, ErrorCode *code
);

And in C++ as

extern "C" {
__declspec(dllexport) bool T_Cursor_GetBoolean(
int natKey, short cid, int *code
);
}

__declspec(dllexport) bool T_Cursor_GetBoolean(
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
CompactFramework.

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
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){...
4
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++...
5
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...
3
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
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...
3
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...
8
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>...
7
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...
2
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.