C# 在Vista和XP中,LoadLibrary函数返回null
我的应用程序在Win7 x86和x64测试环境下运行良好,但在Vista和XP上则不行 以下是示例代码:C# 在Vista和XP中,LoadLibrary函数返回null,c#,.net,C#,.net,我的应用程序在Win7 x86和x64测试环境下运行良好,但在Vista和XP上则不行 以下是示例代码: internal static class NativeMethods { [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool Se
internal static class NativeMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetDllDirectory(string lpPathName);
[DllImport("kernel32", SetLastError = true)]
internal static extern IntPtr LoadLibrary(string lpFileName);
}
....
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Native");
path = Path.Combine(path, string.Format("x{0}", IntPtr.Size*8));
NativeMethods.SetDllDirectory(path);
IntPtr ptr2 = NativeMethods.LoadLibrary("SQLite.Interop.dll");
在Vista或XP中,无论x86还是x64,IntPtr==null
在.net Framework 2 x86下的解决方案构建目标
有没有办法解决这个问题
IntPtr ptr2 = NativeMethods.LoadLibrary("SQLite.Interop.dll");
这不是本机库,而是SQLite的.NET互操作库。它包含托管类声明。您需要使用Assembly.LoadFrom()来加载它。尽管您几乎总是喜欢将它添加为项目的引用,但这样编写代码要容易得多。您可能仍然需要SetDllDirectory(),因为SQLite的核心是用C编写的非托管代码,所以包装器可能需要帮助才能找到SQLite.dll
不确定为什么要这样做,可能与64位操作系统有关。请记住,这是一个部署问题,而不是编程问题。您是否尝试将.dll添加到DllImport指令中的库名称中?您是否检查了
path
是否正确?显然,您正在向路径中添加x32
或x64
,但在XP或Vista中可能不正确。安装到的SQLite.Interop.dll
在哪里?是的,我检查了它,路径是正确的。Marshal.GetLastWin32Error()
说什么?@guga你能给我一个例子吗。