Start 32bit process on 64bit OS - c#

My C# programm starts java application on 64bit OS (Windows). Java process is 64bit, how can i fix my launching code to start java process in 32bit?
My launching code:
ProcessStartInfo info = new ProcessStartInfo();
info.WorkingDirectory = ServerProperties.ServerWorkingDirectory;
info.FileName = "java"
info.Arguments = "some arguements"
ServerProcess = new Process();
ServerProcess.StartInfo = info;
ServerProcess.Start();
Thanks!

This is more a function of the process you're starting as opposed to the process that's starting it (i.e. the process that you're starting has to have been compiled/targeted to x86 as opposed to x86-64).
However, if both x86 and x86-64 java.exe are installed on the machine-in-question, you can probably hunt the x86 one down by looking in Program Files (x86) as opposed to Program Files.

If you host your application in IIS you need enable x86 processes for pool of web app. Follows next steps:
Select the app pool for your web app.
Click Advanced Settings under Edit Application Pool on the right.
Change the value of Enable 32-Bit Applications to True.
Check next article for details - Support of 32-bit applications in the 64-bit Windows environment

Related

How can I conditionally run my program in 64 bit or 32 bit mode based on external resources?

There are a few questions (and unwanted answers) all over the forums about Microsoft.ACE.OLEDB.12.0 provider not being registered on the local machine, like this one. The gist of the problem, how I understand it, is that the application will look for the provider on the same platform as what the application is running on. So if your computer is 64 bit and the provider 32 bit, then there will be a mismatch if you don't compile the application to run in 32 bit mode.
Most answers effectively deal with this by installing the appropriate data components for the current platform. Other suggestions are to compile for whichever platform the data components are available.
I am developing an app using PCs running Windows 7, 8 and 10, all 64 bit, depending on where I am, but some have older versions of Office and others newer versions. This causes me to have to change the platform for which I compile depending on the PC I currently work on. While this is no problem for me, personally, I foresee this causing headaches for the end users not being able to run the program.
Trying to avoid asking users to install other components on their computers; is there a way I can tell the program to check the platform availability of the database provider and then run in that mode? Might it be possible to create 32 bit and 64 bit extensions of the database module and load the appropriate one regardless of the mode the main program is running in?
EDIT:
I just tried to compile my database extensions on different platforms Whichever one that is not the same platform as the application causes an exception when being loaded saying that I am attempting to load an assembly from a different platform. So I guess I'm out of luck with my option 2...
You can use CorFlags utility to modify your executable on target machine, after you will detect which mode it needs to run.
First ensure that your main exe is compiled under any cpu with Prefer 32 bit flag not set. Now when application is started you need to check if we are in 64-bit process or not, and also check your dependencies (this I won't cover - I don't work with OLEDB). If you found mismatch (say you are running in 64-bit process but your dependencies are 32-bit) - you need to run external process to modify your main executable and then restart it. Easiest way to do it is via simple cmd script, like this (in this example my main exe is called ConsoleApplication3.exe):
:start
start /wait "" "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\CorFlags.exe" ConsoleApplication3.exe /32BIT+
if errorlevel 1 (
goto start
)
start "" ConsoleApplication3.exe
Note that this is just example and if something goes wrong it will fall into endless loop, adjust to your requirements. What this script does is just updates your exe using CorFlags tool to run in 32-bit mode, then starts your main exe.
Right after starting your application, you might do the following check:
static void Main() {
if (Environment.Is64BitProcess) {
// here you also check if you have dependency mismatch, and then:
Console.WriteLine("Running in 64-bit mode. Press any key to fix");
Console.ReadKey();
// run script (here we assume script is in the same directory as main executable, and is called fix-mode.cmd
var process = new Process() {
StartInfo = new ProcessStartInfo("cmd.exe", "/c call fix-mode.cmd")
};
process.Start();
// here you should exit your application immediatly, so that CorFlags can update it (since it cannot do that while it is running)
}
else {
// after restart by our script - we will get here
Console.WriteLine("Running in 32bit mode");
}
}
Note that this tool (CorFlags) is available only with Visual Studio so you may want to pack it together with your application (might be not available on remote machine).

I get 'A 32 bit processes cannot access modules of a 64 bit process.' exception invoking Process.Start()

Here is the code sample
var startInfo = new ProcessStartInfo
{
Arguments = commandStr,
FileName = #"C:\Windows\SysWOW64\logman.exe",
};
using (var createCounterProc = new Process { StartInfo = startInfo })
{
createCounterProc.Start();
createCounterProc.WaitForExit();
}
After running the code I get "A 32 bit processes cannot access modules of a 64 bit process." message in MainModule (NativeErrorCode:299). My solution is configured to AnyCPU. I've tried both 64 and 32 bit versions of logman.exe (C:\Windows\SysWOW64\logman.exe and C:\Windows\System32\logman.exe) but I still have the same error. My OS is Win8.1Prox64. What could cause the problem?
Stack trace:
at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId)
at System.Diagnostics.Process.get_MainModule()
Here is the Build configuration:
Choosing Any CPU for Platform target is not enough, you also have to uncheck Prefer 32-bit, otherwise the application will still be run as a 32-bit application.
This is only possible on application projects, not on library project. If your project is a library, you must do it in the project using your library.
After investigation I realized that Logman.exe is unmanaged code, so it cannot return a .NET object as MainModule, so it's a normal behavior. I'ts just slightly confusing error message for Win32Exception. It doesn't influence the execution of Process.Start().

Trying to run slui.exe from within a method using ProcessStartInfo and Process

I'm having an issue with running slui.exe from a method in c#. I'm using the code:
ProcessStartInfo startInfo = new ProcessStartInfo(#"C:\Windows\System32\slui.exe");
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
but I keep getting a Win32Exception: 'The system cannot find the file specified'.
If I change the ProcessStartInfo to: (#"C:\Windows\System32\cmd.exe") it will launch just fine.
Is there something with running slui.exe in this context that is breaking?
I'm certain that the file is in the directory specified, so I'm stumped as to what may be going wrong here.
Any ideas how to call slui.exe from a c# method?
Slui.exe is only available as a 64-bit program on Windows x64. Your hard-coded path c:\windows\system32 will get re-directed to c:\windows\syswow64 when you run as a 32-bit process. And thus won't find the file.
Project + Properties, Compile tab, change the Platform target setting to "AnyCPU". Repeat for the Release configuration. And use Environment.GetFolderPath() to ensure it still works when Windows isn't installed to c:\windows.

Check installed Excel version and launch it

I am trying in my application to launch Microsoft Excel with specific arguments (ie. additional xla & xll).
For now it works fine because all of my users only have Office11 (=2003) installed.
My company is going to switch to Windows 7 & Office 2010 and I logically can't launch any excel since the .exe is not located in C:\Program Files:\Microsoft Office\Office11\EXCEL.EXE
I ran a quick check in the registry to see that I can definitely check what version is currently installed. There are also plenty of articles explaining how to get the currently installed Office version.
However, I'd like to know if it is possible to find anything (such as a good registry key) directly giving me the .exe path so as to launch Excel.
Using my current machine (Win XP x86, Office11), I can find it in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\11.0\Excel\InstallRoot
Using this key I can, basically, find a workaround to get the actual path. Problem: there is no such key in Windows 7's registry with Office 2010 (= Office 14) installed.
Do you guys know any way to launch the currently installed excel from C#?
FYI, here is the current code section, launching Office11 from a x64 / x86 machine:
private void LaunchExcel(string arguments)
{
if (!Is64BitsOS())
{
Process process = new Process();
process.StartInfo.FileName = "excel";
process.StartInfo.Arguments = arguments;
process.Start();
}
else
{
Process process = new Process();
process.StartInfo.FileName = "c:/Program Files (x86)/Microsoft Office/Office11/excel.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.Arguments = arguments;
process.Start();
}
}
Any ideas to make this code more generic?
If you start Excel to open an Excel file, you can start a Process with the Excel file as FileName and let the Windows shell do all the work to find the associated application. You'd need an exception handler, obviously.
This would make you independent of Office and Windows versions and registry keys.
Otherwise you could take a different approach and find the associated application, like here.
The point of these suggestions is: presently, you have to change your code as soon as a new Office version is installed or a different Windows version is used, while there is a way to avoid these dependencies.
The 32 bit version of Excel 2010 running on a 64 bit version of Windows (XPx64,Vistax64,Win7x64) will have the following key.
I think this is the key you are looking for
HKLM\SOFTWARE\Wow6432Node\Microsoft\Office\14.0\Excel\InstallRoot
The 64 bit version of Excel 2010 running on a 64 bit version of Windows (XPx64,Vistax64,Win7x64) will have the following key
HKLM\SOFTWARE\Microsoft\Office\14.0\Excel\InstallRoot
copied from here

Open ACCESS Application thru C# Application on windows 7

Some background:
we have a windows application (c#) that locate in the system try.
that simple application is a basically shortcuts manger for other application and messaging between the workers.
one of the application is an Access 2007 application (connected to sqlserver) - the client works with ACCESS Runtime 2007 (latest version)
THE problem is that we can not launch the Access application correctly from the C# application.
THE problem is only on windows 7 (we don't have vista) - [on XP OS everything works fine)
"correctly" - meaning that the Access application running but the Ribbon Bar is missing some Icons (strange). also some functionality like open the Outlook is not working.
Some more Info:
- IF we put shortcut on the client desktop to the Access application everything ok.
- The C# application have no problem to launch other EXE file.
- The C# application include Manifest file (run as admin on Win 7).
The Original code is very simple (Works only in XP):
System.Diagnostics.Process.Start(AppPath);
The 'Open EXE' code that works (Works on XP and Win7)-[not working with Access Application]
Process Proc= new Process();
Proc.StartInfo.UseShellExecute = false;
Proc.StartInfo.FileName = Application.StartupPath + #"\PasswordManager.exe";
Proc.Start();
We try many codes with no success like [NOT WORKING]:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = sAccPath;// msaccess Path;
proc.StartInfo.Arguments = #"""" + AppPath+ #"""";
proc.Start();
We also try to add [NOT WORKING]:
System.OperatingSystem osInfo = System.Environment.OSVersion;
if (osInfo.Version.Major > 5)
proc.StartInfo.Verb = "runas";
Helppppppppppp!!!
Thanks
I could be wrong on this, but my guess is that it's running in reduced functionality mode due to your not defining a trusted location from which it can run. I don't know how this is done in code, but if you launch A2007, it's on the Office menu under Access Options (I'm posting from menu, as I mostly use A2003 and don't want to wait through the re-registration process).
We uninstall current OFFICE SBE 2007 and Install a newest version of OFFICE SBE 2007.
everything works fine now.
our conclusion is that Office SBE 2007 first version (instaled at client computer) doesn't work well with Access Runtime 2007.
(weird, but work)
David, Thank You very much.

Categories