0

I have a button in winforms that needed to run batch file and open an exe (monitor.exe) file when clicked. The batch file snippet:-

@echo on
rem @echo $Id: runnit_navigator.bat,v X.X XXXX/XX/XX XX:XX:XX sxxxaxx Exp $

echo Running runnit_monitor.bat >%TEMP%\runnit_monitor.log
if exist "%CD%\Config.bat" (
call Config.bat >>%TEMP%\runnit_monitor.log
)
if "%1%" NEQ "" (
set SPACE_SITE=%1%
)

@set monitor_start=Y

rem if not defined SPACE_CONFIG (
rem     set SPACE_CONFIG=%TEMP%
rem )

if not exist "%PROGRAMDATA%\space\config\%SPACE_SITE% \monitor.properties" (
set SPACE_SITE=NA
)

if "%SPACE_SITE%" EQU "NA" (
call "%SPACE_HOME%\Runnit\runnit_site_monitor.bat" >>%TEMP%\runnit_monitor.log
exit
)

set SPACE_SETTINGS=%PROGRAMDATA%\space\config\%SPACE_SITE%

@set JAVA_EXE=%LoginDialogJavaHome%\bin\monitor.exe

I have tried this code, no error but the exe file is not open at all.

Process proc = new Process();
private void btn1_Click(object sender, EventArgs e)
{
    try
    {
        string batDir = string.Format(@"C:\Program Files\Space\Runnit\");
        proc.StartInfo.WorkingDirectory = batDir;
        proc.StartInfo.FileName = "runnit_monitor.bat";
        proc.StartInfo.CreateNoWindow = false;
        proc.Start();
        proc.WaitForExit();
        proc.Close();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.StackTrace.ToString());
    }
}

Also, this is the output for the above code. After this window pop out, there's nothing happened. But as I put a breakpoint, I can see that some Process properties return an exception.

output

Is the way I called process is wrong? Or any suggestion of what can I understand/do to make monitor.exe run after batch file is read?

FredM
  • 454
  • 9
  • 20
HNA
  • 107
  • 2
  • 14
  • What does the exception says? – FredM Dec 04 '18 at 06:11
  • there are a few exceptions for respective properties. some of them are: `'proc.MainModule' threw an exception of type 'System.ComponentModel.Win32Exception'` and `'proc.MainWindowHandle' threw an exception of type 'System.InvalidOperationException'` – HNA Dec 04 '18 at 06:17
  • 2
    Based on [this answer](https://stackoverflow.com/questions/9501771/how-to-avoid-a-win32-exception-when-accessing-process-mainmodule-filename-in-c), the `Win32Exception` tells you that you're trying to do something, the OS doesn't allow. `Win32Exception` has the property `NativeErrorCode` and also a `Message` that will explain what the problem is. – FredM Dec 04 '18 at 07:00
  • @FredM thanks for the recommendation. Now I can see and understand what was not allowed by OS from the `Message`. From the [link](https://stackoverflow.com/questions/9501771/how-to-avoid-a-win32-exception-when-accessing-process-mainmodule-filename-in-c) you share, the problem is because I'm accessing 64 bit processes from a 32 bit process. – HNA Dec 04 '18 at 08:05
  • Glad it helped. :) – FredM Dec 04 '18 at 08:17

1 Answers1

0

I've found the answer by referring to these links..

From here (thanks to @FredM btw), I knew that the problem is because I'm accessing 64 bit processes from a 32 bit process. I can see the error by catching the Win32Exception properties.

catch (Win32Exception w)
{
   MessageBox.Show(w.Message);
   MessageBox.Show(w.NativeErrorCode.ToString());
}

Then, I found a way to correct this here. I compile and run my program not as 32-bit application. I just untick the prefer 32-bit option under Project --> Build properties. change project properties

Lastly, I rebuild and it works perfectly! The monitor.exe file can be run successfully now.

HNA
  • 107
  • 2
  • 14