编程语言
首页 > 编程语言> > 无法在C#中加载C DLL

无法在C#中加载C DLL

作者:互联网

>My previous thread<

我创建了这个,因为我在VMBox上安装了WinXP,但无法使其再次运行.

这次我在表单上创建了一个OnLoad事件

        if (LoadLibrary("blowfish.dll") == 0)
        {
            Misc.LogToFile("Could not load dll", true);
            Application.Exit();
        }

在我的PC上运行正常,但在VMBox上LoadLibrary返回0.

一些用户提到问题在于将旧的NET Framework(2.0)与最新的MS Visual Studio(2008 SP1)上生成的dll混合在一起,所以我采取了行动,现在将其程序属性设置为可与NET 3.5配合使用

在VMBox上,我有NET 2.0,但这不是问题-程序本身运行良好.我也有C Redistributable(2005、2005 SP1和2008).

可能是什么问题呢?

解决方法:

如果您遇到进一步的麻烦,可以致电

Marshal.GetLastWin32Error();

这应该给你一个错误代码.

是否有可能部署了本机dll的调试版本,而该版本也需要MSVCR90D.DLL的调试版本?您应该已经发布了发行版本,因为调试版本要求目标系统上存在一组不同的dll.

显然,它可以在您的开发计算机上使用,因为所需库的所有调试版本都随Visual Studio一起提供.

这是您如何获得属于错误代码的消息的方式:

[DllImport("kernel32.dll")]
private static extern int FormatMessage(int dwFlags,
    IntPtr lpSource, int dwMessageId, int dwLanguageId,
    out string lpBuffer, int nSize, IntPtr pArguments);

public static string GetErrorMessage(int errorCode)
{
    const int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
    const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
    const int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;

    string lpMsgBuf;
    int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER
        | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;

    int retVal = FormatMessage(dwFlags, IntPtr.Zero, errorCode, 0,
                                out lpMsgBuf, 0, IntPtr.Zero);
    if (0 == retVal)
    {
        return null;
    }
    return lpMsgBuf;
}

标签:c,dll,crash,net-3-5,c-2
来源: https://codeday.me/bug/20191012/1900481.html