Thread: the console always appears......

  1. #1
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92

    the console always appears......

    Hi, I try to run a java application by exe file.....

    Here's the code:
    Code:
    #include <stdio.h> 
    #include <process.h> 
    
    int main(void)
    {
      char *my_args[4];
      
      my_args[0] = "jre1.5.0\\bin\\java.exe";
      my_args[1] = "-jar";
      my_args[2] = "SBD.jar";
      my_args[3] = NULL;
        
      spawnv( P_NOWAIT, "jre1.5.0\\bin\\java.exe", my_args);
        
      return 0;
    }
    The code works....... But if I click the exe file ( that is run.exe ) from Windows Explorer, the console appears first, then my java application appear..... damn the console does not want to disappear.......

    How do I make the condition that when I double click the exe file ( that is run.exe ), my java application appears but the console does not appear......

    What code do I have to put?????

    The second question ( not related with c++ anyway ) : How do I change the appearance of this exe file in Windows explorer??? To make it clear.... I installed Adobe Reader 6 and the exe file is different with mine. The Adobe one has the icon. Mine has no icon. Mine looks like a rectangular white box with blue menubar.

    Dev-Cpp 4.9.... Windows XP.....

    Thank you......
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Use some of the better ways of starting processes in win32, as listed in the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    I have read the faq before I post my question, Salem....

    system() always make the console appears......

    I try to use CreateProcess function. Here's the code:
    Code:
    #include <windows.h> 
    
    int main(void)
    {
      char szPath[] = "jre1.5.0\\bin\\java.exe -jar SBD.jar";
      PROCESS_INFORMATION pif;  //Gives info on the thread and..
                               //..process for the new process
      STARTUPINFO si;          //Defines how to start the program
    
      ZeroMemory(&si,sizeof(si)); //Zero the STARTUPINFO struct
      si.cb = sizeof(si);         //Must set size of structure
    
      BOOL bRet = CreateProcess(
            szPath, //Path to executable file
            NULL,   //Command string - not needed here
            NULL,   //Process handle not inherited
            NULL,   //Thread handle not inherited
            FALSE,  //No inheritance of handles
            0,      //No special flags
            NULL,   //Same environment block as this prog
            NULL,   //Current directory - no separate path
            &si,    //Pointer to STARTUPINFO
            &pif);   //Pointer to PROCESS_INFORMATION
    
      if(bRet == FALSE)
      {
        MessageBox(HWND_DESKTOP,"Unable to start program","",MB_OK);
        return 1;
      }
    
      CloseHandle(pif.hProcess);   //Close handle to process
      CloseHandle(pif.hThread);    //Close handle to thread
    
      return 0;
    }
    It did not work. If I change szPath to "C:\\windows\\system32\\cacl.exe" or "jre1.5.0\\bin\\java.exe", it works..... but how do I handle the argument that program needs?????
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  4. #4
    Banned
    Join Date
    Oct 2004
    Posts
    250
    Code:
    HINSTANCE Open = ShellExecute(HWND_DESKTOP,"open","filetoopen.txt",0,0,SW_SHOW);

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by gandalf_bar
    but how do I handle the argument that program needs?????
    Set the first parameter of CreateProcess to NULL and use the second parameter instead to pass path and command line arguments:
    Code:
    #include <windows.h> 
    
    int main(void)
    {
      char szPath[] = "jre1.5.0\\bin\\java.exe -jar SBD.jar";
      PROCESS_INFORMATION pif;  //Gives info on the thread and..
                               //..process for the new process
      STARTUPINFO si;          //Defines how to start the program
    
      ZeroMemory(&si,sizeof(si)); //Zero the STARTUPINFO struct
      si.cb = sizeof(si);         //Must set size of structure
    
      BOOL bRet = CreateProcess(
            0, //Path to executable file
            szPath, //Command string - use this to pass path and args 
            NULL,   //Process handle not inherited
            NULL,   //Thread handle not inherited
            FALSE,  //No inheritance of handles
            0,      //No special flags
            NULL,   //Same environment block as this prog
            NULL,   //Current directory - no separate path
            &si,    //Pointer to STARTUPINFO
            &pif);   //Pointer to PROCESS_INFORMATION
    
      if(bRet == FALSE)
      {
        MessageBox(HWND_DESKTOP,"Unable to start program","",MB_OK);
        return 1;
      }
      
      CloseHandle(pif.hProcess);   //Close handle to process
      CloseHandle(pif.hThread);    //Close handle to thread
    
      return 0;
    }
    This is described in the msdn page for CreateProcess and is demonstrated in the example that accompanies it.
    Last edited by Ken Fitlike; 01-08-2005 at 11:48 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #6
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Hi Ken,

    I have tried your code and this:
    Code:
    #include <windows.h>  //You need shell32.lib for this one 
    
    int main(void)
    {
      char szPath[] = "jre1.5.0\\bin\\java.exe";
    
      char *my_args;
      
      my_args = "-jar SBD.jar";
    
      HINSTANCE hRet = ShellExecute(
            HWND_DESKTOP, //Parent window
            "open",       //Operation to perform
            szPath,       //Path to program
            my_args,         //Parameters
            NULL,         //Default directory
            SW_SHOW);     //How to open
    
      /*
      The function returns a HINSTANCE (not really useful in this case)
      So therefore, to test its result, we cast it to a LONG.
      Any value over 32 represents success!
      */
    
      if((LONG)hRet <= 32)
      {
        MessageBox(HWND_DESKTOP,"Unable to start program","",MB_OK);
        return 1;
      }
    
      return 0;
    }
    They both works but the console still appears. Then I realize that I launch two process actually not one. The first is: java.exe and the second is: my java application that is SBD.jar ( GUI application ). Of course the console appears because that is java.exe application.

    JSmooth solved my problem ( although it is not c++ related ).
    http://jsmooth.sourceforge.net/
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  7. #7
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    Try a search on it, cos I can remember a post like that one before, it might help


  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> They both works but the console still appears.
    Take Ken's code and add:
    Code:
    si.cb = sizeof(si);         //Must set size of structure
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console, Terminal and Terminal Emulator
    By lehe in forum C Programming
    Replies: 4
    Last Post: 02-15-2009, 09:59 PM
  2. Full Screen Console
    By St0rmTroop3er in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2005, 09:59 PM
  3. Problems with a simple console game
    By DZeek in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2005, 02:02 PM
  4. Console Functions Help
    By Artist_of_dream in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 03:44 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM