C#GetProcAddress返回零

Jim*_*ell 13 c# dll getprocaddress kernel32

出于某种原因,每当我的C#.NET 2.0应用程序调用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.
    }
}
Run Code Online (Sandbox Code Playgroud)

我确定函数名称拼写正确,并且_fullPath可能是正确的,因为_dllHandle总是分配一个非零值.您可能提供的任何见解表示赞赏.谢谢.

Nic*_*ckD 16

GetProcAddress只有ANSI风格,因此我们通过告诉它在编组字符串参数时始终使用ANSI来帮助运行时.我们还阻止运行时查找不存在的GetProcAddressA,因为C#的默认设置是将ExactSpelling设置为false.

http://www.pinvoke.net/default.aspx/kernel32.getprocaddress