Visual C - Win32 APP - How to use the system call with system () without opening a command prompt window?

advertisements

I am new to VC++ I build a simple Win32 Application via default templet. when i run it it shows a normal window with a menubar.

I added a system call that does a curl operation in InitInstance function:

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

{

   HWND hWnd;
   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);

   UpdateWindow(hWnd);

   //EDITED THIS WILL CREATE A FILE.HTML

   system("curl www.google.co.in > file.html");

   return TRUE;
}

all works fine.

But the problem is when ever i call system it opens in console window. I want it to be a hidden process that does not shows that command prompt window..

help me with this.


I put code in separate function, with url and filename as parameters.

    #include <tchar.h>
    #include <windows.h>

    BOOL DownloadWithoutConsole(TCHAR* pszURI, TCHAR* pszFileName)
    {
    //here we will calculate our command line length in bytes
    DWORD dwLen = (lstrlen(pszURI) + lstrlen(pszFileName) + 20)*sizeof(TCHAR);
    //memory allocation for the command line
    TCHAR* pszCmdLine = (TCHAR*)HeapAlloc(GetProcessHeap(), 0, dwLen);
    ZeroMemory(pszCmdLine, dwLen);
    //making command line
    _stprintf_s(pszCmdLine,dwLen/sizeof(TCHAR), TEXT("cmd /c curl %s > %s"), pszURI, pszFileName);

    //preparing arguments for CreateProcess
    STARTUPINFO si;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);

    PROCESS_INFORMATION pi;
    ZeroMemory(&pi, sizeof(pi));

    //creating new process
    BOOL bResult = CreateProcess(NULL, pszCmdLine, NULL, NULL, FALSE, CREATE_NO_WINDOW,
    NULL, NULL, &si, &pi);

    if(bResult)
    {
    //waiting for process termination
    WaitForSingleObject(pi.hProcess, INFINITE);
    //cleanup
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
    }
    //freeing memory
    HeapFree(GetProcessHeap(), 0, pszCmdLine);
    return(bResult);
    }

   int __cdecl _tmain(void)
   {
      DownloadWithoutConsole(TEXT("stackoverflow.com"), TEXT("test.txt"));
      return(0);
   }