none
How to run exe file using xp_cmdshell

    Question

  • Hello everyone,

    I've a exe file developed using Visual Studio .Net 2005.  It is a console application and it does not contain a GUI. 
    This application will convert any file(like .txt, .doc etc) to .pdf file.
    It has following code:

    using

    System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;

    namespace

    PrintToPDF
    {
    class Program
    {
    static void Main(string[] args)
    {
    try
    {
    Process printProcess = new Process();
    printProcess.StartInfo.FileName = "C:\\k.txt";
    printProcess.StartInfo.Verb = "print";
    printProcess.StartInfo.WindowStyle =
    ProcessWindowStyle.Minimized;
    printProcess.StartInfo.CreateNoWindow =
    true;

    printProcess.Start();

    try
    {
    //printProcess.WaitForExit();
    }
    catch (InvalidOperationException ex)
    {
    throw ex;
    }
    printProcess.Dispose();
    }
        catch (Exception ex)
       {
        throw ex;
       }
    }
    }
    }



    I need to run it from stored proc using xp_cmdshell.
    Please note that I'm using SQL Server 2000.

    My stored proc is :

    Create proc callExe
    As
    EXEC master.dbo.xp_cmdshell 'C:\PrintToPDFConsole.exe'

    After executing this, the output is :

    NULL
    Unhandled Exception: System.ComponentModel.Win32Exception: No application is associated with the specified file for this operation
    at PrintToPDF.Program.Main(String[] args)
    NULL

    and the exe file did not run.

    Please help in this.

    Also, I've tried with common exe file like 'notepad.exe' like:

    ALTER proc callExe
    As
    EXEC master.dbo.xp_cmdshell 'notepad.exe'

    When I executed this, the execution is going on and not showing any output. The execution time is more that 10 mins and going on...
    Then I forcefully closed the isqlw.exe from Task Manager.

    Is that mean exe file can't be executed in sql server 2000 using xp_cmdshell ?
    Monday, March 30, 2009 1:17 PM

