请问VB调用OCX中带指针的函数,应该怎么做?
在vc里的函数是这样的
long CMytestCtrl::pow(float FAR* power)
{
// TODO: Add your dispatch handler code here
float datapoints[25536];
float datapointi[25536];
.......
}
我在VB里这样写:
dim power as single
mytest1.pow(power)
这样操作后,提示出错,类型不匹配。
在VB里我怎么调用这个函数呢?
请大家提提意见
[解决办法]
我自己作了一个例子。
vc的dll如下:
extern "C " _declspec(dllexport) long MyPow(float FAR* power)
{
*power+=2;
return (long)pow(*power,3);;
}
vb.net测试代码:
函数声明如下:
Public Declare Function MyPow Lib "SDK_Dll.dll " Alias "MyPow " (ByRef power As Single) As Long
调用如下:
Dim power As Single = 2
Dim lData As Long = MyPow(power)
MessageBox.Show(power.ToString() + vbCrLf + lData.ToString())
你可以对照的看看你声明是不是声明错了。
[解决办法]
代码自动完成那里提示pow要接受的参数类型是什么?你照着写不就可以了么
[解决办法]
注册不上。提示:
Unable to locate the LabVIEW Run-Time Engine.
POWER requires a version 7.1(or compatible) LabVIEW Run-Time Engine. Please contact the vendor of POWER to correct this problem.
估计还有别的东西一起要用的。
[解决办法]
我的调用方法:利用GCHandle可以传递任何类型的变量.供你参考一下
Imports System.Runtime.InteropServices
Private Shared Function nRsaEncrypt(ByRef byteDest() As Byte, ByVal Ctx() As Integer, ByVal byteSrc() As Byte) As Boolean
Dim SrcObj As GCHandle = GCHandle.Alloc(byteSrc, GCHandleType.Pinned)
Dim SrcPtr As IntPtr = SrcObj.AddrOfPinnedObject
Dim CtxObj As GCHandle = GCHandle.Alloc(Ctx, GCHandleType.Pinned)
Dim CtxPtr As IntPtr = CtxObj.AddrOfPinnedObject
ReDim byteDest(127)
Dim DestObj As GCHandle = GCHandle.Alloc(byteDest, GCHandleType.Pinned)
Dim DestPtr As IntPtr = DestObj.AddrOfPinnedObject
Dim DestLen As Integer = 128
Dim blnRtn As Boolean
blnRtn = vRsaEncrypt(CtxPtr.ToInt32, SrcPtr.ToInt32, byteSrc.Length, DestPtr.ToInt32, DestLen)
If SrcObj.IsAllocated Then SrcObj.Free()
If CtxObj.IsAllocated Then CtxObj.Free()
If DestObj.IsAllocated Then DestObj.Free()
SrcPtr = Nothing
CtxPtr = Nothing
DestPtr = Nothing
DestLen = Nothing
Return blnRtn
End Function