Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

shellexecute console output to the heap

Status
Not open for further replies.

jbs

Programmer
Feb 19, 2000
121
US
I'm trying to find a way to send the console output from an application that is run via a shellexecute api.

For example, the below lines of code will do a shell execute that produces a console window that has the time on it (this is just an example...I know how to obtain system time...again it' just an example).

I'd like to be able to send a copy of the output of the console that results from my shellexecute api to the heap.

Any ideas? Of tried a few things....having trouble. Code samples would be appreciated vice just pointing to an API. Thanks,/jbs

------code sample-------

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
int iReturn=0;
//Declare and execute the shellexecute api
HWND hwnd = NULL;
LPCTSTR lpVerb = NULL;
LPCTSTR lpFile = TEXT("cmd.exe");
LPCTSTR lpParameters = " /k time /t";
LPCTSTR lpDirectory = "c:\\windows\\system32\\";
INT nShowCmd = SW_SHOWNORMAL;
HINSTANCE hReturn;
hReturn=ShellExecute(hwnd, lpVerb, lpFile, lpParameters, lpDirectory, nShowCmd);
return (iReturn);
}

 
If you use the CreateProcess() API you can specify stream HANDLE(s) for stdin, stdout and stderr for the process you are creating. As you will see when you investigate this function the HANDLE values go into the STARTUPINFO structure.

good luck
-pete
 
Pete,

I could use a second or two with the implementation. As shown below I've implemented the createprocess and created a heap area. It works, however how do I set up the handle to the output stream correctly and then copy the console data -=via the output handle- to the heap area.

Thanks, Jerry

// Time.cpp : Defines the entry point for the application.
//
#include <windows.h>
#include &quot;stdafx.h&quot;
#include <Shellapi.h>
#include &quot;Time.h&quot;

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
int iReturn=0;
//Declare and execute the shellexecute api
HWND hwnd = NULL;
LPCTSTR lpVerb = NULL;
LPCTSTR lpFile = TEXT(&quot;cmd.exe&quot;);
LPCTSTR lpParameters = &quot; /k time /t&quot;;
LPCTSTR lpDirectory = &quot;c:\\windows\\system32\\&quot;;
INT nShowCmd = SW_SHOWNORMAL;
HINSTANCE hReturn;
hReturn=ShellExecute(hwnd, lpVerb, lpFile, lpParameters, lpDirectory, nShowCmd);


//2nd Try
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

//Set so stdouput
si.dwFlags=STARTF_USESTDHANDLES;

HANDLE hHeap;
TCHAR *sConsoleOutput;
hHeap=GetProcessHeap();
sConsoleOutput=(TCHAR *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,(sizeof(TCHAR)*MAX_PATH));

//Set so stdouput
si.dwFlags=STARTF_USESTDHANDLES;
si.hStdOutput=hHeap;

// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
&quot;c:\\windows\\system32\\cmd.exe /k time /t&quot;, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
TRUE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
MessageBox(NULL,&quot;2nd Process Message&quot;,&quot;CreateProcess failed.&quot;,MB_OK );
}

// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );

// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );


HeapFree(hHeap,NULL,sConsoleOutput);
return (iReturn);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top