需要从非托管c ++代码调用托管代码! [英] need to call managed code from unmanaged c++ code !

查看:80
本文介绍了需要从非托管c ++代码调用托管代码!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个要求,我需要从一个

C ++代码调用用VB.Net编写的方法。我一直在疯狂阅读网上的各种文章

互操作和编组,但现在更加困惑了。


我需要符合的签名VB.Net DLL是

int MyAppInitFunc(int argc,char ** argv)


到这个程度我试过以下

公共函数MyAppInitFunc(ByVal argc as integer,ByRef argv as string)

as Integer

公共函数MyAppInitFunc(ByVal argc as Int32,ByVal argv()as string )

作为Int32


....基于我甚至尝试的一些帖子

公共函数MyAppInitFunc(ByVal argc as Int32 ,ByVal argv as IntPtr)

Int32


但是还没能得到任何工作。


是否有一些阅读我错过了?


问候。


-

Ravi Shankar

解决方案

我通过COM完成所有vb6互操作,这个效果很好


也许这是你的想法?


问候


米歇尔


" Ravi Shankar" < sh ********* @ newsgroup.nospamschreef in bericht

新闻:4F ********************* ************* @ microsof t.com ...





我有一个要求,我需要从

C ++代码中调用VB.Net编写的方法。我一直疯狂阅读网上的各种文章

wrt

互操作和编组,但现在更加困惑。


我需要在VB.Net DLL中符合的签名是

int MyAppInitFunc(int argc,char ** argv)


在这个程度上,我有试过以下

公共函数MyAppInitFunc(ByVal argc作为整数,ByRef argv作为字符串)

作为整数

公共函数MyAppInitFunc(ByVal argc as Int32,ByVal argv()as string)

as Int32


Public函数MyAppInitFunc(ByVal argc as Int32,ByVal argv as IntPtr)

as

Int32


但未能获得什么都可以工作。


是否有一些阅读我错过了?


问候。

-

Ravi Shankar



>我需要的签名在VB.Net DLL中符合


> int MyAppInitFunc(int argc,char ** argv)



这是否意味着C ++调用者希望这样的函数是从DLL导出的静态输出入口点? VB不允许你这样做。

Mattias


-

Mattias Sj?gren [C#MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。


Hello Michel,


请解释..我已经用注册COM Interop制作了我的项目;。我还是
也做了一个regasm / tlb:.....


我的所有功能都是ComVisible()。


由于VB.Net DLL和经典的Win32 DLL有区别,

我甚至做过IL调整以确保我的函数正在运行导出。


我无法弄清楚如何将Char **编组到我的托管代码中?


我尝试的组合是

1.< MarshalAs(UnmanagedType.SafeArray,SafeArraySubType:= VarEnum.VT_BSTR)>

ByVal argv as string()


这给了我一个错误,说11888等级的SafeArray已被传递给

方法,期望排名为1的数组

2.< MarshalAs (Unmanaged.LPArray)ByRef argv as string


这给我一个错误,说不能编组''参数#2'':无效

托管/非托管类型组合(字符串参数和返回类型必须

与LPStr,LP配对WStr,LPTStr,BSTR,TBStr,VBByRefStr或AnsiBStr)。


和上面的组合使用char()()而不是string()等等。

x -

Ravi Shankar

" Michel Posseth [MCP]"写道:


我通过COM做我所有的vb6互操作,这个效果很好


也许这是你的想法?


问候


米歇尔


" Ravi Shankar" < sh ********* @ newsgroup.nospamschreef in bericht

新闻:4F ********************* ************* @ microsof t.com ...





我有一个要求,我需要从

C ++代码中调用VB.Net编写的方法。我一直疯狂阅读网上的各种文章

wrt

互操作和编组,但现在更加困惑。


我需要在VB.Net DLL中符合的签名是

int MyAppInitFunc(int argc,char ** argv)


在这个程度上,我有试过以下

公共函数MyAppInitFunc(ByVal argc作为整数,ByRef argv作为字符串)

作为整数

公共函数MyAppInitFunc(ByVal argc as Int32,ByVal argv()as string)

as Int32


Public函数MyAppInitFunc(ByVal argc as Int32,ByVal argv as IntPtr)

as

Int32


但未能获得什么都可以工作。


是否有一些阅读我错过了?


问候。

-

Ravi Shankar




Hi,

I have a requirement where I need to call methods written in VB.Net from a
C++ code. I have been going crazy reading various articles on the net w.r.t
interop and marshalling but an more confused now that before.

the signature I need to conform in the VB.Net DLL is
int MyAppInitFunc(int argc, char** argv)

To this extent I have tried the following
Public Function MyAppInitFunc(ByVal argc as integer, ByRef argv as string)
as Integer
Public Function MyAppInitFunc(ByVal argc as Int32, ByVal argv() as string)
as Int32

.... based on some posts I even tried
Public Function MyAppInitFunc(ByVal argc as Int32, ByVal argv as IntPtr) as
Int32

but have not been able to get anything to work.

Is there perhaps some reading I''m missing up on ?

Regards.

--
Ravi Shankar

解决方案

I do all my vb6 interop through COM , this works great

maybe this is an idea for you to ?

Regards

Michel

"Ravi Shankar" <sh*********@newsgroup.nospamschreef in bericht
news:4F**********************************@microsof t.com...

Hi,

I have a requirement where I need to call methods written in VB.Net from a
C++ code. I have been going crazy reading various articles on the net
w.r.t
interop and marshalling but an more confused now that before.

the signature I need to conform in the VB.Net DLL is
int MyAppInitFunc(int argc, char** argv)

To this extent I have tried the following
Public Function MyAppInitFunc(ByVal argc as integer, ByRef argv as string)
as Integer
Public Function MyAppInitFunc(ByVal argc as Int32, ByVal argv() as string)
as Int32

... based on some posts I even tried
Public Function MyAppInitFunc(ByVal argc as Int32, ByVal argv as IntPtr)
as
Int32

but have not been able to get anything to work.

Is there perhaps some reading I''m missing up on ?

Regards.

--
Ravi Shankar



>the signature I need to conform in the VB.Net DLL is

>int MyAppInitFunc(int argc, char** argv)

Does that mean the C++ caller expects such a function to be a staticly
exported entrypoint from the DLL? VB doesn''t let you do that.
Mattias

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


Hello Michel,

Please explain.. I have made my project with "Register for COM Interop". I
have also done a regasm /tlb:.....

All my functions are ComVisible().

Since there is a difference in the VB.Net DLL''s and classic Win32 DLL''s,
I''ve even done the IL tweaking to ensure that my functions are being exported.

I can''t figure out how to get Char** marshalled into my managed code ?

The combinations Ive tried are
1. <MarshalAs(UnmanagedType.SafeArray, SafeArraySubType:=VarEnum.VT_BSTR)>
ByVal argv as string()

This gives me an error saying SafeArray of rank 11888 has been passed to a
method expecting an array of rank 1

2. <MarshalAs(Unmanaged.LPArray)ByRef argv as string

This gives me an error saying cannot marshal ''parameter #2'' : Invalid
managed/unmanaged type combination (string parameters and return types must
be paired with LPStr, LPWStr, LPTStr, BSTR, TBStr, VBByRefStr, or AnsiBStr).

and combiantions of the above with char()() instead of string() etc, etc.
x--
Ravi Shankar
"Michel Posseth [MCP]" wrote:

I do all my vb6 interop through COM , this works great

maybe this is an idea for you to ?

Regards

Michel

"Ravi Shankar" <sh*********@newsgroup.nospamschreef in bericht
news:4F**********************************@microsof t.com...

Hi,

I have a requirement where I need to call methods written in VB.Net from a
C++ code. I have been going crazy reading various articles on the net
w.r.t
interop and marshalling but an more confused now that before.

the signature I need to conform in the VB.Net DLL is
int MyAppInitFunc(int argc, char** argv)

To this extent I have tried the following
Public Function MyAppInitFunc(ByVal argc as integer, ByRef argv as string)
as Integer
Public Function MyAppInitFunc(ByVal argc as Int32, ByVal argv() as string)
as Int32

... based on some posts I even tried
Public Function MyAppInitFunc(ByVal argc as Int32, ByVal argv as IntPtr)
as
Int32

but have not been able to get anything to work.

Is there perhaps some reading I''m missing up on ?

Regards.

--
Ravi Shankar




这篇关于需要从非托管c ++代码调用托管代码!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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