开发者

How to create exported functions in Mono.Cecil?

开发者 https://www.devze.com 2023-04-02 00:02 出处:网络
What am I doing wrong? I\'m trying to create an exported function of LoadLibraryA and inject it into an assembly.

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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号