Sign in to follow this  
Ternary

Redirecting standard output & Possible forum bug

Recommended Posts

Since I am unable to post my question I shall post this filler and attempt to edit it. Edit: didn't work, I'll trying posting it as a reply Edit2: That didn't work either. Okay this is weird. I tried to post a question but got a "this document contains no data" message upon pressing the submit button instead. So I copied the text and tried a few more times; thinking it was a temporary problem. But it seems I just can't post this exact body of text. I'll try posting it a little bit at a time. [Edited by - Ternary on September 1, 2005 1:51:32 PM]

Share this post


Link to post
Share on other sites
This is the original quesiton:

I'm trying to use a console application like a function. Specifically, I'm trying to launch a console app from a windows app and read everything the console app posts into its standard output (e.g. wprintf, wcout). I tried following an example from msdn, but I'm not having any luck.

This is my code so far
SECURITY_ATTRIBUTES sAttr;
sAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
sAttr.bInheritHandle = true;
sAttr.lpSecurityDescriptor = 0;

HANDLE myRead,childWrite;
if(!CreatePipe(&myRead,&childWrite,&sAttr,400))
{
ErrorBox(L"could not create a pipe");
return;
}
SetHandleInformation(myRead,HANDLE_FLAG_INHERIT,0);

HANDLE childRead,myWrite;
if(!CreatePipe(&childRead,&myWrite,&sAttr,0))
{
CloseHandle(myRead);
CloseHandle(childWrite);
ErrorBox(L"could not create a second pipe");
return;
}
SetHandleInformation(myWrite,HANDLE_FLAG_INHERIT,0);

STARTUPINFO startupInfo = {0};
startupInfo.cb = sizeof(STARTUPINFO);
startupInfo.dwFlags = STARTF_USESTDHANDLES;
startupInfo.hStdInput = childRead;
startupInfo.hStdOutput = childWrite;
startupInfo.hStdError = childWrite;

PROCESS_INFORMATION processInfo;

if(!CreateProcess(L"C:\\WINXP2\\system32\\cmd.exe",0,0,0,true,0,0,0,&startupInfo,&processInfo))
{
CloseHandle(myRead);
CloseHandle(myWrite);
CloseHandle(childRead);
CloseHandle(childWrite);
ErrorBox(L"cmd.exe could not be executed");
return;
}
CloseHandle(processInfo.hThread);

WaitForSingleObject(processInfo.hProcess,INFINITE);

CloseHandle(processInfo.hProcess);
CloseHandle(childWrite);
CloseHandle(childRead);
CloseHandle(myWrite);

wchar_t buffer[201];
buffer[200] = 0;
DWORD numRead;
ReadFile(myRead,(LPVOID)&buffer,200*sizeof(wchar_t),&numRead,0);
CloseHandle(myRead);

MessageBox(hWnd,buffer,L"caption",MB_OK);



But the final output is garbage (looks like a row of boxes).

Can somebody steer me in the right direction?

p.s. I'm just using _cmd.exe for testing purposes.

[Edited by - Ternary on September 1, 2005 9:03:40 AM]

Share this post


Link to post
Share on other sites
I have almost isolated the problem. I can't post the last little bit.

Edit: Looks like I can't add another 'e' or 'E'. Although I can add other letters.

Edit2: The problem is isolated. It seems I cannot post space-see-em-dee-dot-ee-ex-ee. I really would love to know the reason behind this.

[Edited by - Ternary on September 1, 2005 8:09:56 AM]

Share this post


Link to post
Share on other sites
[SEMI_EDIT] Hmm... It looks like I can't post that dreaded cmd line executable either...

Can't say I know why that would be, but I do know that a while back I was trying to create a thread, and it would never take the message contents, only the title. It definitely pissed me off [grin] Anyway, you might want to PM one of the staff, it sounds like it might just be a forum bug, but even if it isn't, I'm sure they could give you an answer.

As for your question, I wish I could help, but I have very littel (read: next to none) knowledge in that area...

Share this post


Link to post
Share on other sites
GAH! What a huge oversight! The output wasn't garbage. It was perfectly intelligable when I tried
MessageBoxA(hWnd,(char*)buffer,"caption",MB_OK);

What's going on here? The same thing happens with my own console program which was definitely written with unicode strings.


Anyway, I thought for sure a moderator or staff member would have noticed this thread by now and explained or at least acknowledged the_cmd.exe issue. A PM it is, then.

[Edited by - Ternary on September 2, 2005 1:02:22 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this