locked
Run Windows application from Command Prompt

    Question

  •  

    Hi

     

    I am new to programming in Visual Basic and need some assistance. I have Visual Studio 2008 and I have created a Windows VB Application. Basically I have one form that runs some external applications like diskpart and others.

     

    I have succesfully run these from within the VS2008 and also after installing the application on my development workstation. Now what I need to do is launch it from a command prompt windows.

     

    If I type Notepad from the command prompt it launches notepad, but when I try my application I just get the prompt again.

     

    Can anyone assist please?

     

    Thanks

    Thursday, February 14, 2008 11:40 AM

Answers

  • It will always work to launch your application from the command prompt if your command window is in the right folder where your executeable is or if you provide the fill path together with the command like

     

    C:\> D:\MyFiles\Something\This\myApp.exe

     

    If you want to have myApp.exe work regardless of the folder you are in and without a path you have eigther to copy your executeable to a known search path or add the path of your executeable to the known search paths found in Control Panel --> System ---> Advanced --> Environment Variables --> System Variables

    Find the entry for PATH in this list and either edit it or copy your exe to one of the paths existing in this list.

     

    Pure oldschool DOS-knowledge mate Wink

    Thursday, February 14, 2008 1:47 PM
  • The normal command prompt won't work.  .NET exe files are NOT true exe files, but really MSIL files (intermediate language) that will not run without the JIT (Just In Time) compiler available with the CLR that is part of .NET. 

     

    You need to use the special command prompt that comes with Visual Studio.  This will access the JIT compiler that translates MSIL into machine code that will run on the destination computer.  Click on Start/Programs/Microsoft Visual Studio 2005 (or 2008)/ Visual Studio Tools/Visual Studio 2005 (or 2008) Command Prompt

     

    Note that this path is not found with the Express editions, but you can look for the file to see if it was installed in one of the directories.

     

    The special command prompt runs a batch file named vcvarsall.bat

    For Visual Studio 2005, it may be located in:

    Microsoft Visual Studio 8\VC\vcvarsall.bat

     

    For Visual Studio 2008, it may be located in:

    Program Files\VC\vcvarsall.bat

     

    But if you click on the file, it opens and closes instantly, so I don't really know how it works.  Maybe you need to run the batch file before running the pseudo-exe file, but you will need to experiment with it to see what happens.

     

    In any case, I copied and pasted the batch file code from my computer, so here it is.  If you can't find it, you can try copying it and pasting it into Notepad, and save it as vcvarsall.bat

     

     

    @echo off
    if "%1" == "" goto x86
    if not "%2" == "" goto usage

    if /i %1 == x86       goto x86
    if /i %1 == amd64     goto amd64
    if /i %1 == x64       goto amd64
    if /i %1 == ia64      goto ia64
    if /i %1 == x86_amd64 goto x86_amd64
    if /i %1 == x86_ia64  goto x86_ia64
    goto usage

    :x86
    if not exist "%~dp0bin\vcvars32.bat" goto missing
    call "%~dp0bin\vcvars32.bat"
    goto :eof

    :amd64
    if not exist "%~dp0bin\amd64\vcvarsamd64.bat" goto missing
    call "%~dp0bin\amd64\vcvarsamd64.bat"
    goto :eof

    :ia64
    if not exist "%~dp0bin\ia64\vcvarsia64.bat" goto missing
    call "%~dp0bin\ia64\vcvarsia64.bat"
    goto :eof

    :x86_amd64
    if not exist "%~dp0bin\x86_amd64\vcvarsx86_amd64.bat" goto missing
    call "%~dp0bin\x86_amd64\vcvarsx86_amd64.bat"
    goto :eof

    :x86_ia64
    if not exist "%~dp0bin\x86_ia64\vcvarsx86_ia64.bat" goto missing
    call "%~dp0bin\x86_ia64\vcvarsx86_ia64.bat"
    goto :eof

    :usage
    echo Error in script usage. The correct usage is:
    echo     %0 [option]
    echo where [option] is: x86 ^| ia64 ^| amd64 ^| x86_amd64 ^| x86_ia64
    echo:
    echo For example:
    echo     %0 x86_ia64
    goto :eof

    :missing
    echo The specified configuration type is missing.  The tools for the
    echo configuration might not be installed.
    goto :eof

     

    ======================================================================

    NOTE:  It looks like the batch file needs a parameter -- the name of the exe file to run.  I haven't tested it myself, but you should try placing the batch file in the same directory as the exe file you want to run, and issue the command from the prompt as follows (for example, if the file name is MyProject.exe) but starting with the full path.  There should be one space between the name of the batch file and the name of the exe file.

     

    > vcvarsall MyProject.exe

     

    ======================================================================

    ANOTHER NOTE:   A further look shows that the above batch file calls on other batch files in locations known to it.  In that case, copying the file will not do the trick.  You will need to point to the location of the batch file, using the complete path of the destination computer to the original vcvarsall.bat file, followed by a space, and then the exe file you want to run.  Since the client's directories and installations may differ from your own, that is really not a good solution.

    Saturday, February 16, 2008 12:30 AM

