本文整理汇总了C#中ImageFileMachine类的典型用法代码示例。如果您正苦于以下问题:C# ImageFileMachine类的具体用法?C# ImageFileMachine怎么用?C# ImageFileMachine使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ImageFileMachine类属于命名空间,在下文中一共展示了ImageFileMachine类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EmitHelloWorldConsoleExeAssembly
private static byte[] EmitHelloWorldConsoleExeAssembly(PortableExecutableKinds peKind, ImageFileMachine machine)
{
byte[] bytes;
var asmName = new AssemblyName { Name = "Dummy" + Guid.NewGuid() };
var asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
asmName,
System.Reflection.Emit.AssemblyBuilderAccess.Save);
var modBuilder = asmBuilder.DefineDynamicModule(asmName.Name, asmName.Name + ".exe");
var programTypeBuilder = modBuilder.DefineType("Program");
var mainMethodBuilder = programTypeBuilder.DefineMethod("Main", MethodAttributes.Static, typeof(void), Type.EmptyTypes);
var il = mainMethodBuilder.GetILGenerator();
il.EmitWriteLine("Hello, World!");
il.Emit(OpCodes.Ret);
asmBuilder.SetEntryPoint(mainMethodBuilder);
programTypeBuilder.CreateType();
asmBuilder.Save(asmName.Name + ".exe", peKind, machine);
try
{
bytes = File.ReadAllBytes(asmName.Name + ".exe");
}
finally
{
File.Delete(asmName.Name);
}
return bytes;
}
示例2: WriteModule
internal static void WriteModule(StrongNameKeyPair keyPair, byte[] publicKey, ModuleBuilder moduleBuilder,
PEFileKinds fileKind, PortableExecutableKinds portableExecutableKind, ImageFileMachine imageFileMachine,
ResourceSection resources, int entryPointToken, Stream stream)
{
if (stream == null)
{
string fileName = moduleBuilder.FullyQualifiedName;
bool mono = System.Type.GetType("Mono.Runtime") != null;
if (mono)
{
try
{
// Mono mmaps the file, so unlink the previous version since it may be in use
File.Delete(fileName);
}
catch { }
}
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
WriteModuleImpl(keyPair, publicKey, moduleBuilder, fileKind, portableExecutableKind, imageFileMachine, resources, entryPointToken, fs);
}
// if we're running on Mono, mark the module as executable by using a Mono private API extension
if (mono)
{
File.SetAttributes(fileName, (FileAttributes)(unchecked((int)0x80000000)));
}
}
else
{
WriteModuleImpl(keyPair, publicKey, moduleBuilder, fileKind, portableExecutableKind, imageFileMachine, resources, entryPointToken, stream);
}
}
示例3: AssemblyGen
internal AssemblyGen(AssemblyName name, string outDir, string outFileExtension, bool isDebuggable,
PortableExecutableKinds peKind, ImageFileMachine machine) {
ContractUtils.RequiresNotNull(name, "name");
#if SILVERLIGHT // AssemblyBuilderAccess.RunAndSave, Environment.CurrentDirectory
_myAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
_myModule = _myAssembly.DefineDynamicModule(name.Name, isDebuggable);
#else
if (outFileExtension == null) {
outFileExtension = ".dll";
}
if (outDir != null) {
try {
outDir = Path.GetFullPath(outDir);
} catch (Exception) {
throw Error.InvalidOutputDir();
}
try {
Path.Combine(outDir, name.Name + outFileExtension);
} catch (ArgumentException) {
throw Error.InvalidAsmNameOrExtension();
}
_outFileName = name.Name + outFileExtension;
_outDir = outDir;
}
// mark the assembly transparent so that it works in partial trust:
CustomAttributeBuilder[] attributes = new CustomAttributeBuilder[] {
new CustomAttributeBuilder(typeof(SecurityTransparentAttribute).GetConstructor(Type.EmptyTypes), new object[0])
};
if (outDir != null) {
#if CLR4
_myAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.RunAndSave, outDir, false, attributes);
#else
//The API DefineDynamicAssembly is obsolete in Dev10.
_myAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.RunAndSave, outDir,
null, null, null, null, false, attributes);
#endif
_myModule = _myAssembly.DefineDynamicModule(name.Name, _outFileName, isDebuggable);
} else {
_myAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run, attributes);
_myModule = _myAssembly.DefineDynamicModule(name.Name, isDebuggable);
}
_myAssembly.DefineVersionInfoResource();
#endif
_machine = machine;
_peKind = peKind;
_isDebuggable = isDebuggable;
if (isDebuggable) {
SetDebuggableAttributes();
}
}
示例4: WriteModule
internal static void WriteModule(StrongNameKeyPair keyPair, byte[] publicKey, ModuleBuilder moduleBuilder,
PEFileKinds fileKind, PortableExecutableKinds portableExecutableKind, ImageFileMachine imageFileMachine,
ResourceSection resources, int entryPointToken, Stream stream)
{
if (stream == null)
{
using (FileStream fs = new FileStream(moduleBuilder.FullyQualifiedName, FileMode.Create))
{
WriteModuleImpl(keyPair, publicKey, moduleBuilder, fileKind, portableExecutableKind, imageFileMachine, resources, entryPointToken, fs);
}
}
else
{
WriteModuleImpl(keyPair, publicKey, moduleBuilder, fileKind, portableExecutableKind, imageFileMachine, resources, entryPointToken, stream);
}
}
示例5: EmitLibraryAssembly
private static byte[] EmitLibraryAssembly(PortableExecutableKinds peKind, ImageFileMachine machine)
{
byte[] bytes;
var asmName = new AssemblyName { Name = "Dummy" + Guid.NewGuid() };
var asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
asmName,
System.Reflection.Emit.AssemblyBuilderAccess.Save);
asmBuilder.Save(asmName.Name, peKind, machine);
try
{
bytes = File.ReadAllBytes(asmName.Name);
}
finally
{
File.Delete(asmName.Name);
}
return bytes;
}
示例6: CompilerOptions
public CompilerOptions(){
this.autoRef = true;
this.PEFileKind = PEFileKinds.ConsoleApplication;
this.PEKindFlags = PortableExecutableKinds.ILOnly;
this.PEMachineArchitecture = ImageFileMachine.I386;
this.fFast = true;
this.fPrint = true;
this.nWarningLevel = 4;
this.SourceFileNames = new ArrayList();
this.ImportFileNames = new ArrayList();
this.ManagedResourceFileNames = new Hashtable(10);
this.ManagedResources = new Hashtable(10);
this.Defines = new Hashtable();
string libpath = System.Environment.GetEnvironmentVariable("LIB");
if (libpath != null)
this.libpath = libpath;
else
this.libpath = "";
}
示例7: AssemblyGen
public AssemblyGen(string moduleName, string outDir, string outFile, bool emitDebugInfo,
bool staticTypes, PortableExecutableKinds peKind, ImageFileMachine machine)
{
this.outFileName = outFile;
this.outDir = outDir;
this.emitDebugInfo = emitDebugInfo;
this.staticTypes = staticTypes;
this.peKind = peKind;
this.machine = machine;
AssemblyName asmname = new AssemblyName();
AppDomain domain = System.Threading.Thread.GetDomain();
asmname.Name = Path.GetFileNameWithoutExtension(outFileName);
if (outFileName == null) {
myAssembly = domain.DefineDynamicAssembly(
asmname,
AssemblyBuilderAccess.Run,
outDir,
null);
myModule = myAssembly.DefineDynamicModule(moduleName);
} else {
myAssembly = domain.DefineDynamicAssembly(
asmname,
AssemblyBuilderAccess.RunAndSave,
outDir,
null);
myModule = myAssembly.DefineDynamicModule(outFileName, outFileName, emitDebugInfo);
}
myAssembly.DefineVersionInfoResource();
if (emitDebugInfo) SetDebuggableAttributes();
}
示例8: GetPEKind
public override void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine)
{
ModuleHandle.GetPEKind(GetNativeHandle(), out peKind, out machine);
}
示例9: Save
[System.Security.SecurityCritical] // auto-generated
internal void Save(String fileName, bool isAssemblyFile, PortableExecutableKinds portableExecutableKind,
ImageFileMachine imageFileMachine)
{
// This is a helper called by AssemblyBuilder save to save information for the persistable modules.
if (m_moduleData.m_embeddedRes != null)
{
// There are embedded resources for this module
ResWriterData resWriter;
// Add each resource content into the to be saved PE file
for (resWriter = m_moduleData.m_embeddedRes; resWriter != null; resWriter = resWriter.m_nextResWriter)
{
if (resWriter.m_resWriter != null)
resWriter.m_resWriter.Generate();
byte[] resBytes = new byte[resWriter.m_memoryStream.Length];
resWriter.m_memoryStream.Flush();
resWriter.m_memoryStream.Position = 0;
resWriter.m_memoryStream.Read(resBytes, 0, resBytes.Length);
AddResource(GetNativeHandle(),
resWriter.m_strName,
resBytes,
resBytes.Length,
m_moduleData.FileToken,
(int)resWriter.m_attribute,
(int)portableExecutableKind,
(int)imageFileMachine);
}
}
DefineNativeResource(portableExecutableKind, imageFileMachine);
PEFileKinds pekind = isAssemblyFile ? ContainingAssemblyBuilder.m_assemblyData.m_peFileKind : PEFileKinds.Dll;
SavePEFile(GetNativeHandle(), fileName, m_EntryPoint.Token, (int)pekind, isAssemblyFile);
m_moduleData.m_isSaved = true;
}
示例10: DefineNativeResource
[System.Security.SecurityCritical] // auto-generated
internal void DefineNativeResource(PortableExecutableKinds portableExecutableKind, ImageFileMachine imageFileMachine)
{
string strResourceFileName = m_moduleData.m_strResourceFileName;
byte[] resourceBytes = m_moduleData.m_resourceBytes;
if (strResourceFileName != null)
{
DefineNativeResourceFile(GetNativeHandle(),
strResourceFileName,
(int)portableExecutableKind, (int)imageFileMachine);
}
else
if (resourceBytes != null)
{
DefineNativeResourceBytes(GetNativeHandle(),
resourceBytes, resourceBytes.Length,
(int)portableExecutableKind, (int)imageFileMachine);
}
}
示例11: GetPEKind
public override void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine)
{
peKind = 0;
if ((cliHeader.Flags & CliHeader.COMIMAGE_FLAGS_ILONLY) != 0)
{
peKind |= PortableExecutableKinds.ILOnly;
}
switch (cliHeader.Flags & (CliHeader.COMIMAGE_FLAGS_32BITREQUIRED | CliHeader.COMIMAGE_FLAGS_32BITPREFERRED))
{
case CliHeader.COMIMAGE_FLAGS_32BITREQUIRED:
peKind |= PortableExecutableKinds.Required32Bit;
break;
case CliHeader.COMIMAGE_FLAGS_32BITREQUIRED | CliHeader.COMIMAGE_FLAGS_32BITPREFERRED:
peKind |= PortableExecutableKinds.Preferred32Bit;
break;
default:
// COMIMAGE_FLAGS_32BITPREFERRED by itself is illegal, so we ignore it
// (not setting any flag is ok)
break;
}
if (peFile.OptionalHeader.Magic == IMAGE_OPTIONAL_HEADER.IMAGE_NT_OPTIONAL_HDR64_MAGIC)
{
peKind |= PortableExecutableKinds.PE32Plus;
}
machine = (ImageFileMachine)peFile.FileHeader.Machine;
}
示例12: AssemblyGen
public AssemblyGen(string moduleName,
string outDir,
string outFile,
AssemblyGenAttributes generationAttributes,
PortableExecutableKinds peKind,
ImageFileMachine machine)
{
Contract.Requires(!String.IsNullOrEmpty(moduleName), "moduleName", "Module name cannot be a null reference or an empty string.");
Contract.Requires(outFile != null || !SaveAndReloadAssemblies, "outFile", "SaveAssemblies mode requires non-null output file name.");
_genAttrs = generationAttributes;
AssemblyName asmname = new AssemblyName();
AppDomain domain = AppDomain.CurrentDomain; //System.Threading.Thread.GetDomain();
_machine = machine;
_peKind = peKind;
_outFileName = outFile;
#if SILVERLIGHT // AssemblyBuilderAccess.RunAndSave, Environment.CurrentDirectory
asmname.Name = moduleName;
_myAssembly = domain.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run);
_myModule = _myAssembly.DefineDynamicModule(moduleName, EmitDebugInfo);
#else
try {
outDir = Path.GetFullPath(String.IsNullOrEmpty(outDir) ? Environment.CurrentDirectory : outDir);
} catch (Exception e) {
throw new ArgumentException("Invalid output directory", e);
}
if (SaveAndReloadAssemblies
#if PEVERIFY
|| VerifyAssemblies
#endif
) {
_outDir = outDir;
}
if (moduleName == "ironscheme.boot.new")
{
_outDir = outDir = Path.Combine(outDir, "build");
_outFileName = "ironscheme.boot.dll";
}
// SymbolWriter fails on Mono for some reason
if (SaveAndReloadAssemblies) {
asmname.Name = moduleName == "ironscheme.boot.new" ? "ironscheme.boot" : moduleName;
if (File.Exists("DEVELOPMENT.snk"))
{
asmname.KeyPair = new StrongNameKeyPair(File.ReadAllBytes("DEVELOPMENT.snk"));
}
asmname.Version = new Version("1.0.0.0");
#pragma warning disable 0618
_myAssembly = domain.DefineDynamicAssembly(asmname, moduleName == "ironscheme.boot.new" ? AssemblyBuilderAccess.Save : AssemblyBuilderAccess.Save, outDir, null);
#pragma warning restore 0618
_myModule = _myAssembly.DefineDynamicModule( moduleName == "ironscheme.boot.new" ? "ironscheme.boot.dll" : _outFileName,
_outFileName, EmitDebugInfo);
} else {
asmname.Name = moduleName;
_myAssembly = domain.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run);
_myModule = _myAssembly.DefineDynamicModule(moduleName, EmitDebugInfo);
}
_myAssembly.DefineVersionInfoResource();
#endif
if (EmitDebugInfo) SetDebuggableAttributes();
}
示例13: CalculateProcArchIndex
internal static ProcessorArchitecture CalculateProcArchIndex(PortableExecutableKinds pek, ImageFileMachine ifm, AssemblyNameFlags flags)
{
if (((uint)flags & 0xF0) == 0x70)
return ProcessorArchitecture.None;
if ((pek & System.Reflection.PortableExecutableKinds.PE32Plus) == System.Reflection.PortableExecutableKinds.PE32Plus)
{
switch (ifm)
{
case System.Reflection.ImageFileMachine.IA64:
return ProcessorArchitecture.IA64;
case System.Reflection.ImageFileMachine.AMD64:
return ProcessorArchitecture.Amd64;
case System.Reflection.ImageFileMachine.I386:
if ((pek & System.Reflection.PortableExecutableKinds.ILOnly) == System.Reflection.PortableExecutableKinds.ILOnly)
return ProcessorArchitecture.MSIL;
break;
}
}
else
{
if (ifm == System.Reflection.ImageFileMachine.I386)
{
if ((pek & System.Reflection.PortableExecutableKinds.Required32Bit) == System.Reflection.PortableExecutableKinds.Required32Bit)
return ProcessorArchitecture.X86;
if ((pek & System.Reflection.PortableExecutableKinds.ILOnly) == System.Reflection.PortableExecutableKinds.ILOnly)
return ProcessorArchitecture.MSIL;
return ProcessorArchitecture.X86;
}
if (ifm == System.Reflection.ImageFileMachine.ARM)
{
return ProcessorArchitecture.Arm;
}
}
return ProcessorArchitecture.None;
}
示例14: GetPEKind
void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine) {
ModuleHandle.GetPEKind (out peKind, out machine);
}
示例15: GetPEKind
public virtual void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine)
{
throw new NotSupportedException();
}