我有一个C#应用程序,需要从C ++ dll导入函数。我使用DLLImport加载函数。它在英语和中文环境中都能正常工作,但在法语操作系统中始终会引发“找不到模块”异常。

代码快照:

[DllImport("abcdef.dll", CallingConvention = CallingConvention.StdCall)]
public static extern string Version();


请注意,模块名称中的字母已被替换,但名称本身具有相同的格式和相同的长度。

和截图:


有任何想法吗?

最佳答案

我已经尝试了您提出的所有方法,但是问题仍然存在。

这是可以避免该错误的另一种方法。它使用Win32 API加载库并找到函数的地址,然后调用它。演示代码如下:

class Caller
{
    [DllImport("kernel32.dll")]
    private extern static IntPtr LoadLibrary(String path);
    [DllImport("kernel32.dll")]
    private extern static IntPtr GetProcAddress(IntPtr lib, String funcName);
    [DllImport("kernel32.dll")]
    private extern static bool FreeLibrary(IntPtr lib);

    private IntPtr _hModule;

    public Caller(string dllFile)
    {
        _hModule = LoadLibrary(dllFile);
    }

    ~Caller()
    {
        FreeLibrary(_hModule);
    }

    delegate string VersionFun();

    int main()
    {
        Caller caller = new Caller("abcdef.dll");
        IntPtr hFun = GetProcAddress(_hModule, "Version");
        VersionFun fun = Marshal.GetDelegateForFunctionPointer(hFun, typeof(VersionFun)) as VersionFun;
        Console.WriteLine(fun());

        return 0;
    }
}

关于c# - DLLImport找不到DLL文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15898458/

10-13 06:29