Thread: system() without window

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    35

    Question system() without window

    Hi

    I am wrinting a program that calls netsh from the command line

    system("netsh -f script_adapters.txt > adapters.dat");


    It dumps the result in a text file - This works fine.

    However is there a way to surpress the command line window?
    When the system line is executed a new window is opened, the command executed and then the window is closed.
    I still want the results, but to not show the command window.

    Please help.

    Mike

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    How to eliminate console-window when calling external executables?

    Here is a function you can use. This function should work with NT/2000/XP.
    Code:
    #include <windows.h>
    #include <strsafe.h>
    
    #if !defined(ARRAY_SIZE)
    #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof((arr)[0]))
    #endif
    
    BOOL RunCommand(LPCTSTR szCommand)
    {
    	TCHAR               szComSpec[MAX_PATH];
    	TCHAR               szCmd[2048];
    	STARTUPINFO         si = { sizeof(si) };
    	PROCESS_INFORMATION pi = { 0 };
    
    	if ( SUCCEEDED(StringCchPrintf(szCmd, ARRAY_SIZE(szCmd), TEXT("/c %s"), szCommand)) &&
    	     GetEnvironmentVariable(TEXT("COMSPEC"), szComSpec, ARRAY_SIZE(szComSpec))      && 
    	     CreateProcess(szComSpec, szCmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi) )
    	{
    		CloseHandle(pi.hThread);
    		CloseHandle(pi.hProcess);
    		return TRUE;
    	}
    
    	return FALSE;
    }
    
    int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE reserved, LPSTR lpCmdLine, int nCmdShow)
    {
    	RunCommand("ECHO HelloWorld > test.txt");
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. Window scrollbar
    By maxorator in forum Windows Programming
    Replies: 2
    Last Post: 10-07-2005, 12:31 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. dont want to use all params
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 02-18-2003, 08:10 AM
  5. Invoking MSWord
    By Donn in forum C Programming
    Replies: 21
    Last Post: 09-08-2001, 04:08 PM