문제

I've been having some issues with static constructors with my project. I need to add a static constructor to the type "" in order to call my resource decryption method.

Below in the gif you will see the issue I run into.

I will also include the code snippet. enter image description here

Code for creating cctor:

MethodDefinition method = new MethodDefinition(
    ".cctor",
    Mono.Cecil.MethodAttributes.Private
    | Mono.Cecil.MethodAttributes.Static
    | Mono.Cecil.MethodAttributes.HideBySig
    | Mono.Cecil.MethodAttributes.SpecialName
    | Mono.Cecil.MethodAttributes.RTSpecialName,
    mod.Import(typeof(void))
); 

I have also tried changing the attributes to the exact same as Yano's. It somehow never works. By "works" I mean detect it as a static constructor in DotNet Resolver.

Here's some more information about real outcome and expected result.

enter image description here

I do not have an ResolveEventHandler attached to my entrypoint. I have it attached to the application, which is being obfuscated and it is located in the "" type's static constructor which is executed before even the entrypoint is called.

Application resources have been encrypted with AES and are not recognised as valid resources by dotnet resolver or other decompilers. I am simply asking why the event is not being triggered as it should be triggered when a resource is invalid or missing. As you can see in the example a messagebox should pop up before the application is launched but it never does (string encryption encrypts the strings, so its a bit hard to see theres a string there).

Any help is appreciated.

도움이 되었습니까?

해결책

use this :

void AddConstructor(TypeDefinition type, MethodReference baseEmptyConstructor)
{
    var methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
    var method = new MethodDefinition(".ctor", methodAttributes, ModuleDefinition.TypeSystem.Void);
    method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
    method.Body.Instructions.Add(Instruction.Create(OpCodes.Call, baseEmptyConstructor));
    method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
    type.Methods.Add(method);
}

you can also refer :

http://www.mono-project.com/Cecil:FAQ

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top