Jump to content

Printing windowed application output in bat file


Josh
 Share

Recommended Posts

When Leadwerks Editor runs a game compiled as a Windowed application (no console) it successfully prints the game's output, even though there is no console.

 

When a bat file is used to do the same thing, no text is printed. Is there a way to do this?

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

No, I mean actually print / echo the output of a winnowed application. The console of a bat file only shows the output of a console application. It does not show the output of a windowed application, even though Leadwerks editor shows it, and it can be printed to a text file with a bat file. So what is the default > setting to make a bat file actually show the text the program it runs?

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Oh, I don't know... I feel like if the output isn't already being redirected it should print to the console by default. I don't know how you'd do this if it's not already doing it, sorry.

 

Edit:

-Keep in mind if the application doesn't open it's own console it has no object to pass stdout to... it'd be NULL, so unless you could set from C++ the console to redirect stdout to (the one created by the batch file) this might not be do-able.

If you wanted to build it right into the engine you could create a console and redirect stdout and what not, stderr, etc... but other then that idk.

Link to comment
Share on other sites

Hi Josh,

A couple of years ago I made my dedicated server (app) for minecraft=), there for interception from the console I used pipes. But, this is works only in windows.

#include <windows.h>
#include <iostream>
#include <string>
#include <stdio.h>

#pragma warning(disable : 4996) // For GetVersionEx

#define bzero(a) memset(a,0,sizeof(a))

char buf[1024];
STARTUPINFO si;
SECURITY_ATTRIBUTES sa;
SECURITY_DESCRIPTOR sd;
PROCESS_INFORMATION pi;
HANDLE newstdin, newstdout, read_stdout, write_stdin;
unsigned long bread;
unsigned long avail;

bool IsWinNT()
{
OSVERSIONINFO osv;
osv.dwOSVersionInfoSize = sizeof(osv);
GetVersionEx(&osv);
return (osv.dwPlatformId == VER_PLATFORM_WIN32_NT);
}
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
bool CreateAnonymousPipes(std::string procName)
{
if (IsWinNT())
{
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&sd, true, NULL, false);
sa.lpSecurityDescriptor = &sd;
}
else {
sa.lpSecurityDescriptor = NULL;
}
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = true;
//CreatePipe(&newstdin, &write_stdin, &sa, 0); //stdin
CreatePipe(&read_stdout, &newstdout, &sa, 0); //stdout
GetStartupInfo(&si);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
si.hStdOutput = newstdout;
si.hStdError = newstdout;
//si.hStdInput = newstdin;
std::wstring stemp = std::wstring(procName.begin(), procName.end());
if (CreateProcess(stemp.c_str(), NULL, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, π)) {
return true;
}
bzero(buf);
return false;
}
// Child process stdout
void ReceiveChild()
{
PeekNamedPipe(read_stdout, buf, 1023, &bread, &avail, NULL);
if (bread != 0)
{
bzero(buf);
if (avail > 1023)
{
while (bread >= 1023)
{
ReadFile(read_stdout, buf, 1023, &bread, NULL);
bzero(buf);
}
}
else {
ReadFile(read_stdout, buf, 1023, &bread, NULL);
if (buf != "") {
std::cout << buf << std::endl; // STDOUT
}
}
}
}
int main(int argc, char** argv)
{
std::string filename;
if (argc > 1) {
std::cout << argv[1] << std::endl;
filename = argv[1];
}

bool start = CreateAnonymousPipes(filename); // Windowed app start
while (start) {
Sleep(1);
ReceiveChild();
}
return 0;
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...