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

Pointers btwn C# and C++

I have a native C++ pointer object which I need to hold in a C# environment,
initialize in the C++ environment and the pass between the C# and C++
environments. Which is the data structure to use? I have tried to use an
IntPtr in C#, send it through C++/CLI as a System::IntPtr and convert it to a
pointer of my class in the end, but that gives compilation errors. What I
guess I need is som C# equivalence to a void* to keep in the C# environment.
But I have no clue how to do it...
May 24 '07 #1
6 13278
>I have tried to use an
IntPtr in C#, send it through C++/CLI as a System::IntPtr and convert it to a
pointer of my class in the end, but that gives compilation errors.
Can you post the errors and your code? Using IntPtr should work
nicely. You can also use an actual void* in C# if you enable unsafe
code.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 24 '07 #2

"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:97**********************************@microsof t.com...
>I have a native C++ pointer object which I need to hold in a C#
environment,
initialize in the C++ environment and the pass between the C# and C++
environments. Which is the data structure to use? I have tried to use an
IntPtr in C#, send it through C++/CLI as a System::IntPtr and convert it
to a
pointer of my class in the end, but that gives compilation errors. What I
guess I need is som C# equivalence to a void* to keep in the C#
environment.
But I have no clue how to do it...
An IntPtr is the right thing to use, as long as this is a native pointer,
i.e. not a pointer into a garbage collected object. Use the ToPointer()
method to get the native pointer back.
May 24 '07 #3
The C# app:

IntPtr m_DVPSDK_ptr;

[DllImport(g_wrapper_dll_path)]
static extern int Initialize(
out IntPtr class_ptr,
ref int[] devices_ids,
Int32 encoder_id);

[DllImport(g_wrapper_dll_path)]
static extern string GetLastError(
IntPtr class_ptr,
Int32 encoder_id);

void Initialize()
{
int[] l_devs_ids = new int[g_max_devices];
int l_nr_of_devices = Initialize(
out m_DVPSDK_ptr,
ref l_devs_ids,
m_settings.EncoderId);

if (l_nr_of_devices == 0)
{
//TODO: throw error?
}
}
The C++/CLI code:

extern "C" int Initialize(
System::IntPtr class_ptr,
array<int>^ devices_ids,
int encoder_id)
{
return Impl::Initialize(
(DVP1412DLL*) class_ptr, //ERROR 2 BELOW
devices_ids,
encoder_id);
}

Error 2 error C2440: 'type cast' : cannot convert from 'System::IntPtr' to
'DVP1412DLL *'

extern "C" const System::String^ GetLastError(
System::IntPtr class_ptr,
int encoder_id)
{
return Impl::GetLastError(
(DVP1412DLL*) class_ptr, //ERROR 12 BELOW
encoder_id);
}
Error 12 error C2440: 'type cast' : cannot convert from 'System::IntPtr' to
'DVP1412DLL *'
"Mattias Sjögren" wrote:
>
I have tried to use an
IntPtr in C#, send it through C++/CLI as a System::IntPtr and convert it to a
pointer of my class in the end, but that gives compilation errors.

Can you post the errors and your code? Using IntPtr should work
nicely. You can also use an actual void* in C# if you enable unsafe
code.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 25 '07 #4
Only declaring an IntPtr (IntPtr myPtr;) in C# and passing it as an out
parameter to the C++ dll should work just fine then?

"Ben Voigt" wrote:
>
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:97**********************************@microsof t.com...
I have a native C++ pointer object which I need to hold in a C#
environment,
initialize in the C++ environment and the pass between the C# and C++
environments. Which is the data structure to use? I have tried to use an
IntPtr in C#, send it through C++/CLI as a System::IntPtr and convert it
to a
pointer of my class in the end, but that gives compilation errors. What I
guess I need is som C# equivalence to a void* to keep in the C#
environment.
But I have no clue how to do it...

An IntPtr is the right thing to use, as long as this is a native pointer,
i.e. not a pointer into a garbage collected object. Use the ToPointer()
method to get the native pointer back.
May 25 '07 #5
Seemed like it had to do with that I declared the functions as

__declspec(dllexport)

However, even though it compiles, the value of the IntPtr is not changed in
C# even though it is in C++

"Joachim" wrote:
The C# app:

IntPtr m_DVPSDK_ptr;

[DllImport(g_wrapper_dll_path)]
static extern int Initialize(
out IntPtr class_ptr,
ref int[] devices_ids,
Int32 encoder_id);

[DllImport(g_wrapper_dll_path)]
static extern string GetLastError(
IntPtr class_ptr,
Int32 encoder_id);

