我試圖調用在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