c# windows application launches external exe's fine in visual studio but not when installed

StackOverflow https://stackoverflow.com/questions/21858801

  •  13-10-2022
  •  | 
  •  

Pregunta

I have a program which is a authoring tool for making courses. I decided to create a screen recording feature for it using camstudio. The code I used to launch the exe is:

Process process = new Process();
process.StartInfo.FileName = "Recorder.exe";
process.StartInfo.Arguments = "";
process.Start();

When I am running the program in visual studio and step through the code it works fine and launches the recorder.exe with no errors. BUT when I make an installer for the project in visual studio 2010 and install the program, the program crashes when I press the button which should launch the recorder.exe.

I checked the windows error log and here is what it told me:

Application: Perform.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.ComponentModel.Win32Exception Stack: at System.Diagnostics.Process.StartWithShellExecuteEx(System.Diagnostics.ProcessStartInfo) at System.Diagnostics.Process.Start() at P2Designer.Window1.scriptUpdatePerform(System.Object, System.Windows.RoutedEventArgs) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(System.Object, System.Windows.RoutedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(System.Object, System.Windows.RoutedEventArgs, Boolean) at System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject, System.Windows.RoutedEventArgs) at System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs) at DevComponents.WpfRibbon.ButtonDropDown.OnClick() at DevComponents.WpfRibbon.ButtonDropDown.x984587de9d70aaba(System.Object) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object) at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr) at MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef) at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame) at System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame) at System.Windows.Threading.Dispatcher.Run() at System.Windows.Application.RunDispatcher(System.Object) at System.Windows.Application.RunInternal(System.Windows.Window) at System.Windows.Application.Run(System.Windows.Window) at System.Windows.Application.Run() at P2Designer.App.Main()

Found a similar unanswered question on here: Why i'm getting a win32exception once i'm starting the Process?

If more information is need please let me know and I'll provide as much as I can. I have seen similar questions on here and other forums but none have given me the answer. Thanks, Jim

¿Fue útil?

Solución

(Per Josh Gallagher's suggestion, reposting comment as answer)

That is likely an issue with current working directory (cwd). IIRC, VS actually sets cwd to the output folder when launching your exe in debug. However, in read world, cwd really depends on how exe is launched and does not necessarily point to the same folder where your target exe is.

You can actually test it easily by simply writing the value of Environment.CurrentWorkingDirectory (or something like that) and see what it points to. alternatively try to hardcode full path for now while troubleshooting. Either way, my money are on the fact that your context for execution is in wrong dir.

Otros consejos

Wrap your code in

try
{
  Process process = new Process();
  process.StartInfo.FileName = "Recorder.exe";
  process.StartInfo.Arguments = "";
  process.Start();
}
catch(Exception e)
{
   //Log using your logger the full error message.
}

This will give you indication of what's faulting your program. Difference between success in VS and failure in real world is likely some context such as current working directory. But at least this will give you information as to what is the nature of the error that causes it to crash.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top