How to avoid "System.BadImageFormatException: 'Could not load file or assembly '\..bin\test.exe' or one of its dependencies. The module was expected t

  • Thread starter Thread starter waqasm78
  • Start date Start date
W

waqasm78

Guest
I am compiling the C++ code dynamically from C# using the bat file.

call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvars32.bat"
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\bin\Hostx86\x86\cl.exe" /EHsc D:\test\test.cpp /FoD:\Test\ /link /out:D:\test\test.exe

it generates the exe file, when I run the exe using AppDomain


var appId = args[0];
var binPath = args[1];
var sandboxPath = args[2];
var binFileName = args[3];

System.AppDomain appDomain = null;

try
{
var setup = new AppDomainSetup {ApplicationBase = binPath};

// PERMISSION
var permissionSet = new PermissionSet(PermissionState.None);
permissionSet.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, binPath));
permissionSet.AddPermission(new FileIOPermission(FileIOPermissionAccess.AllAccess, sandboxPath));
permissionSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
permissionSet.AddPermission(new UIPermission(PermissionState.Unrestricted));


appDomain = System.AppDomain.CreateDomain(appId, null, setup, permissionSet);

// EXECUTE
{
var result = appDomain.ExecuteAssembly(binFileName, new string[0]);
}
}
finally
{
if (appDomain != null)
{
System.AppDomain.Unload(appDomain);
}
}




It throws the following exception.

"System.BadImageFormatException: 'Could not load file or assembly '\..bin\test.exe' or one of its dependencies. The module was expected to contain an assembly manifest.'

Any possible solution?

Continue reading...
 
Back
Top