I'm attempting to call a function in a C++ dll from vb.net, but am running into the following error:
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.
I'm hopeful someone might be versed enough in this stuff to spot where I'm going wrong? I would appreciate any suggestions, so far the googles has been unproductive in helping me identify what the issue is. Here is the small reproducible example I'm attempting to run:
The C++ is set up as such:
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;
}
And here is the VB function attempting to call the C (where "..." is the path on my machine):
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