plc.myforum.ro Forum Index plc.myforum.ro
PLCs users www.plc.myforum.ro!
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to force .NET application to run in 32bit process on 64b

 
Post new topic   Reply to topic    plc.myforum.ro Forum Index -> Programming
View previous topic :: View next topic  
Author Message
rumsfeld
Member 3
Member 3


Joined: 29 Oct 2011
Posts: 572

PostPosted: 09/02/2013 23:07    Post subject: How to force .NET application to run in 32bit process on 64b Reply with quote

How to force .NET application to run in 32bit process on 64bitOS
The problem is to find a possibility to force a .NET (command line) tool to run as a 32bit process on a 64bit OS (e.g. Windows Server 2003/2008).
The simplest solution is to set the Platform Target to be x86 on the project properties (build register).
How to force .NET application to run in 32bit process on 64b How to force .NET application to run in 32bit process on 64b
This will cause the compiler to set the 32Bit flag in the CLR header of the corresponding assembly. Every time we run this application no matter on what type of OS it will execute as a 32bit process. But this solution although simple and straight forward was not a viable solution for us since – as mentioned above – we want to have one and only one version of our product. Thus all components of our package have to be compiled with Platform Target set to Any CPU.

Run in the context of IIS
If the application is running in the context of IIS then we can instruct IIS to run the application in a 32bit process. Of course this solution is not the right one for a command line tool…

To run a web application in 32bit mode on an 64bit OS open the IIS Manager and navigate to the application pool that the application should run in. Select the application pool and select “Advance Settings…”. In the Advanced Settings dialog set the “Enable 32-Bit Applications” to true.
How to force .NET application to run in 32bit process on 64b How to force .NET application to run in 32bit process on 64b

Use corflags.exe to set the bitness

You can flip the 32-bit flag on the assembly after-the-fact using a tool called CorFlags.exe, but that’s probably not a good idea for a long-term sustainable option. For the details about how to use CorFlags.exe see this link. E.g. to set the 32Bit flag open the Visual Studio command prompt, navigate to the directory where your assembly is and use this command

Code:
CorFlags.exe MyAssembly.exe /32Bit+


and to turn off the 32Bit flag it would be

Code:
CorFlags.exe MyAssembly.exe /32Bit-


Create a shallow wrapper application

Code:
public class Program

{

    public static int Main(string[] args)

    {

        // ...

    }

   

    // more code

}


Our wrapper application can now reference the wrapped application and execute it by using this code

Code:
class Program32

{

    static int Main(string[] args)

    {

        return Program.Main(args);

    }

}


Since the “bitness” of the starting assembly determines the “bitness” of the process the whole application will run in a 32bit process.

But what if we could create a generic 32bit wrapper application that can call any (command line) tool and force it to run in a 32bit process?
Here is....
Code:
RunAs32Bit.exe /assembly:MyAssembly.exe /params:"/connection:Oracle1 /u...


Code:
static int Main(string[] args)

{

    // load the assembly

    var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

    var assemblyName = Path.Combine(directory, args[0].Substring(10));

    var assembly = Assembly.LoadFile(assemblyName);

 

    // find the entry point of the assembly

    var type = assembly.GetTypes().FirstOrDefault(t => t.GetMethod("Main") != null);

    var mi = type.GetMethod("Main");

 
    // call the entry point of the wrapped assembly and forward the command line parameters

    var arguments = args[1].Substring(8).Split(' ');

    var list = arguments.Where(a => a.Trim() != "").ToArray();

    return (int)mi.Invoke(type, new object[] { list });

}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    plc.myforum.ro Forum Index -> Programming All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Fa-ti si tu propriul forum GRATUIT, pe www.myforum.ro