通过托管包装类公开C ++数组。 [英] Expose C++ array through managed wrapper class.

查看:66
本文介绍了通过托管包装类公开C ++数组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个非常困难的时候遇到一个适当的解决方案

这个看似简单的问题。我为一些传统的C ++例程编写了一个托管包装器

类。一个例程生成一个

整数的数组,我希望通过托管包装器公开它。托管的

C ++包装器被用作中介,以允许VB.NET访问遗留C例程(特别是返回数组)。

。 >

鉴于这些限制,有人可以指示

行动的最佳方案吗?我无法弄清楚如何将C数组定义/更新为.NET

兼容。我需要使用Marshal类吗?我是否需要声明

" array< int,1>"并使用句柄(^)。任何帮助,特别是

特定的语法示例,都是巨大的。


void returnArray(int * pArray,int& pSize); //遗产C函数


谢谢,Josh

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

推荐答案

没有做任何研究我我认为你必须在你的

托管包装中使用SAFEARRAY。


< jo ********* @ gmail.comwrote in消息

news:11 ********************** @ 16g2000cwy.googlegro ups.com ...
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...

我很难遇到一个合适的解决方案

这个看似简单的问题。我为一些传统的C ++例程编写了一个托管包装器

类。一个例程生成一个

整数的数组,我希望通过托管包装器公开它。托管的

C ++包装器被用作中介,以允许VB.NET访问遗留C例程(特别是返回数组)。

。 >

鉴于这些限制,有人可以指示

行动的最佳方案吗?我无法弄清楚如何将C数组定义/更新为.NET

兼容。我需要使用Marshal类吗?我是否需要声明

" array< int,1>"并使用句柄(^)。任何帮助,特别是

特定的语法示例,都是巨大的。


void returnArray(int * pArray,int& pSize); //遗产C功能


谢谢,Josh
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



Kurt感谢您的回复。不幸的是,即使SAFEARRAY是答案,我也不确定如何正确实施它。如果有人

别有建议和具体例子,我将不胜感激。


-Josh

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


jo*********@gmail.com 写道:

我很难遇到一个合适的解决方案

这个看似简单的问题。我为一些传统的C ++例程编写了一个托管包装器

类。一个例程生成一个

整数的数组,我希望通过托管包装器公开它。托管的

C ++包装器被用作中介,以允许VB.NET访问遗留C例程(特别是返回数组)。

。 >

鉴于这些限制,有人可以指导最好的课程

的行动?我无法弄清楚如何定义/更新C数组是否与.NET兼容。我需要使用Marshal类吗?我是否需要

声明一个array< int,1>"并使用句柄(^)。任何帮助,特别是特定的语法示例,都会是巨大的。


void returnArray(int * pArray,int& pSize); // Legacy C函数
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



根据该声明,看起来调用者正在分配此函数将填充的内存

。如果确实如此,您可以创建一个

托管数组,并将一个固定指针传递给该数组的第一个元素

到您的遗留函数。 pSize通过引用传递的事实

导致我怀疑它被设置为

输入到函数的内存分配大小,并设置从函数返回到实际数据的大小

函数。

#pragma unmanaged

extern void r(int * p,int& s);

#pragma managed

公共引用等级X

{

静态数组< int> ^ f (int s)

{

array< int> ^ a = gcnew array< int>(s);

pin_ptr< intp =& ; a [0];

r(p,s);

返回a;

}

};


-cd

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



这篇关于通过托管包装类公开C ++数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