What am I doing wrong? I'm trying to create an exported function of LoadLibraryA and inject it into an assembly.
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 ParameterDefini开发者_C百科tion("name", Mono.Cecil.ParameterAttributes.None, stringType));
Don't worry, I found the solution. The entry point is suppose to be LoadLibrary
not LoadLibraryA
. I wonder why I did that.....
Also I didn't do this:
loadLibraryA.IsPreserveSig = true;
The charset also needs to be unicode not ansi...
精彩评论