Hi
Ich habe mir jetzt mal dnlib heruntergeladen und erzeuge mit diesem Code ein "Hallo-Welt"-Programm:
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:
Müsste meiner Meinung eigentlich funktionieren.
Wisst ihr wo der Fehler ist?
mfg
Ich habe mir jetzt mal dnlib heruntergeladen und erzeuge mit diesem Code ein "Hallo-Welt"-Programm:
C-Quellcode
- // Create a new module. The string passed in is the name of the module,
- // not the file name.
- ModuleDef mod = new ModuleDefUser("MyModule.exe");
- // It's a console application
- mod.Kind = ModuleKind.Console;
- // Add the module to an assembly
- Byte[] b = new Byte[3];
- b[0] = (byte)17;
- b[1] = (byte)36;
- b[2] = (byte)24;
- UTF8String uts = new UTF8String(b);
- AssemblyDef asm = new AssemblyDefUser("MyAssembly", new Version(1, 2, 3, 4), null, uts);
- asm.Modules.Add(mod);
- // Add a .NET resource
- byte[] resourceData = Encoding.UTF8.GetBytes("Hello, world!");
- mod.Resources.Add(new EmbeddedResource("My.Resource", resourceData,
- ManifestResourceAttributes.Private));
- // Add the startup type. It derives from System.Object.
- TypeDef startUpType = new TypeDefUser("MyAssembly", "Program", mod.CorLibTypes.Object.TypeDefOrRef);
- startUpType.Attributes = TypeAttributes.Public | TypeAttributes.AutoLayout |
- TypeAttributes.Class | TypeAttributes.AnsiClass;
- // Add the type to the module
- mod.Types.Add(startUpType);
- // Create the entry point method
- MethodDef entryPoint = new MethodDefUser("Main",
- MethodSig.CreateStatic(mod.CorLibTypes.Int32, new SZArraySig(mod.CorLibTypes.String)));
- entryPoint.Attributes = MethodAttributes.Private | MethodAttributes.Static |
- MethodAttributes.HideBySig | MethodAttributes.ReuseSlot;
- entryPoint.ImplAttributes = MethodImplAttributes.IL | MethodImplAttributes.Managed;
- // Name the 1st argument (argument 0 is the return type)
- entryPoint.ParamDefs.Add(new ParamDefUser("args", 1));
- // Add the method to the startup type
- startUpType.Methods.Add(entryPoint);
- // Set module entry point
- mod.EntryPoint = entryPoint;
- // Create a TypeRef to System.Console
- TypeRef consoleRef = new TypeRefUser(mod, "System", "Console", mod.CorLibTypes.AssemblyRef);
- // Create a method ref to 'System.Void System.Console::WriteLine(System.String)'
- MemberRef consoleWrite1 = new MemberRefUser(mod, "WriteLine",
- MethodSig.CreateStatic(mod.CorLibTypes.Void, mod.CorLibTypes.String),
- consoleRef);
- // Add a CIL method body to the entry point method
- CilBody epBody = new CilBody();
- entryPoint.Body = epBody;
- epBody.Instructions.Add(OpCodes.Ldstr.ToInstruction("Hello World!"));
- epBody.Instructions.Add(OpCodes.Call.ToInstruction(consoleWrite1));
- epBody.Instructions.Add(OpCodes.Ldc_I4_0.ToInstruction());
- epBody.Instructions.Add(OpCodes.Ret.ToInstruction());
- 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:
Müsste meiner Meinung eigentlich funktionieren.
Wisst ihr wo der Fehler ist?
mfg