1

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

Community
  • 1
  • 1
James Blackburn
  • 594
  • 4
  • 23
  • Could you put some error handling in to catch and log the Message and ErrorCode in the Win32Exception? The error code especially will give more information as to what the issue is. Also, can you tell us what the behaviour is when running the program as an administrator? I suspect a permissions related issue. – Josh Gallagher Feb 18 '14 at 15:58
  • I've ran as non admin an as admin and got the same error on both. I will get on with that error handling now – James Blackburn Feb 18 '14 at 16:03
  • I'm getting: System.ComponentModel.Win32Exception(0x800004005): The system cannot find the file sepecififed.... Pretty sure the path I am providing is correct. Am I doing something wrong when setting up the installer? Do I have to add exe's in a special way? – James Blackburn Feb 18 '14 at 16:13

2 Answers2

2

(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.

LB2
  • 4,802
  • 19
  • 35
  • I forgot to ask. Do you know how I make it so that the external exe's of my project get included in the program files folder of my project when installed? – James Blackburn Feb 19 '14 at 08:54
  • My project deleted the EXEs when I uploaded to team foundation and I forgot how to add them back in again! Took me an hour to figure it out again. So I am going to note here how to do it. Right click installer project then click view then click file system. Then on the left where it says application folder expand this then expand your project folder then the bin folder then within debug add the EXEs in there. – James Blackburn Apr 23 '14 at 11:58
0

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.

LB2
  • 4,802
  • 19
  • 35
  • Literally just done that then saw your post :p. Im getting: System.ComponentModel.Win32Exception(0x800004005): The system cannot find the file sepecififed.... Dunno how the hell its not finding it as pretty damn sure the path is correct! Am I doing something wrong when setting up the installer? Do I have to add exe's in a special way? – James Blackburn Feb 18 '14 at 16:11
  • That is likely 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. – LB2 Feb 18 '14 at 16:15
  • @LB2 Write that up as an answer; I'm sure you've nailed it. – Josh Gallagher Feb 18 '14 at 16:26
  • I tried Environment.CurrentDirectory and your right its looking for the files elsewhere. I feel pretty stupid now! – James Blackburn Feb 18 '14 at 16:46