Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
FoxPro writing to Windows Console
Message
General information
Forum:
Visual FoxPro
Category:
Windows API functions
Miscellaneous
Thread ID:
01528144
Message ID:
01528165
Views:
92
Likes (1)
Thanks I actually managed to figure out how to write to FoxPro's console with some C++ code - which should also work through FoxPro I guess and without a seperate window.
#include <windows.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <iostream>
#include <fstream>

BOOL WINAPI WriteToConsole(char *input)
{	
	BOOL bConsole = AttachConsole(ATTACH_PARENT_PROCESS) != FALSE;

	if (bConsole)
	{
		int fd = 0;
		long lStdOut;
		lStdOut = (long)GetStdHandle( STD_ERROR_HANDLE); // STD_OUTPUT_HANDLE);
		fd = _open_osfhandle(lStdOut, _O_TEXT);
		if (fd > 0)
		{
			*stdout = *_fdopen(fd, "w");
			setvbuf(stdout, NULL, _IONBF, 0 );
		}
	}

	printf(input);	
	FreeConsole();
	return TRUE;
}
Unfortunately the problem isn't writing output to *any* console, but writing output to the main console that VFP starts when the EXE starts.

The problem with that is that a Windows app effectively doesn't a Console that sticks around because the app starts, enters the Windows event loop, then exits back to the command prompt while the event loop keeps running.

What I need is to be able to write messages into the startup console so that I can communicate status back to a calling application or support output redirection (ie. MyApp.exe > stdoutcapture.txt).

I'm realizing that this probably isn't going to be possible from within VFP because by the time any Fox code runs VFP has already exited back to the command prompt. I'm able to write to the console using the code above but the content cannot be captured by a calling application (MyApp.exe > stdoutcapture.txt reusults in a 0 length file).

In the end I think my solution will to create a small Console app wrapper front end that automates my app's COM server. Ugly but I know that'll work :-)

If anybody sees a way that this could work from within Fox I'd love to hear about it!!!



+++ Rick ---

>This one is even simpler. But a console window cannot be closed gracefully. I have not checked for a solution yet.
>
>
>* Using Win32 Functions in Visual FoxPro
>* example=119
>* Saying "Hello World!" with VFP and WinAPI
>
>
>#DEFINE STD_OUTPUT_HANDLE -11
>
>DECLARE INTEGER AllocConsole IN kernel32
>DECLARE INTEGER GetConsoleWindow IN kernel32
>DECLARE INTEGER GetStdHandle IN kernel32 LONG nStdHandle
>
>DECLARE INTEGER ShowWindow IN user32 AS ShowWindowA;
>	INTEGER hWindow, INTEGER nCmdShow
>
>DECLARE INTEGER WriteConsole IN kernel32;
>	INTEGER hConsoleOutput, STRING @lpBuffer,;
>	INTEGER nCharsToWrite, INTEGER lpCharsWritten,;
>	INTEGER lpReserved
>
>= AllocConsole()
>= ShowWindowA(GetConsoleWindow(), 1)
>= WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "Hello World!", 12,0,0)
>
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Previous
Reply
Map
View

Click here to load this message in the networking platform