简体   繁体   English

调用具有“指针”和结构的DLL(C ++-> VB.NET)

[英]Call a DLL with 'pointer' and structure (C++ -> VB.NET)

I have problem about how I call a function on DLL to get data from a structure ... 我对如何在DLL上调用函数以从结构中获取数据有疑问...

Have a C++ example about how it's works the library, and I want to use it on vb.net 有一个有关库如何工作的C ++示例,我想在vb.net上使用它

Okay, this is the working c++ example: 好的,这是有效的c ++示例:

Declaration: 宣言:

BOOL (WINAPIV* MyFun)(DWORD start1, DWORD stop1, P_RESULT pResult, PDWORD pTpNum, myCALLBACK lpFunc) = NULL;

Call: 呼叫:

// pStruc = RESULT structure
// TrpNum1, TrpNum is ULONG

ULONG TrpNum1=0;
MyFun(Start,Stop,pStruc+TrpNum,&TrpNum1,&myCB);

Callback: 打回来:

void __stdcall myCB (RESULT *pStruc)
{
    printf ("%.3f", (double)pStruc->val1);
    tpn++;
}

And of course, the structure: 当然,结构为:

typedef struct {
    BOOL            mybool;
    DWORD           val1;       
    DWORD           val2;       
} RESULT, *P_RESULT;

And this is my VB.net code (not working): 这是我的VB.net代码(不起作用):

Public Declare Auto Function MyFun Lib "\mydll.dll" ( _
        ByVal start As UInt32, _
        ByVal stop As UInt32, _
        ByRef Result As RESULT, ByRef pTpNum As UInt32, ByVal lpFunc As DlgCB) As Boolean

Public Sub CallMyFun()
    Dim Res As New RESULT
    Dim TpN As UInt32

    If MyFun(100, 200, Res, TpN, AddressOf myCB) Then
        SendDebug("OK")

    Else
        SendDebug("Failed!")
    End If

End Sub

Public Delegate Function DlgCB(ByVal Res As RESULT) As Boolean

Public Function myCB(ByVal Res As RESULT) As Boolean
    Debug.Print(Res.val1)
    Return True
End Function

Structure: 结构体:

Public Structure RESULT
    Dim mybool As Boolean
    Dim val1 As UInt32
    Dim val2 As UInt32
End Structure

What is wrong guys? 谁错了?

Thanks in advance. 提前致谢。

Edit: The error is this: "Attempted to read or write protected memory. This often indicates that other memory is corrupt." 编辑:错误是这样的:“试图读取或写入受保护的内存。这通常表明其他内存已损坏。”

Also i do a small 'fix' in the code. 我也在代码中做了一个小的“修正”。 Still don't work. 还是不行。

There are a large number of problems here: 这里有很多问题:

  • myBool is an Integer, not a Boolean myBool是整数,而不是布尔值
  • the Res variable almost surely can't be a local variable, make it a field Res变量几乎肯定不能是局部变量,请使其成为字段
  • possibly the same for TpN, it isn't clear if the C++ code stores the pointer TpN可能相同,尚不清楚C ++代码是否存储了指针
  • for both, if the C++ code stores a pointer then the object needs to be pinned. 对于这两种情况,如果C ++代码存储了一个指针,则需要固定该对象。 Marshaling yourself to memory allocated with Marshal.AllocHGlobal is much better 将自己编组到由Marshal.AllocHGlobal分配的内存中要好得多
  • the delegate you create with AddressOf is going to get garbage collected. 使用AddressOf创建的委托将收集垃圾。 You need to store a reference in a field so that can't happen 您需要在字段中存储引用,以免发生这种情况
  • the callback should declare the parameter ByRef, not ByVal 回调应声明参数ByRef,而不是ByVal
  • the C++ callback is void, you made it return a Boolean for some reason. C ++回调无效,您由于某种原因使它返回布尔值。

You need help to get this going, help that's a bit beyond with SO can provide. 您需要帮助才能做到这一点,SO所不能提供的帮助。 Ask an experienced team member for assistance or hire a consultant. 向经验丰富的团队成员寻求帮助或聘请顾问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM