使用VB的C ++ DLL和传递指针? [英] Using a C++ DLL from VB, and Passing Pointers?

查看:47
本文介绍了使用VB的C ++ DLL和传递指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在使用我在C ++中编写的DLL,并且正在尝试从VB调用并使用它。

。 />

这对我按值传递参数的函数很好,但是我不能指示工作。


我在VB.net应用程序中收到以下错误:

"类型未处理的异常

''System.Runtime.InteropServices.MarshalDirectiveEx ception''发生在

Pendant.exePInvoke限制:无法返回变种"

对于dll,GamePad.dll,我设置了这样的功能:


#define DllExport externC __declspec(dllimport)__stdcall


void DllExport TestFunction(int * pv)

{

* pv = 987; //只是传回任意值

}

在Visual Basic中我导入了这样的函数:


< DllImport (QUOT; gamepad.dll")>公共共享函数TestFunction(ByRef v

作为整数)

结束函数


然后,我在按钮中使用该函数:


昏暗的结果作为整数

TestFunction(结果)

TextBox1.Text = result.ToString

错误发生在TestFunction()调用。


有没有人有任何想法?


感谢您提供任何反馈。


问候,

.... John

解决方案

试试这个:


< DllImport(" gamepad.dll")>公共共享函数TestFunction(_

ByVal ptr As IntPtr)

结束函数


私有子TestPInvoke()

作为整数的暗淡结果

Dim ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(_

GetType(整数)))

TestFunction(ptr)

result = Marshal.ReadInt32(ptr)

TextBox1.Text = result.ToString()

End Sub

希望有所帮助..

Imran。


" John Alway" < JW ***** @ hotmail.com>在留言中写道

news:db ************************** @ posting.google.c om ...

你好,

我正在使用我在C ++中编写的DLL,并试图从VB调用并使用它。

这适用于我按值传递参数的函数,但是我无法获得指向工作的指示。

我在VB.net应用程序中收到以下错误:
类型未处理的异常
''System.Runtime.InteropServices.MarshalDirectiveEx ception''发生在
Pendant.exePInvoke限制:无法返回变体"

对于dll,GamePad.dll,我设置了这样的功能:

#define DllExport extern" C" __declspec(dllimport)__ stdcall

void DllExport TestFunction(int * pv)
{
* pv = 987; //只是传回任意值
}

在Visual Basic中我导入了这样的函数:

< DllImport(" gamepad.dll") >公共共享函数TestFunction(ByRef v
As Integer)
结束函数

然后,我在一个按钮中使用该函数:

Dim result As Integer
TestFunction(结果)
TextBox1.Text = result.ToString

错误发生在TestFunction()调用中。

有没有人有任何错误想法?

感谢您的反馈。

问候,
...... John



< blockquote> 2004-10-13,John Alway< jw ***** @ hotmail.com>写道:

你好,

我正在使用我用C ++编写的DLL,并试图从VB调用并使用它。
>
这适用于我按值传递参数的函数,但是我无法获得指向工作的指示。

我在VB.net中收到以下错误应用程序:
未处理的异常类型
''System.Runtime.InteropServices.MarshalDirectiveEx ception''发生在
Pendant.exePInvoke限制:无法返回变体"



那是因为你将它声明为一个函数,但你的方法确实是在C ++中的
void所以应该声明为sub ...

对于dll,GamePad.dll,我设置了这样的功能:

#define DllExport extern" C" __declspec(dllimport)__ stdcall

void DllExport TestFunction(int * pv)
{
* pv = 987; //只是传回任意值
}

在Visual Basic中我导入了这样的函数:

< DllImport(" gamepad.dll") >公共共享功能TestFunction(ByRef v
整数)
结束功能




更改为:


< DllImport(" gamepad.dll")> _ $ / $
Public Shared Sub TestFunction(ByRef v As Integer)

End Sub

-

Tom Shelton [ MVP]


" Imran Koradia" <无**** @ microsoft.com>在消息新闻中写道:<#9 ************* @ TK2MSFTNGP09.phx.gbl> ...

试试这个:
<的DllImport(QUOT; gamepad.dll")>公共共享函数TestFunction(_
ByVal ptr As IntPtr)
结束函数
私有子TestPInvoke()
昏暗结果为整数
Dim ptr As IntPtr = Marshal.AllocHGlobal (Marshal.SizeOf(_
GetType(Integer)))
TestFunction(ptr)
result = Marshal.ReadInt32(ptr)
TextBox1.Text = result.ToString()
End Sub



感谢你和汤姆谢尔顿。两种方式都有效!


然而,我希望做的是将这个指针传递给

一个结构。我觉得这很容易,但结果却不会是b $ b。我可以看到你的方法可能是用于获得
的方法,因为我无法使用ByRef完成它。


如果我有数据我在DLL和VB中定义的struct,如何使用你提到的方法并从

结构中提取元素?


一旦我有了结构尺寸,我就需要能够将

指向内存中的相应偏移量以获得元素。


例如,如果结构如下:


结构数据{

int x;

byte y;

长z;

};


