gpt4 book ai didi

c++ - 从 VB.NET 调用 C++ dll 的内存损坏

转载 作者:行者123 更新时间:2023-11-30 03:33:38 25 4
gpt4 key购买 nike

<分区>

我试图从 vb.net 调用 C++ dll 中的函数,但遇到以下错误:

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.

我希望有人能精通这些东西来发现我哪里出错了?我将不胜感激任何建议,到目前为止,谷歌在帮助我确定问题所在方面一直没有成效。这是我尝试运行的可重现的小示例:

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;
}

这里是试图调用 C 的 VB 函数(其中“...”是我机器上的路径):

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com