1

编辑(谢谢 ildjarn!):通过将委托(和要匹配的回调函数签名)更改为解决

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void InstallStatusDel([MarshalAs(UnmanagedType.LPStr)]string Mesg, int Status);

原帖:

我有一个用 C# 编写的调用 C dll 中的函数的 .net 应用程序的问题。我看过其他有类似问题的主题,但我一定错过了一些不同的东西。当我在 C# 中调试它时,我能够在 InstallStatusCallback 中打断点,但是当执行退出 InstallStatusCallback 时,会出现 AccessViolationException。我尝试使用 C 进行调试,并且在执行从回调返回之前发生访问冲突。感谢您的任何意见。

C dll 中的项目设置默认设置为使用 __cdecl。在 C DLL 领域中,有以下代码:

typedef void (__cdecl *StatusCallback)(const char* Mesg, int Status);
__declspec(dllexport) int Install(void* thing1, void* thing2, void* thing3, StatusCallback Func);


int Install(void* thing1, void* thing2, void* thing3, StatusCallback Func)
{
    Func("msg", 3);
    return 0;
}

在 C# 中,我有:

public partial class InstallerStatus : Form
{
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void InstallStatusDel(StringBuilder Mesg, int Status);

    public static extern int Install(IntPtr thing1, IntPtr thing2, IntPtr thing3, InstallStatusDel Func);
    [DllImport("myDll.dll", CallingConvention = CallingConvention.Cdecl)]

    private IntPtr mThing1;
    private IntPtr mThing2;
    private InstallStatusDel mInstallStatusFunc;
    private BackgroundWorker mInstallWorker;

    public InstallerStatus(IntPtr pThing1, IntPtr pThing2)
    {
        InitializeComponent();

        mThing1 = pThing1;
        mThing2 = pThing2;
        mInstallStatusFunc = InstallStatusCallback;

        mProgressBar.Minimum = 0;
        mProgressBar.Maximum = 100;
        mProgressBar.Value = 0;

        mInstallWorker = new BackgroundWorker();
        mInstallWorker.DoWork += new DoWorkEventHandler(InstallWork);
        mInstallWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(InstallWork_Completed);
    }

    private void InstallWork(object sender, DoWorkEventArgs e)
    {
        Install(mThing1, mThing2, IntPtr.Zero, mInstallStatusFunc);
    }

    private void InstallWork_Completed(object sender, RunWorkerCompletedEventArgs e)
    {
        Close();
    }

    private void InstallStatusCallback(StringBuilder PartName, int Status)
    {
    }

    private void InstallLoad_Shown(object sender, EventArgs e)
    {
        mInstallWorker.RunWorkerAsync();
    }
}
4

1 回答 1

2

通过将委托(和要匹配的回调函数签名)更改为来解决

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void InstallStatusDel([MarshalAs(UnmanagedType.LPStr)]string Mesg, int Status);

谢谢你!

于 2013-03-07T20:18:10.190 回答