IntPtr when calling C++ functions

R

Richards

The .NET documentation has the following statement about

"The IntPtr type can be used ...... as a common means of referring to data
between languages that do and do not support pointers."

Can someone give me an example of how I pass a pointer (ByRef in VB terms)
parameter from a VB program to a C++ function?

I currently am using IntPtr, and I can get the code to compile, but the ByRef
paramenter doesn't seem to be working. When I call the C++ function.

Thanks
 
M

Mattias Sjögren

Can someone give me an example of how I pass a pointer (ByRef in VB terms)
parameter from a VB program to a C++ function?

Why don't you instead post the signature of the C++ function you want
to call and your current calling VB code? There are too many
variations on this to show it all in just one example.


Mattias
 
R

Richards via DotNetMonster.com

Thank you for your reply. Here is the .Net call and the C++ method:


Private MyIntPtr As System.IntPtr

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.
EventArgs) Handles Timer1.Tick

'For testing only
Dim x As Integer = 100

'Mattias - I believe the following creates a pointer to an interger
value. The integer value is initialized to 1
MyIntPtr = New IntPtr(1)

'Mattias - I believe this chages the value of the integer pointed to
by MyIntPtr to 2
MyIntPtr = op_Explicit(2)

'Mattias, this will test that I can pass a "ByValue" parameter (x) and
a "ByRef" parameter (MyIntPtr)
x= MyImprovedNS.CalcDataCrc(x, MyIntPtr)

End Sub

Here is the C++ (abridged):

int ImprovedNetsen::NetsenMethods::CalcDataCrc(long by_value, IntPtr
by_reference )
{
// Mattias, I belived this changes the integer pointed to by "by_reference"
to 1000
*&by_reference = 1000;
return true;
}

The Debugger Watch window appears to confirm that the C++ code works as I
expected. However, when returning to the VB program, MyIntPtr is not changed.

Perhaps I do not properly understand the IntPtr type. I am not a C++
programmer, so my knowledge of that language is not great.

Thank you again for your assistance.

Best Regards,

Richard
(PS: I am in the United States, so I will be on holiday until 28/November. It
is not necessary to respond before Monday.)
 
M

Mattias Sjögren

'Mattias - I believe the following creates a pointer to an interger
value. The integer value is initialized to 1
MyIntPtr = New IntPtr(1)

No, it creates a pointer-sized integer (which is what an IntPtr is)
with the value 1. If you treat it as a pointer it will "point to"
address 1 in memory.

'Mattias - I believe this chages the value of the integer pointed to
by MyIntPtr to 2
MyIntPtr = op_Explicit(2)

I don't think it even compiles, but if it did all it would do is
change the "address" to 2.

int ImprovedNetsen::NetsenMethods::CalcDataCrc(long by_value, IntPtr
by_reference )

OK I assume you're using managed C++ since you're using the IntPtr
type here as well.

// Mattias, I belived this changes the integer pointed to by "by_reference"
to 1000
*&by_reference = 1000;

That doesn't really make sense. You're just taking the address of
by_reference and then immediately dereferencing that address again. So
the code is the same as

by_reference = 1000;

You're not passing anything by reference here, the IntPtr parameter is
still passed by value. So all you're changing is the local copy of
by_reference.

It looks like you're trying to return an integer value to the caller
through a byref parameter. To do that you'd declare the function

int ImprovedNetsen::NetsenMethods::CalcDataCrc(long by_value, int __gc
*by_reference )
{
*by_reference = 1000;

No need for IntPtrs here.

return true;

This also doesn't really make sense since the return type is int.

(PS: I am in the United States, so I will be on holiday until 28/November. It
is not necessary to respond before Monday.)

Have a great Thanksgiving.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top