C# 无法在我的项目中加载dll文件

C# 无法在我的项目中加载dll文件,c#,dll,C#,Dll,在我的项目中,我无法加载dll文件 dll文件-->平台目标为x86 我的项目-->平台目标是Anycpu 机器正在运行64位操作系统 这就是我使用的代码 pDll = NativeMethods.LoadLibrary(dllname); // some code here static class NativeMethods { [DllImport("kernel32.dll")] public static extern IntPtr LoadLibrary(strin

在我的项目中,我无法加载dll文件

dll文件-->平台目标为x86

我的项目-->平台目标是Anycpu

机器正在运行64位操作系统

这就是我使用的代码

 pDll = NativeMethods.LoadLibrary(dllname);
// some code here
static class NativeMethods
{
    [DllImport("kernel32.dll")]
    public static extern IntPtr LoadLibrary(string dllToLoad);

    [DllImport("kernel32.dll")]
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

    [DllImport("kernel32.dll")]
    public static extern bool FreeLibrary(IntPtr hModule);
}
为什么我无法加载此dll文件


抱歉我的英语不好

假设其他一切都正常(DLL名称等):

你所描述的是一个已知的问题。。。例如,如果您的DLL为32位,而您使用的操作系统为64位,则您的.NET应用程序将以64位模式运行

最简单的解决方案:如果DLL仅在x86中可用,则需要为x86而不是任何CPU编译

否则,您将需要该DLL的64位版本,然后您将遇到称为“并排汇编”的问题。。。我想你会发现这些很有帮助:


您如何知道无法加载DLL文件?您的示例还不够-请将静态字符串设置为常量,而不是
dllname
,这样可以清楚地看出参数本身的值没有问题,还可以添加例外信息。