加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#GetProcAddress返回零

发布时间:2020-12-16 03:14:08 所属栏目:百科 来源:网络整理
导读:由于某些原因,每当我的C#.NET 2.0应用程序调用GetProcAddress,它总是返回零. public class MyClass{ internal static class UnsafeNativeMethods { [DllImport("kernel32.dll",CharSet = CharSet.Auto,SetLastError = true)] internal static extern IntPtr
由于某些原因,每当我的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",SetLastError = true)]
        internal static extern bool SetDllDirectory(string lpPathName);

        [DllImport("kernel32.dll",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总是被赋值为非零值.您可能能够提供的任何见解表示赞赏.谢谢.

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读