473,508 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pinvoke question: marshalling pointers to blittable types

I have to call a c++ library funtion with the following signature:
void ReadBool(bool* pb1)
>From C# I've imported the function as
[DllImport("library.dll")]
unsafe static extern void NicamPLCRead(out bool);

but this does not work, the bool (not initialized) is not correctly
returned.

But when I write

[DllImport("library.dll")]
unsafe static extern void NicamPLCRead(ref bool);

All goes well.

It is correct that to marshall poiters it is only correct to use "ref"
and not "out"?

Thanks.

Jun 5 '07 #1
4 2426
it's true. when marshalling pointers you must explicitly use ref (out
is a C# buzzword for ref with checks).

Beorne je napisao/la:
I have to call a c++ library funtion with the following signature:
void ReadBool(bool* pb1)
From C# I've imported the function as

[DllImport("library.dll")]
unsafe static extern void NicamPLCRead(out bool);

but this does not work, the bool (not initialized) is not correctly
returned.

But when I write

[DllImport("library.dll")]
unsafe static extern void NicamPLCRead(ref bool);

All goes well.

It is correct that to marshall poiters it is only correct to use "ref"
and not "out"?

Thanks.
Jun 5 '07 #2
"Beorne" <ma*******@gmail.comwrote in message
news:11**********************@w5g2000hsg.googlegro ups.com...
>I have to call a c++ library funtion with the following signature:
void ReadBool(bool* pb1)
>>From C# I've imported the function as

[DllImport("library.dll")]
unsafe static extern void NicamPLCRead(out bool);

but this does not work, the bool (not initialized) is not correctly
returned.

But when I write

[DllImport("library.dll")]
unsafe static extern void NicamPLCRead(ref bool);

All goes well.

It is correct that to marshall poiters it is only correct to use "ref"
and not "out"?
Not really, all depends on the semantics.

If the caller does something like this...
void __stdcall F(bool * b)
{
// set the value of the variable who's address is passed in b to true
*b = true;
}
then you need to call the function like this:

[DllImport("library.dll")]
extern static void F([Out,MarshalAs( UnmanagedType.I1 )] out bool b);
...
F(out b);
If on the other hand, the calle looks like this:
....
bool m_b;
void __stdcall F(bool * b)
{
m_b = false;
// set b to the address of variable m_b
b = &m_b;
}

then you have to call F as:

[DllImport("library.dll")]
extern static void F([In, Out,MarshalAs( UnmanagedType.I1 )] ref bool b);
..
bool b = false;
F(ref b);

Note that when using "bool" in C++, you need to apply the MarshalAs(
UnmanagedType.I1 ) attribute on the bool parameter, failing to do this will
marshal the bool as an int value, which is not what you are expecting in C#.
Willy.

Jun 5 '07 #3
bool m_b;
void __stdcall F(bool * b)
{
m_b = false;
// set b to the address of variable m_b
b = &m_b;
}
What do you expect this to accomplish? It overwrites the local copy of the
pointer, passing no data to the caller.

A parameter needing to be passed as ref would be used thusly:

void __stdcall F(bool * pb)
{
*pb++;
}
Jun 5 '07 #4
"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:ui**************@TK2MSFTNGP02.phx.gbl...
>
>bool m_b;
void __stdcall F(bool * b)
{
m_b = false;
// set b to the address of variable m_b
b = &m_b;
}

What do you expect this to accomplish? It overwrites the local copy of
the pointer, passing no data to the caller.

A parameter needing to be passed as ref would be used thusly:

void __stdcall F(bool * pb)
{
*pb++;
}
Oh sure, my bad, forget about this sample.

Willy.

Jun 5 '07 #5

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

Similar topics

4
10664
by: TT (Tom Tempelaere) | last post by:
Hi people I am wrapping a C dll using PInvoke from C#. I need to wrap the following signature in C int dma_start( const UCHAR* data, UINT data_length ) The function should start with a DMA...
1
4661
by: | last post by:
Hi, Is there any good links for datatype interop? I need to pass some structure pointers into an unmanaged method and return char* etc but having some problems in my C++/CLI proxy class. I...
0
1054
by: Dgdege | last post by:
Hi all, I want to wrapp an exiting API written in C++ to .NET using managed C++ extensions. I want my managed C++ class library to link with the unmanaged dll. So I don't use PInvoke for...
7
3456
by: Asfar | last post by:
I have a MFC dll in which one of the parameters is of LPSTR. The declaration of my MFC dll looks like extern "C" __declspec(dllexport) int WINAPI EXPORT TestPInvokeFunc(LPSTR szFileName, LPSTR...
5
7857
by: _iycrd | last post by:
After numerous problems, I'm having second thoughts about using C++/CLI to wrap a native DLL. On the other hand, PInvoke seems like it will take a huge amount of work, if it will work at all. ...
2
1478
by: shankgreen | last post by:
My question is related to what happens under the hood when marshalling happens. When you pass a pointer to a structure from managed to unmanaged, and if the unmanaged code modified the structure,...
6
1725
by: gregarican | last post by:
I am trying to port a legacy CTI application written in another programming language to C# 2005. From my initial research into it I see I can utilize the DllImport method to tap into the DLL file...
2
4936
by: Beorne | last post by:
I have to call a c++ library funtion returning a string with the following signature: char *get_identifier(); Usually when I have to marshal a function with a char* output parameter I do: ...
1
1639
by: roche72 | last post by:
I am having some trouble marshalling data between c++/C# Here is the C++ code: --------------------------------------------------------------------------------- typedef struct { ...... ...
0
7118
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...
1
7038
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...
0
5625
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,...
1
5049
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...
0
4706
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...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
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 ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
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...

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.