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

Expose C++ array through managed wrapper class.

I'm having a very difficult time coming across an appropriate solution
for this seemingly simple problem. I've written a managed wrapper
class for some legacy C++ routines. One routine generates an array of
ints, which I wish to expose through the managed wrapper. The managed
C++ wrapper is being used as an intermediary to allow VB.NET to access
the legacy C routines (and the return array, in particular).

Given these constraints, can someone please instruct the best course of
action? I can't figure out how to define/update the C array to be .NET
compatible. Do I need to use the Marshal class? Do I need to declare
an "array<int, 1>" and use a handle (^). Any help, particularly
specific syntax examples, would be tremendous.

void returnArray(int* pArray, int& pSize); //Legacy C function

Thanks, Josh

Dec 15 '06 #1
12 2154
Without doing any research I would think you have to use a SAFEARRAY in your
managed wrapper.

<jo*********@gmail.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
I'm having a very difficult time coming across an appropriate solution
for this seemingly simple problem. I've written a managed wrapper
class for some legacy C++ routines. One routine generates an array of
ints, which I wish to expose through the managed wrapper. The managed
C++ wrapper is being used as an intermediary to allow VB.NET to access
the legacy C routines (and the return array, in particular).

Given these constraints, can someone please instruct the best course of
action? I can't figure out how to define/update the C array to be .NET
compatible. Do I need to use the Marshal class? Do I need to declare
an "array<int, 1>" and use a handle (^). Any help, particularly
specific syntax examples, would be tremendous.

void returnArray(int* pArray, int& pSize); //Legacy C function

Thanks, Josh

Dec 15 '06 #2
Kurt thank you for responding. Unfortunately even if a SAFEARRAY is
the answer, I am uncertain how to properly implement it. If anyone
else has suggestions and specific examples I'd be grateful.

-Josh

Dec 15 '06 #3
jo*********@gmail.com wrote:
I'm having a very difficult time coming across an appropriate solution
for this seemingly simple problem. I've written a managed wrapper
class for some legacy C++ routines. One routine generates an array of
ints, which I wish to expose through the managed wrapper. The managed
C++ wrapper is being used as an intermediary to allow VB.NET to access
the legacy C routines (and the return array, in particular).

Given these constraints, can someone please instruct the best course
of action? I can't figure out how to define/update the C array to be
.NET compatible. Do I need to use the Marshal class? Do I need to
declare an "array<int, 1>" and use a handle (^). Any help,
particularly specific syntax examples, would be tremendous.

void returnArray(int* pArray, int& pSize); //Legacy C function
Based on that declaration, it looks like the caller is allocating memory
that this function will fill. If that's indeed the case, you can create a
managed array and pass a pinned pointer to the first element of that array
to your legacy function. The fact that pSize is being passed by reference
leads me to suspect that it's set to the size of the memory allocation on
input to the function, and set to the size of the actual data on return from
the function.
#pragma unmanaged
extern void r(int* p, int& s);
#pragma managed
public ref class X
{
static array<int>^ f(int s)
{
array<int>^ a = gcnew array<int>(s);
pin_ptr<intp = &a[0];
r(p,s);
return a;
}
};

-cd


Dec 15 '06 #4
Kurt wrote:
Without doing any research I would think you have to use a SAFEARRAY
in your managed wrapper.
SAFEARRAY is needed only for returning an array to VB6 or any other OLE
automation client. VB.NET can handle an array<int>^ since that's just an
ordinary CLR array.'

-cd
Dec 15 '06 #5
Josh

Look in MSDN there are several good examples for creating SAFEARRAYs.

<jo*********@gmail.comwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...
Kurt thank you for responding. Unfortunately even if a SAFEARRAY is
the answer, I am uncertain how to properly implement it. If anyone
else has suggestions and specific examples I'd be grateful.

-Josh

Dec 15 '06 #6
Kurt, thanks again. Carl, your example seems to be the direction I
need to head. Thank you for getting me pointed there. I will take
examine how this C++.NET class looks when viewed from VB.NET.

Can you tell me how the .NET code will differ when the C routine does
the memory allocation AND the array filling (returning an array
pointer)?

Thank you.
-Josh

Dec 15 '06 #7
Kurt, thanks again. Carl, your example seems to be the direction I
need to head. Thank you for getting me pointed there. I will take
examine how this C++.NET class looks when viewed from VB.NET.

Can you tell me how the .NET code will differ when the C routine does
the memory allocation AND the array filling (returning an array
pointer)?

Thank you.
-Josh

Dec 15 '06 #8
Kurt, thanks again. Carl, your example seems to be the direction I
need to head. Thank you for getting me pointed there. I will take
examine how this C++.NET class looks when viewed from VB.NET.

Can you tell me how the .NET code will differ when the C routine does
the memory allocation AND the array filling (returning an array
pointer)?

Thank you.
-Josh

Dec 15 '06 #9
Kurt, thanks again. Carl, your example seems to be the direction I
need to head. Thank you for getting me pointed there. I will take
examine how this C++.NET class looks when viewed from VB.NET.

Can you tell me how the .NET code will differ when the C routine does
the memory allocation AND the array filling (returning an array
pointer)?

Thank you.
-Josh

Dec 15 '06 #10

"Kurt" <ju**@junk.comwrote in message
news:uC**************@TK2MSFTNGP04.phx.gbl...
Without doing any research I would think you have to use a SAFEARRAY in
your managed wrapper.
SAFEARRAY is for COM objects, like VB6. .NET does things differently.
>
<jo*********@gmail.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
>I'm having a very difficult time coming across an appropriate solution
for this seemingly simple problem. I've written a managed wrapper
class for some legacy C++ routines. One routine generates an array of
ints, which I wish to expose through the managed wrapper. The managed
C++ wrapper is being used as an intermediary to allow VB.NET to access
the legacy C routines (and the return array, in particular).

Given these constraints, can someone please instruct the best course of
action? I can't figure out how to define/update the C array to be .NET
compatible. Do I need to use the Marshal class? Do I need to declare
an "array<int, 1>" and use a handle (^). Any help, particularly
specific syntax examples, would be tremendous.

void returnArray(int* pArray, int& pSize); //Legacy C function

Thanks, Josh


Dec 15 '06 #11

<jo*********@gmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...
Kurt, thanks again. Carl, your example seems to be the direction I
need to head. Thank you for getting me pointed there. I will take
examine how this C++.NET class looks when viewed from VB.NET.

Can you tell me how the .NET code will differ when the C routine does
the memory allocation AND the array filling (returning an array
pointer)?
Then you should allocate a managed array, memcpy the data, and free the
memory returned by the C routine in the way described in its documentation.
>
Thank you.
-Josh

Dec 15 '06 #12
Wonderful, thank you Ben, I'll try that.

-Josh

Dec 15 '06 #13

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

Similar topics

6
by: rakefet | last post by:
Hi. I'm really new to this world of .Net so your help would be most appreciated... We have API and COM interfaces developed in c. We would also like to supply a .net interface to our clients....
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
2
by: Brett Styles | last post by:
Hi Guys, I am trying to access a class in an unmanaged dll. I have created a wrapper managed class to access the functions I need but no matter what I try from the MSDN samples I can not get it to...
0
by: DotNetJunkies User | last post by:
Background: I am creating a VC++ .NET wrapper for a C++ DLL; the aim is to use this wrapper in C# as shown below: Range r = new Range( 2, 2 ); r = new Cell( “Hello Mum” ); Range is a...
3
by: WithPit | last post by:
I am trying to create an managed wrapper but have some problems with it by using abstract classes. In my unmanaged library code i had the following three classes with the following hierarchy ...
9
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for...
16
by: pkoniusz | last post by:
Hello everybody, Been just thinking how actually one could convert the following unmanaged code to the managed C++: struct JustAnExample { char value1; int value2; // etc ....
9
by: =?Utf-8?B?RWR3YXJkUw==?= | last post by:
I would greatly appreciate some help on passing managed object into unmanaged code. I need to pass a reference (address of) of a managed class into unmanaged code (written by a thrid party). The...
2
by: chandramohanp | last post by:
I am having problem to delete a Managed C++ DLL that was used in a pure C# DLL even in the following scenario. 1. Launch the application. This applicatio has its own default applicaiton...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.