简体   繁体   English

无法在C#中加载C ++ DLL

[英]Cannot Load C++ DLL in C#

>My previous thread< >我之前的主题<

I created this one,because I installed WinXP on VMBox and I cannot get it working again. 我创建了这个,因为我在VMBox上安装了WinXP,但无法使其再次运行。

This time I created an OnLoad Event on my form 这次我在表单上创建了一个OnLoad事件

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

Runs fine on my PC,but on VMBox LoadLibrary returns 0. 在我的PC上运行正常,但在VMBox上LoadLibrary返回0。

Some users mentioned that the problem would be in mixing older NET Framework(2.0) with dlls made on newest MS Visual studio(2008 SP1) so I took action and now the program properties it's set to work with NET 3.5 一些用户提到问题在于将旧的NET Framework(2.0)与最新的MS Visual Studio(2008 SP1)上生成的dll混合在一起,所以我采取了行动,现在将其程序属性设置为可与NET 3.5配合使用

On the VMBox I have NET 2.0,but this is not the problem - the program itself runs fine.I also have C++ Redistributable(2005,2005 SP1 and 2008). 在VMBox上,我有NET 2.0,但这不是问题-程序本身运行良好。我也有C ++ Redistributable(2005、2005 SP1和2008)。

What could be the problem? 可能是什么问题呢?

To further trouble should you could call 如果您遇到进一步的麻烦,可以致电

Marshal.GetLastWin32Error();

which should give you an error code. 这应该给你一个错误代码。

Is it possible that you deployed a debug version of your native dll which also requires a debug version of MSVCR90 D .DLL? 是否有可能部署了本机dll的调试版本,而该版本也需要MSVCR90 D .DLL的调试版本? You should have distributed the release version because the debug version requires a different set of dlls to be present on the target system. 您应该已经发布了发行版本,因为调试版本要求目标系统上存在一组不同的dll。

It obviously works on your development machine because all debug versions of the required libraries come with Visual Studio. 显然,它可以在您的开发计算机上使用,因为所需库的所有调试版本都随Visual Studio一起提供。

This is how you would get the message belonging to an error code: 这是您如何获得属于错误代码的消息的方式:

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

在LoadLibrary之后调用GetLastError,在此处检查错误代码值: http : //msdn.microsoft.com/en-us/library/ms681381.aspx ,看看是否有帮助。

It could be that the dll's location is on the path in one environment and not in the other. dll的位置可能在一个环境中而不是在另一个环境中。 It could also be permissions in one environment are not the same as the other. 也可能是一个环境中的权限与另一个环境中的权限不同。

尝试在DLL上运行依赖项遍历器 -查看是否缺少任何模块。

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

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