473,804 Members | 3,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2190
Without doing any research I would think you have to use a SAFEARRAY in your
managed wrapper.

<jo*********@gm ail.comwrote in message
news:11******** **************@ 16g2000cwy.goog legroups.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*********@gma il.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*********@gm ail.comwrote in message
news:11******** **************@ 73g2000cwn.goog legroups.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

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

Similar topics

6
1900
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. Searching - I found many new words :-) like .Net wrapper, Assembly... but I couldn't find a clear cut answer -
16
14143
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 byteArray and afterwards see the changes in C#. (if you wanna know more details: the goal is to write the content of an existing char-array in c++ precisely into the passed byteArray, so that after the function has proceded the content of the...
2
2412
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 work with my code. I have a VB Net front end which need the access the unmanaged functions. The wrapper I wrote can be accessed but the wrapper will not compile when I refer to the unmanaged class. I have tried using header file for definitions...
0
1231
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 managed wrapper around an unmanaged range class. Vector is a managed wrapper around an unmanaged vector class. Cell is a managed wrapper around an unmanaged cell class. Here r returns a managed Vector* and r accesses a particular Cell in the Vector.
3
1816
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 Referenced (class) Object (abstract class, inheriting from referenced) Node (class, inheriting from object)
9
3123
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 this, I want to use it. Is it possible to call Managed class functions from Unmanaged class? How to do it? I did something like this. I declared a managed class (in C++ CLI) called as MyManagedClass whose
16
8789
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
3568
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 3rd party unmanaged DLL will pass this reference into standard Win32 unmanaged static callback function in my code. Inside this unmanaged callback function I need to cast this unmnaged pointer that I have received from 3rd party back into the...
2
3200
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 domain. 2. Create a new Application Domain. 3. Load the Pure C# DLL in the new applicaiton domain. This C# DLL uses managed C++ DLL. 4. Create a Proxy object using CreateInstanceAndUnwrap for the wrapper
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10319
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10076
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9144
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6851
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
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 we have to send another system
3
2990
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.