我试图调用在C++从vb.net DLL函数,但我遇到了以下错误:内存损坏调用C++从VB.NET
Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'D:...\calling_project_in_VB.vshost.exe'.
Additional information: A call to PInvoke function 'calling_project_in_VB! calling_project_in_VB.Module1::add' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
我希望有人可能精通这些东西来发现我要去哪里错了?我会很感激任何建议,到目前为止,Google帮助我确定问题的实质并非毫无用处。下面是我试图运行小型可重复的例子:
的C++被设置为这样:
called_c.h:
extern "C" __declspec(dllexport)
int add(int* a, int* b);
called_c.cpp:
#include "stdafx.h"
#include "called_c.h"
#include <string>
using namespace std;
// using namespace System;
int add(int* a, int* b)
{
int Aa = *a;
int Bb = *b;
return Aa + Bb;
}
这里是VB函数试图调用C(其中“。 ..”是我的机器上的路径):
Imports System.Runtime.InteropServices
Imports System
Module Module1
'Public Class called_c
<DllImport("D:\...\called_c.dll", EntryPoint:="add", ExactSpelling:=False)>
Public Function add(ByRef a As Int32, ByRef b As Int32) As Int32
End Function
'End Class
Sub Main()
Dim val1 As Int32
Dim val2 As Int32
Dim answer As Int32
val1 = 3
val2 = 4
answer = add(val1, val2)
MsgBox(answer)
End Sub
End Module
为什么地球上你想要通过INT指针只是增加他们(你*不*引用无论如何需要这些)。 – crashmstr
@crashmstr这只是一个小的可重复的例子,我希望能够成为一个更复杂的问题的前兆,其中0填充的向量将被传入c代码,填充并返回到VB。如果你有一个小例子,如果你能分享它,我会非常感激。 – user2256085
我不知道这是否有帮助,但可以尝试添加['<[In]()>'属性](https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.inattribute(v = vs.110).aspx)转换为'ByRef'参数,以防止Marshaler将该值复制回来。 – TnTinMn