All replies

  • This has nothing to do with xp_cmdshell.  The error you are seeing is coming from the application.  Your application must need something configured to run properly.   You will probably need to login as the SQL Server service user and configure the application.

    The reason you don't see notepad, is because is running on the server as a service in the background. You will never see it.
    Monday, March 30, 2009 2:18 PM
    Moderator

  • You should not expect the GUI window to be shown for any windows application or a form based application like 'notepad.exe'.

    xp_cmdshell is executed by SQL Server and SQL Agent both run as a service ( They are not user ).

    If the notepad.exe was opened by a service. It will not be visible to the user. You cannot interact to them as well. So you see them in Task Manager , and you can able to close them only by killing the process.

    Coming back to ur problem,

    You have your "k.txt" file which was default assosiated with the textpad.exe application.

    so when you do,

    printProcess.StartInfo.FileName = "C:\\k.txt";

    you are implicitly starting the "Notepad.exe".

    and the next statement ,

    printProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;

    you are making the Process as Windows application in minimized state.

    Possible Solution ,

    Associate ".txt" to PrintToPDFConsole.exe

    and change, 

    Process printProcess = new Process();
    printProcess.StartInfo.FileName = "C:\\k.txt"
    ;
    printProcess.StartInfo.CreateNoWindow = true;



    Be the change you want.
    Monday, March 30, 2009 2:40 PM
  • This has nothing to do with xp_cmdshell.  The error you are seeing is coming from the application.  Your application must need something configured to run properly.   You will probably need to login as the SQL Server service user and configure the application.

    The reason you don't see notepad, is because is running on the server as a service in the background. You will never see it.
    Thank you for your reply.

    I'm logging in the sql server as user only. Also when I'm trying this:

    ALTER     proc callExe
    As
    EXEC master.dbo.xp_cmdshell 'dir C:\PrintToPDFConsole.exe'

    and after execution, the output is:


     Volume in drive C has no label.
     Volume Serial Number is 84DE-0D66
    NULL
     Directory of C:\
    NULL
    03/26/2009  06:19 PM            16,384 PrintToPDFConsole.exe
                   1 File(s)         16,384 bytes
                   0 Dir(s)     736,858,112 bytes free
    NULL


    But how I'll configure to get the exe to run ? The exe is working fine running it directly by double clicking, also it is working running through command prompt.

    I'm getting hopeless.
    Please help.

    Tuesday, March 31, 2009 5:42 AM
  • Are you trying to Run PrintToPDFConsole.exe or your C# exe?


    I mean an exe calling another exe inside?

    Be the change you want.
    Tuesday, March 31, 2009 6:09 AM

  • You should not expect the GUI window to be shown for any windows application or a form based application like 'notepad.exe'.

    xp_cmdshell is executed by SQL Server and SQL Agent both run as a service ( They are not user ).

    If the notepad.exe was opened by a service. It will not be visible to the user. You cannot interact to them as well. So you see them in Task Manager , and you can able to close them only by killing the process.

    Coming back to ur problem,

    You have your "k.txt"  file which was default assosiated with the textpad.exe application.

    so when you do,

    printProcess.StartInfo.FileName = "C:\\k.txt" ;

    you are implicitly starting the "Notepad.exe".

    and the next statement ,

    printProcess.StartInfo.WindowStyle = ProcessWindowStyle .Minimized;

    you are making the Process as Windows application in minimized state.

    Possible Solution ,

    Associate ".txt" to PrintToPDFConsole.exe

    and change, 

    Process printProcess = new Process ();
    printProcess.StartInfo.FileName = "C:\\k.txt"
    ;
    printProcess.StartInfo.CreateNoWindow = true ;



    Be the change you want.
    Thank you for your reply.

    Can you please tell me how I'll associate ".txt" to PrintToPDFConsole.exe ?
    Except this, I did the following change as per you have said.

    Process printProcess = new Process ();
    printProcess.StartInfo.FileName = "C:\\k.txt"
    ;
    printProcess.StartInfo.CreateNoWindow = true ;

    But I'm getting the same unwanted output as previously I got.
    My problem did not solve yet. Please tell me if I can do any more changes.

    Please note that I'm using Adobe Acrobat 6.0 Professional and PDF Printer is installed in my system and it is set to default printer. The default port is set to My Document\*.pdf .

    When I run the exe file, the 'k.txt' file getting converted to 'k.pdf' and save to My Document folder.
    But when I'm trying to run it from the stored proc it does not work and I got the result which I mentioned previously.

    Tuesday, March 31, 2009 6:14 AM
  • Try changing SP to,

    EXEC master.dbo.xp_cmdshell 'C:\PrintToPDFConsole.exe'

    and check.
    Be the change you want.
    Tuesday, March 31, 2009 6:42 AM
  • Try changing SP to,

    EXEC master.dbo.xp_cmdshell 'C:\PrintToPDFConsole.exe'

    and check.
    Be the change you want.
    Yes I've tried this also.
    I'm getting same result.
    Tuesday, March 31, 2009 7:10 AM
  • Are you trying to Run PrintToPDFConsole.exe or your C# exe?


    I mean an exe calling another exe inside?

    Be the change you want.
    I did not understand this.
    Let me clarify.
    I've an exe file which I got from the .Net 2005 console application in Bin/Debug folder of the application.
    Then I copied to my local C drive.
    This is running fine when I double clicked it. Also this is running from the command prompt.

    But this is not running from the stored procedure.
    Tuesday, March 31, 2009 7:15 AM
  • Your program is running from the stored proc.  It is generating the error:

    Unhandled Exception: System.ComponentModel.Win32Exception: No application is associated with the specified file for this operation
    at PrintToPDF.Program.Main(String[] args)

    You need to figure out what is causing your program to generate this error.

    It is probably because the program is not configured properly to run by the SQL Server system account.   The xp_cmdshell is running as the user which runs the SQL Server service.  What is that sent too?  Is it set to a domain user or Local System?  It needs to be set to a domain user.  Then you will need to login to the server using that domain login, and run the program to see what happens.
    Friday, April 03, 2009 1:59 PM
    Moderator
  • Your program is running from the stored proc.  It is generating the error:

    Unhandled Exception: System.ComponentModel.Win32Exception: No application is associated with the specified file for this operation
    at PrintToPDF.Program.Main(String[] args)

    You need to figure out what is causing your program to generate this error.

    It is probably because the program is not configured properly to run by the SQL Server system account.   The xp_cmdshell is running as the user which runs the SQL Server service.  What is that sent too?  Is it set to a domain user or Local System?  It needs to be set to a domain user.  Then you will need to login to the server using that domain login, and run the program to see what happens.
    The error I was getting is due to I was using SQL Server client.
    Now I changed my system to SQL Server 2000 (Server).
    Now I'm not getting the error: Unhandled Exception.
    But I'm getting output as
    NULL
    and the exe file does not run.

    Please guide me what to do more ?
    Friday, April 03, 2009 2:14 PM
  • If you login directly on the SQL Server using the SQL Server service account, open a command prompt, and run the command what do you get?
    Friday, April 03, 2009 5:03 PM
    Moderator
  • If you login directly on the SQL Server using the SQL Server service account, open a command prompt, and run the command what do you get?
    The output is coming NULL

    But I've checked in the PDF Printer, the file is coming as spooling, but not printing it.
    What may be the causes ? Any solution ?
    Monday, April 13, 2009 1:52 PM