我想我会使用Marshal.OffSetOf()或其他东西。

另外,有没有办法将结构从C函数返回到VB?

我试过这个,但是无法做到。

谢谢为你提供帮助!


... John


Hello,

I''m using a DLL I wrote in C++, and am attempting to call and use it
from VB.

This works fine for functions where I pass parameters by value, but I
can''t get pointers to work.

I get the following error in the VB.net application:
"An unhandled exception of Type
''System.Runtime.InteropServices.MarshalDirectiveEx ception'' occurred in
Pendant.exe "PInvoke restriction: can not return variants""
For the dll, GamePad.dll, I have functions set up thus:

#define DllExport extern "C" __declspec(dllimport) __stdcall

void DllExport TestFunction(int *pv)
{
*pv = 987; // just pass back any arbitrary value
}
In Visual Basic I have the function imported thus:

<DllImport("gamepad.dll")> Public Shared Function TestFunction(ByRef v
As Integer)
End Function

Then, I use the function in a button:

Dim result As Integer
TestFunction(result)
TextBox1.Text = result.ToString
The error occurs right at the TestFunction() call.

Does anyone have any ideas?

Thanks for any feedback.

Regards,
....John

解决方案

Try this instead:

<DllImport("gamepad.dll")> Public Shared Function TestFunction( _
ByVal ptr As IntPtr)
End Function

Private Sub TestPInvoke()
Dim result as Integer
Dim ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf( _
GetType(Integer)))
TestFunction(ptr)
result = Marshal.ReadInt32(ptr)
TextBox1.Text = result.ToString()
End Sub
hope that helps..
Imran.

"John Alway" <jw*****@hotmail.com> wrote in message
news:db**************************@posting.google.c om...

Hello,

I''m using a DLL I wrote in C++, and am attempting to call and use it
from VB.

This works fine for functions where I pass parameters by value, but I
can''t get pointers to work.

I get the following error in the VB.net application:
"An unhandled exception of Type
''System.Runtime.InteropServices.MarshalDirectiveEx ception'' occurred in
Pendant.exe "PInvoke restriction: can not return variants""
For the dll, GamePad.dll, I have functions set up thus:

#define DllExport extern "C" __declspec(dllimport) __stdcall

void DllExport TestFunction(int *pv)
{
*pv = 987; // just pass back any arbitrary value
}
In Visual Basic I have the function imported thus:

<DllImport("gamepad.dll")> Public Shared Function TestFunction(ByRef v
As Integer)
End Function

Then, I use the function in a button:

Dim result As Integer
TestFunction(result)
TextBox1.Text = result.ToString
The error occurs right at the TestFunction() call.

Does anyone have any ideas?

Thanks for any feedback.

Regards,
...John



On 2004-10-13, John Alway <jw*****@hotmail.com> wrote:

Hello,

I''m using a DLL I wrote in C++, and am attempting to call and use it
from VB.

This works fine for functions where I pass parameters by value, but I
can''t get pointers to work.

I get the following error in the VB.net application:
"An unhandled exception of Type
''System.Runtime.InteropServices.MarshalDirectiveEx ception'' occurred in
Pendant.exe "PInvoke restriction: can not return variants""


That''s because you declare it as a function, but your method really is
void in C++ so should be declared as sub...
For the dll, GamePad.dll, I have functions set up thus:

#define DllExport extern "C" __declspec(dllimport) __stdcall

void DllExport TestFunction(int *pv)
{
*pv = 987; // just pass back any arbitrary value
}
In Visual Basic I have the function imported thus:

<DllImport("gamepad.dll")> Public Shared Function TestFunction(ByRef v
As Integer)
End Function



Change this to:

<DllImport ("gamepad.dll")> _
Public Shared Sub TestFunction (ByRef v As Integer)
End Sub
--
Tom Shelton [MVP]


"Imran Koradia" <no****@microsoft.com> wrote in message news:<#9*************@TK2MSFTNGP09.phx.gbl>...

Try this instead: <DllImport("gamepad.dll")> Public Shared Function TestFunction( _
ByVal ptr As IntPtr)
End Function Private Sub TestPInvoke()
Dim result as Integer
Dim ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf( _
GetType(Integer)))
TestFunction(ptr)
result = Marshal.ReadInt32(ptr)
TextBox1.Text = result.ToString()
End Sub


Thanks to both you and Tom Shelton. Both ways worked!

What I was hoping to do, however, was extend this pointer passing to
a structure. I thought this would be easy, however it''s turning out
not to be. I can see that your method is probably the way to go for
that, because I could not get it done using ByRef.

If I have a data struct which I define in both the DLL and VB, how
can I use the method you mention and extract elements from the
structure?

Once I have the strucure size, I would then need to be able to point
to the appropriate offsets in memory to get the elements out.

If, for instance, the structure were the following

struct data {
int x;
byte y;
long z;
};

I suppose I''d use Marshal.OffSetOf() or something.
Also, is there a way to return a struct from a C function to VB?
I tried this, and was unable to do it.
Thanks for you help!

...John


这篇关于使用VB的C ++ DLL和传递指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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