shellexecute and nslookup

hi guys,

i am trying to call shellexecute
in VC6 to execute "nslookup" and return the results string back to a variable...
how can I do that?
Shellexecutes only results a HINSTANCE...
pls give a code example if possible.
Note: it is used in a cgi (.exe) program



Haho2Asked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
'ShellExecute()' is not a good solution for this.

Use

      HANDLE hProcess;
      int hStdOut;
      int hStdOutPipe[2];

      // Create the pipe
      if(_pipe(hStdOutPipe, 512, O_BINARY | O_NOINHERIT) == -1)
         return    1;

      // Duplicate stdout handle (next line will close original)
      hStdOut = _dup(_fileno(stdout));

      // Duplicate write end of pipe to stdout handle
      if(_dup2(hStdOutPipe[WRITE_HANDLE], _fileno(stdout)) != 0)
         return    2;

      // Close original write end of pipe
      close(hStdOutPipe[WRITE_HANDLE]);

      // Spawn process
      hProcess = (HANDLE)spawnlp(P_NOWAIT, "nslookup.exe",
       "www.microsoft.com", NULL);

      // Duplicate copy of original stdout back into stdout
      if(_dup2(hStdOut, _fileno(stdout)) != 0)
         return    3;

      // Close duplicate copy of original stdout
      close(hStdOut);

      if(hProcess)
      {
         int nOutRead;
         while    (nExitCode == STILL_ACTIVE)
         {
            nOutRead = read(hStdOutPipe[READ_HANDLE],
             szBuffer, OUT_BUFF_SIZE);
            if(nOutRead)
            {
               // do something with the output of 'nslookup' that is contained
               // in szBuffer
            }

            if(!GetExitCodeProcess(hProcess,(unsigned long*)&nExitCode))
               return 4;
         }
      }


instead...

Feel free to ask if you encounter problems!
0
 
Haho2Author Commented:
hi,

does this require MFC?
0
 
jkrCommented:
No, this actually doesn't require MFC at all ;-)

But you're still free to use MFC if you want to...
0
All Courses

From novice to tech pro — start learning today.