【发布时间】:2011-08-31 10:55:15
【问题描述】:
我做错了什么?我正在尝试创建 LoadLibraryA 的导出函数并将其注入程序集。
TypeReference stringType = asm.MainModule.Import(typeof(String));
TypeReference nativeIntType = asm.MainModule.Import(typeof(IntPtr));
ModuleReference kernel32Ref = new ModuleReference("kernel32");
asm.MainModule.ModuleReferences.Add(kernel32Ref);
MethodDefinition loadLibraryA = new MethodDefinition("LoadLibraryA", Mono.Cecil.MethodAttributes.Public |
Mono.Cecil.MethodAttributes.HideBySig | Mono.Cecil.MethodAttributes.Static |
Mono.Cecil.MethodAttributes.PInvokeImpl, nativeIntType);
loadLibraryA.PInvokeInfo = new PInvokeInfo(PInvokeAttributes.NoMangle | PInvokeAttributes.CharSetAnsi
| PInvokeAttributes.SupportsLastError | PInvokeAttributes.CallConvWinapi, "LoadLibraryA", kernel32Ref);
loadLibraryA.Parameters.Add(new ParameterDefinition("name", Mono.Cecil.ParameterAttributes.None, stringType));
【问题讨论】:
-
如果您不说出问题所在,我们就无法知道问题所在?有什么错误?你有例外吗?还是 pinvoke 不在生成的程序集中?
-
@Jb Evain 当我将它加载到另一个应用程序中作为参考时,异常是 FileNotFoundException。即使现在我修复了它,但在另一个程序集中使用它时仍然显示异常,但它在内部工作。
-
另外,您可能希望使用 kernel32.dll 作为模块引用,而不是单独使用 kernel32。
标签: c# mono mono.cecil kernel32