fooleryang 2022-04-02 10:42 采纳率: 0%
浏览 169
已结题

c# GetProcAddress返回0,GetLastWin32Error返回127,提示找不到指定的程序

问题遇到的现象和发生背景

使用c#测试动态加载托管dll,通过LoadLibrary可以加载到dll,但是GetProcAddress加载函数时返回0,错误代码为 127,错误信息为 找不到指定的程序
但是通过实例化dll中的对象,却可以使用函数

问题相关代码,请勿粘贴截图

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using ClassLibrary1;

namespace ClassLibrary1
{
class Program
{
[DllImport("kernel32", EntryPoint = "LoadLibrary", SetLastError = true)]
public static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32", EntryPoint = "GetProcAddress", SetLastError = true)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
[DllImport("kernel32", EntryPoint = "FreeLibrary", SetLastError = true)]
static extern bool FreeLibrary(IntPtr hModule);
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
public extern static int FormatMessage(int flag, ref IntPtr source, int msgid, int langid, ref string buf, int size, ref IntPtr args);
static void Main(string[] args)
{
string path = @"E:\C#P\DLLTest01\ClassLibrary1\bin\Debug\ClassLibrary1.dll";
//使用GetProcAddress无法使用
IntPtr handel = Test.LoadLibrary(path);//有值 95354880
IntPtr addr = Test.GetProcAddress(handel, "mess");//addr = 0
if (addr == IntPtr.Zero)
{
int errCode = Marshal.GetLastWin32Error();//127
IntPtr tempptr = IntPtr.Zero;
string msg = null;
FormatMessage(0x1300, ref tempptr, errCode, 0, ref msg, 255, ref tempptr);//找不到指定的程序
}
//创建对象可以
Class1 c = new Class1();
c.mess("hello");//弹框
}
}
}

dll中的代码
using System;
using System.Collections.Generic;

using System.Text;
using System.Windows.Forms;

namespace ClassLibrary1
{
public class Class1
{
public void mess(string s)
{
MessageBox.Show(s);
}
}
}

运行结果及报错内容

img

我的解答思路和尝试过的方法

通过dumpbin解析dll,发现没有函数名

img


但是在vs对象查看器中可以看到mess

img

我想要达到的结果
  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2022-04-02 10:49
    关注

    你确认一下动态库路径是否正确呢

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月23日
  • 创建了问题 4月2日

悬赏问题

  • ¥20 这张图页脚大概具体代码该怎么写?
  • ¥20 WPF MVVM模式 handycontrol 框架, hc:SearchBar 控件 Text="{Binding NavMenusKeyWords}" 绑定取不到值
  • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂
  • ¥15 数字信号处理考试111
  • ¥15 allegro17.2生成bom表是空白的
  • ¥15 请问一下怎么打通CAN通讯
  • ¥20 如何在 rocky9.4 部署 CDH6.3.2?
  • ¥35 navicat将excel中的数据导入mysql出错
  • ¥15 rt-thread线程切换的问题
  • ¥15 高通uboot 打印ubi init err 22