由於某種原因,每當我的C#.NET 2.0應用程序調用GetProcAddress
時,它總是返回零。C#GetProcAddress返回零
public class MyClass
{
internal static class UnsafeNativeMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SetDllDirectory(string lpPathName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
}
private void MyFunc()
{
IntPtr _dllHandle;
IntPtr _fptr;
string _fullPath = ".\\mydll.dll";
string _procName = "MyDllFunc";
_dllHandle = UnsafeNativeMethods.LoadLibrary(_fullPath);
_fptr = UnsafeNativeMethods.GetProcAddress(_dllHandle, _procName); // <-- Always returns zero.
}
}
我敢肯定,函數名拼寫正確,並_fullPath
大概是正確的,因爲_dllHandle
總是被分配一個非零值。任何你可能能夠提供的洞察力都是值得讚賞的。謝謝。
+1,我在看到它之前就已經明白了。 – 2010-09-20 18:53:29
這個伎倆!謝謝! – 2010-09-20 20:56:14
+1我一直暗示着我的頭靠在牆上幾個小時,直到谷歌送我到這裏!畢竟這只是將** CharSet = CharSet.Ansi **添加到** DllImport **屬性的命名參數才能使其工作的問題。 – 2013-04-26 22:24:08