简体   繁体   English

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

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

Today I tried to make my first DLL and my first application which would use a DLL. 今天,我尝试制作我的第一个DLL和第一个使用DLL的应用程序。

The DLL is made in C++ and this is the code I'm calling: 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;
}

and this is how I declared it in VB.net and called it: 这就是我在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

So, what is wrong with this code? 那么,这段代码有什么问题呢? The error I am getting is: 我得到的错误是:

A call to PInvoke function 'RCP GUI!RCP_GUI.Form1::CalculatePath' has unbalanced the stack. 调用PInvoke函数'RCP GUI!RCP_GUI.Form1 :: CalculatePath'已使堆栈不平衡。 This is likely because the managed PInvoke signature does not match the unmanaged target signature. 这可能是因为托管PInvoke签名与非托管目标签名不匹配。 Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature. 检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配。

This is probably a calling convention mismatch. 这可能是调用约定不匹配。

Try decorating your DllImport with different calling conventions to see which works (my guess is that it should be cdecl). 尝试使用不同的调用约定装饰DllImport,以查看哪种工作方式(我想这应该是cdecl)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM