尝试复制数组时从 DLL 调用函数时程序崩溃

Program crashes when calling function from DLL when trying to copy an array

本文关键字:函数 程序 崩溃 调用 复制数组 DLL      更新时间:2023-10-16

今天我尝试制作我的第一个DLL和我的第一个使用DLL的应用程序。

DLL 是用 C++ 制作的,这是我调用的代码:

void Graph::findPath_r(Node* pStart, Node* pEnd, std::vector<cell> &road, cell &costMx)
{
//.....
    if(pEnd->getParent() != NULL)
    {
        while(!path.empty())
        {
            road.push_back(path.top()->getName());
            costMx += path.top()->getGCost();
            path.pop();
        }
        return;
    }
    return;
}
vector <int>tbcway;
int FUNCTION CalculatePath(int Start, int End, int * Array, int &cost)
{
    dgraph->findPath_r(xNode[Start].NodeID ,xNode[End].NodeID,tbcway,cost); 
    dgraph->reset();
    std::copy(tbcway.begin(), tbcway.end(), Array);
    tbcway.clear();
    return 1;
}

这就是我在 VB.net 中宣布并称之为:

Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("RCP.dll")> _
Public Shared Function LOAD_SYSTEM() As Boolean
End Function
<DllImport("RCP.dll")> _
Public Shared Function GetPluginVersion() As Integer
End Function
<DllImport("RCP.dll")> _
Public Shared Function CalculatePath(ByVal StartNode As Integer, ByVal EndNode As Integer, ByRef Array() As Array, ByRef cost As Integer) As Integer
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    LOAD_SYSTEM()
    MsgBox(GetPluginVersion().ToString())
    Dim path(4096) As Array
    Dim movecost As Integer
    CalculatePath(1702, 27932, path, movecost)
End Sub
End Class

那么,这段代码有什么问题呢?我得到的错误是:

调用 PInvoke 函数 'RCP GUI!RCP_GUI。Form1::CalculatePath' 使堆栈不平衡。这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。检查 PInvoke 签名的调用约定和参数是否与目标非托管签名匹配。

这可能是调用约定不匹配。

尝试使用不同的调用约定装饰您的 DllImport,看看哪些有效(我的猜测是它应该是 cdecl)。