All replies

  • It will always work to launch your application from the command prompt if your command window is in the right folder where your executeable is or if you provide the fill path together with the command like

     

    C:\> D:\MyFiles\Something\This\myApp.exe

     

    If you want to have myApp.exe work regardless of the folder you are in and without a path you have eigther to copy your executeable to a known search path or add the path of your executeable to the known search paths found in Control Panel --> System ---> Advanced --> Environment Variables --> System Variables

    Find the entry for PATH in this list and either edit it or copy your exe to one of the paths existing in this list.

     

    Pure oldschool DOS-knowledge mate Wink

    Thursday, February 14, 2008 1:47 PM
  • Thanks for the reply.

     

    Yes, I have them in the same directory as the application i.e. the System32 directory which is in the path.

     

    Basically, what I am attempting is launching this application from a command prompt (console window) as mentioned. It works fine if I double click the executable from within the windows explorer (by exploring to the program directory i.e. Windows\system32). But the minute I try it from the command prompt when I am in the Windows\system32's windows it does nothing...just responds with the command prompt.

     

    Did I maybe not compile it correctly (publish). I used the option to publish to CD option.

     

    Also I created this project as a "Windows Form Application"

     

    Thanks.

     

    Thursday, February 14, 2008 9:17 PM
  • Hm, actually typing the exe in the DOS prompt does the same as doubleclicking it in Windows Explorer, so there should be no real difference. Maybe you are doing things in your Form_Load or Main() Functions which for some reason the system does not like. You can figure it out by adding some Debug.WriteLine statements in the Form_Load event and use DebugView to figure out what actually happens.

    To have some more info how this works you can have a look at this thread http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2836454&SiteID=1 where I explained it to somebody else.

     

    Friday, February 15, 2008 9:27 AM
  • The normal command prompt won't work.  .NET exe files are NOT true exe files, but really MSIL files (intermediate language) that will not run without the JIT (Just In Time) compiler available with the CLR that is part of .NET. 

     

    You need to use the special command prompt that comes with Visual Studio.  This will access the JIT compiler that translates MSIL into machine code that will run on the destination computer.  Click on Start/Programs/Microsoft Visual Studio 2005 (or 2008)/ Visual Studio Tools/Visual Studio 2005 (or 2008) Command Prompt

     

    Note that this path is not found with the Express editions, but you can look for the file to see if it was installed in one of the directories.

     

    The special command prompt runs a batch file named vcvarsall.bat

    For Visual Studio 2005, it may be located in:

    Microsoft Visual Studio 8\VC\vcvarsall.bat

     

    For Visual Studio 2008, it may be located in:

    Program Files\VC\vcvarsall.bat

     

    But if you click on the file, it opens and closes instantly, so I don't really know how it works.  Maybe you need to run the batch file before running the pseudo-exe file, but you will need to experiment with it to see what happens.

     

    In any case, I copied and pasted the batch file code from my computer, so here it is.  If you can't find it, you can try copying it and pasting it into Notepad, and save it as vcvarsall.bat

     

     

    @echo off
    if "%1" == "" goto x86
    if not "%2" == "" goto usage

    if /i %1 == x86       goto x86
    if /i %1 == amd64     goto amd64
    if /i %1 == x64       goto amd64
    if /i %1 == ia64      goto ia64
    if /i %1 == x86_amd64 goto x86_amd64
    if /i %1 == x86_ia64  goto x86_ia64
    goto usage

    :x86
    if not exist "%~dp0bin\vcvars32.bat" goto missing
    call "%~dp0bin\vcvars32.bat"
    goto :eof

    :amd64
    if not exist "%~dp0bin\amd64\vcvarsamd64.bat" goto missing
    call "%~dp0bin\amd64\vcvarsamd64.bat"
    goto :eof

    :ia64
    if not exist "%~dp0bin\ia64\vcvarsia64.bat" goto missing
    call "%~dp0bin\ia64\vcvarsia64.bat"
    goto :eof

    :x86_amd64
    if not exist "%~dp0bin\x86_amd64\vcvarsx86_amd64.bat" goto missing
    call "%~dp0bin\x86_amd64\vcvarsx86_amd64.bat"
    goto :eof

    :x86_ia64
    if not exist "%~dp0bin\x86_ia64\vcvarsx86_ia64.bat" goto missing
    call "%~dp0bin\x86_ia64\vcvarsx86_ia64.bat"
    goto :eof

    :usage
    echo Error in script usage. The correct usage is:
    echo     %0 [option]
    echo where [option] is: x86 ^| ia64 ^| amd64 ^| x86_amd64 ^| x86_ia64
    echo:
    echo For example:
    echo     %0 x86_ia64
    goto :eof

    :missing
    echo The specified configuration type is missing.  The tools for the
    echo configuration might not be installed.
    goto :eof

     

    ======================================================================

    NOTE:  It looks like the batch file needs a parameter -- the name of the exe file to run.  I haven't tested it myself, but you should try placing the batch file in the same directory as the exe file you want to run, and issue the command from the prompt as follows (for example, if the file name is MyProject.exe) but starting with the full path.  There should be one space between the name of the batch file and the name of the exe file.

     

    > vcvarsall MyProject.exe

     

    ======================================================================

    ANOTHER NOTE:   A further look shows that the above batch file calls on other batch files in locations known to it.  In that case, copying the file will not do the trick.  You will need to point to the location of the batch file, using the complete path of the destination computer to the original vcvarsall.bat file, followed by a space, and then the exe file you want to run.  Since the client's directories and installations may differ from your own, that is really not a good solution.

    Saturday, February 16, 2008 12:30 AM
  • I added more notes to my post of a few days ago, so please check back if you haven't done so.

     

     

    Sunday, February 17, 2008 7:11 PM
  • Hi Solitaire and thanks for your detailed explanation.

     

    Actually for some reason it works on my machine to launch an Windows Forms application without the need to call the batch file mentioned if I just type

     

    C:\> D:\myAppFolder\myApp.exe

     

    But now I ran into this problem when I try to call myApp via a batch file. If I use the line like above in my batchfile, the problem is that the command window stays open and that is annoying, so I tried to use

     

    start D:\myAppFolder\myApp.exe

     

    within the batch file which won't work. It will however work to launch it with

     

    start C:\...\vsvars32.bat D:\myAppFolder\myApp.exe

     

    so far so good, but what to do if I want to create a batch file to launch myApp without knowing the path of vsvars32.bat and even with no VS installed on the client machine? It seems that somehow windows figures out to find the framework in the first case, but not when using "start"

     

    Maybe somebody has an idea...

     

    TIA, Daniel

    Friday, February 29, 2008 12:01 PM
  • >>But now I ran into this problem when I try to call myApp via a batch file. If I use the line like above in my batchfile, the problem is that the command window stays open and that is annoying, so I tried to use<<

     

    To close the command prompt window, include EXIT as the last command in your batch file.  As far as knowing where the file you need on another computer is located, I can't help you there.

     

    Friday, February 29, 2008 7:51 PM
  • Unfortunately the EXIT thing will not work as well, because the batch file does not advance to the next line until the application is not closed if you don't use start to launch it...

     

    Wednesday, March 05, 2008 9:07 AM
  • Argl, annoying enough, but now I found the solution and will post it for everybody:

     

    If you use the start command within a bat-file without any other parameters, then the first parameter is not the executeable but the window title, so to run an application with start without parameters, you have to do it like this:

     

    start "" "C:\myPath\myApp.exe"

     

    A weird thing, but it works!

     

    Wednesday, March 05, 2008 11:14 AM