Link to home
Start Free TrialLog in
Avatar of ANAT2403
ANAT2403Flag for Israel

asked on

Running a Dot Net Exe via System.Diagnostics.Process.Start

I need to start an exe that i wrote in C# from a WebSerivce which i also wrote.

The exe wraps a vb dll and needs read/write premissions so i am starting the exe with the following code.

////CODE in WEBSERVICE
            ProcessStartInfo startPrint = new ProcessStartInfo();

            System.Security.SecureString spsw = new System.Security.SecureString();
            spsw.AppendChar('m');
            spsw.AppendChar('y');
            spsw.AppendChar('p');
            spsw.AppendChar('s');
            spsw.AppendChar('w');            

            startPrint.UserName = "myuser";
            startPrint.Password = spsw;
            startPrint.Domain = "mydomain";

            //Get the exe folder and full path
            startPrint.WorkingDirectory = @ConfigurationSettings.AppSettings["printexe_directory"];
            startPrint.FileName = @ConfigurationSettings.AppSettings["printexe_name"];
            startPrint.Arguments = string.Format("{0} {1}", FleetName, PackagePrintID.ToString());

            startPrint.CreateNoWindow = true;
            startPrint.WindowStyle = ProcessWindowStyle.Hidden;
            startPrint.UseShellExecute = false;
            Process.Start(startPrint);
/////END IF CODE            

if I set the UseShellExecute = false i get the following error (seen in the EventLogs)

The application, D:\WEB\idmr\NR\PrintInterOp\Debug\Debug\WindowsApplication2.exe, generated an application error The error occurred on 05/16/2006 @ 10:14:18.740 The exception generated was c06d007e at address 77E55DEA (KERNEL32!RaiseException)


If I set the UseShellExecute = true then i can not set the user that i need the exe to run as and then the exe runs but it fails to create the files it needs.


how can i run a exe under a specific user account from within my webservice?
please help.
Avatar of YZlat
YZlat
Flag of United States of America image

use LogonUser API function

Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As [String], _
   ByVal lpszDomain As [String], ByVal lpszPassword As [String], _
   ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _
   ByRef phToken As IntPtr) As Boolean
ASKER CERTIFIED SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial