Frage zu dnlib

  • C#
  • .NET (FX) 4.0

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von tolio.

    Frage zu dnlib

    Hi

    Ich habe mir jetzt mal dnlib heruntergeladen und erzeuge mit diesem Code ein "Hallo-Welt"-Programm:

    C-Quellcode

    1. // Create a new module. The string passed in is the name of the module,
    2. // not the file name.
    3. ModuleDef mod = new ModuleDefUser("MyModule.exe");
    4. // It's a console application
    5. mod.Kind = ModuleKind.Console;
    6. // Add the module to an assembly
    7. Byte[] b = new Byte[3];
    8. b[0] = (byte)17;
    9. b[1] = (byte)36;
    10. b[2] = (byte)24;
    11. UTF8String uts = new UTF8String(b);
    12. AssemblyDef asm = new AssemblyDefUser("MyAssembly", new Version(1, 2, 3, 4), null, uts);
    13. asm.Modules.Add(mod);
    14. // Add a .NET resource
    15. byte[] resourceData = Encoding.UTF8.GetBytes("Hello, world!");
    16. mod.Resources.Add(new EmbeddedResource("My.Resource", resourceData,
    17. ManifestResourceAttributes.Private));
    18. // Add the startup type. It derives from System.Object.
    19. TypeDef startUpType = new TypeDefUser("MyAssembly", "Program", mod.CorLibTypes.Object.TypeDefOrRef);
    20. startUpType.Attributes = TypeAttributes.Public | TypeAttributes.AutoLayout |
    21. TypeAttributes.Class | TypeAttributes.AnsiClass;
    22. // Add the type to the module
    23. mod.Types.Add(startUpType);
    24. // Create the entry point method
    25. MethodDef entryPoint = new MethodDefUser("Main",
    26. MethodSig.CreateStatic(mod.CorLibTypes.Int32, new SZArraySig(mod.CorLibTypes.String)));
    27. entryPoint.Attributes = MethodAttributes.Private | MethodAttributes.Static |
    28. MethodAttributes.HideBySig | MethodAttributes.ReuseSlot;
    29. entryPoint.ImplAttributes = MethodImplAttributes.IL | MethodImplAttributes.Managed;
    30. // Name the 1st argument (argument 0 is the return type)
    31. entryPoint.ParamDefs.Add(new ParamDefUser("args", 1));
    32. // Add the method to the startup type
    33. startUpType.Methods.Add(entryPoint);
    34. // Set module entry point
    35. mod.EntryPoint = entryPoint;
    36. // Create a TypeRef to System.Console
    37. TypeRef consoleRef = new TypeRefUser(mod, "System", "Console", mod.CorLibTypes.AssemblyRef);
    38. // Create a method ref to 'System.Void System.Console::WriteLine(System.String)'
    39. MemberRef consoleWrite1 = new MemberRefUser(mod, "WriteLine",
    40. MethodSig.CreateStatic(mod.CorLibTypes.Void, mod.CorLibTypes.String),
    41. consoleRef);
    42. // Add a CIL method body to the entry point method
    43. CilBody epBody = new CilBody();
    44. entryPoint.Body = epBody;
    45. epBody.Instructions.Add(OpCodes.Ldstr.ToInstruction("Hello World!"));
    46. epBody.Instructions.Add(OpCodes.Call.ToInstruction(consoleWrite1));
    47. epBody.Instructions.Add(OpCodes.Ldc_I4_0.ToInstruction());
    48. epBody.Instructions.Add(OpCodes.Ret.ToInstruction());
    49. mod.Write(@"C:\Users\nico\Desktop\MyAssembly.exe");


    Mein Problem: Das Programm wird zwar kompiliert, aber wenn man es startet kommt da ein Schild wo steht: "MyProgram funktioniert nicht mehr". Ich kann das Programm aber in ILSpy öffnen. Ich krieg dann diesen Code in ILSpy:

    C#-Quellcode

    1. using System;
    2. public class Program
    3. {
    4. public static int Main(string[] args)
    5. {
    6. Console.WriteLine("Hello World!");
    7. return 0;
    8. }
    9. }


    Müsste meiner Meinung eigentlich funktionieren.
    Wisst ihr wo der Fehler ist?

    mfg