void Initialize()
{
int[] l_devs_ids = new int[g_max_devices];
int l_nr_of_devices = Initialize(
out m_DVPSDK_ptr,
ref l_devs_ids,
m_settings.EncoderId);

if (l_nr_of_devices == 0)
{
//TODO: throw error?
}
}
The C++/CLI code:

extern "C" int Initialize(
System::IntPtr class_ptr,
array<int>^ devices_ids,
int encoder_id)
{
return Impl::Initialize(
(DVP1412DLL*) class_ptr, //ERROR 2 BELOW
devices_ids,
encoder_id);
}

Error 2 error C2440: 'type cast' : cannot convert from 'System::IntPtr' to
'DVP1412DLL *'

extern "C" const System::String^ GetLastError(
System::IntPtr class_ptr,
int encoder_id)
{
return Impl::GetLastError(
(DVP1412DLL*) class_ptr, //ERROR 12 BELOW
encoder_id);
}
Error 12 error C2440: 'type cast' : cannot convert from 'System::IntPtr' to
'DVP1412DLL *'
"Mattias Sjögren" wrote:
>I have tried to use an
>IntPtr in C#, send it through C++/CLI as a System::IntPtr and convert it to a
>pointer of my class in the end, but that gives compilation errors.
Can you post the errors and your code? Using IntPtr should work
nicely. You can also use an actual void* in C# if you enable unsafe
code.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 25 '07 #6

"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:D4**********************************@microsof t.com...
Seemed like it had to do with that I declared the functions as

__declspec(dllexport)

However, even though it compiles, the value of the IntPtr is not changed
in
C# even though it is in C++
a C# ref or out parameter must be declared in C++/CLI as a reference
parameter.
>
"Joachim" wrote:
>The C# app:

IntPtr m_DVPSDK_ptr;

[DllImport(g_wrapper_dll_path)]
static extern int Initialize(
out IntPtr class_ptr,
ref int[] devices_ids,
Int32 encoder_id);
Don't do this. To use C++/CLI functions from C#, add a reference to the
generated DLL.
>>
[DllImport(g_wrapper_dll_path)]
static extern string GetLastError(
IntPtr class_ptr,
Int32 encoder_id);

void Initialize()
{
int[] l_devs_ids = new int[g_max_devices];
int l_nr_of_devices = Initialize(
out m_DVPSDK_ptr,
ref l_devs_ids,
m_settings.EncoderId);

if (l_nr_of_devices == 0)
{
//TODO: throw error?
}
}
The C++/CLI code:

extern "C" int Initialize(
System::IntPtr class_ptr,
array<int>^ devices_ids,
int encoder_id)
{
return Impl::Initialize(
(DVP1412DLL*) class_ptr, //ERROR 2 BELOW
Should be class_ptr->ToPointer(), but see below...
>devices_ids,
encoder_id);
}
The compiler shouldn't even allow what you're doing... you have managed
types as parameters, and those are limited to member methods of managed
types. Create a ref class and make these functions static methods within
that class. Or, make them instance methods, so you don't have to pass
around class_ptr.

You're also missing a level of indirection all over the place here. You
want Impl::Initialize to fill in the pointer, right?

Then:

int Initialize( System::IntPtr% class_ptr, array<int>^ devices_ids, int
encoder_id)
{
DVP1412DLL objectPointer;
int retval = Impl::Initialize(&objectPointer, devices_ids, encoder_id);
class_ptr = gcnew System::IntPtr(objectPointer);
return retval;
}
>>
Error 2 error C2440: 'type cast' : cannot convert from 'System::IntPtr'
to
'DVP1412DLL *'

extern "C" const System::String^ GetLastError(
System::IntPtr class_ptr,
int encoder_id)
{
return Impl::GetLastError(
(DVP1412DLL*) class_ptr, //ERROR 12 BELOW
encoder_id);
}
Error 12 error C2440: 'type cast' : cannot convert from 'System::IntPtr'
to
'DVP1412DLL *'
"Mattias Sjögren" wrote:
>
I have tried to use an
IntPtr in C#, send it through C++/CLI as a System::IntPtr and convert
it to a
pointer of my class in the end, but that gives compilation errors.

Can you post the errors and your code? Using IntPtr should work
nicely. You can also use an actual void* in C# if you enable unsafe
code.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

May 25 '07 #7

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

Similar topics

11
by: iceColdFire | last post by:
Hi, What is the Diff btwn Function overloading and overriding thanks, a.a.cpp
27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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
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...
0
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,...

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.