首页  人才招聘  项目交易  BLOG  兴趣小组  图书  资讯  文章  下载  源码  网友作品  刻盘服务 

您所在位置:论坛首页 — 如何让WinMain程序像main程序一样在console中输入输出
打印本页 保存页面
 如何让WinMain程序像main程序一样在console中输入输出


我看了sdk中的一些例子,基本上也走通了,但还有几个难点,希望高人指教。
如果用AllocConsole,会产生一个新窗口,我不想产生新窗口。
如果用AttachConsole, 看说明大概是这个目的的,但AttachConsole 只在Windows XP and Windows .NET Server上可用,98,2000的好像都不可用。
所以第一个问题就是我就是想自己实现AttachConsole这种功能。一般就是attach 到cmd 这个进程。 
第二个问题就是我想在WinMain程序中直接用printf,cout这些东西,当然如果实在不行也没关系,我用api也行了。


 回复内容
【Xcoder】:
thanks very much

【xazl】:
LZ说的没错啊
To compile an application that uses this function, define _WIN32_WINNT as 0x0500 or later. For more information, see Using the SDK Headers.

Requirements
Client Requires Windows Vista or Windows XP. 
Server Requires Windows Server "Longhorn" or Windows Server 2003. 
Header Declared in Wincon.h; include Windows.h.
 
Library Use Kernel32.lib.
 
DLL Requires Kernel32.dll.  


【xazl】:
LZ如果是为了方便调试呢,那就是说只需要输出就可以了,这样就可以使用OutputDebugString,使用DebugView来捕获。

【lixiaosan】:
http://www.codeproject.com/dialog/quickwin.asp
http://www.codeguru.com/Cpp/misc/misc/article.php/c277

【xazl】:
{
startConsoleWin();

ConsolePrintf("%d%s",10,"haha");

char  * ch = new char[1024];

int len = 0;
ConsoleScanf(ch,len);
AfxMessageBox(ch);

}



#ifdef _DEBUG
    HANDLE __hStdOut = NULL;
    HANDLE __hStdIn = NULL;
#endif


void startConsoleWin(int width, int height, char* fname)
{
#ifdef _DEBUG
AllocConsole();
SetConsoleTitle("Debug Window");
__hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
__hStdIn  = GetStdHandle(STD_INPUT_HANDLE);


COORD co = {width,height};
SetConsoleScreenBufferSize(__hStdOut, co);

#endif
}

BOOL ConsoleScanf(char *fmt,int & len)
{
#ifdef _DEBUG

DWORD cCharsRead = 1024;
BOOL flag = false;

if(__hStdOut)
flag = ReadConsole(__hStdIn, fmt, 1024,(DWORD*) &len, NULL);

if (flag)
{
fmt[len] = 0;
}

return true;
#else
return false;
#endif
}


BOOL ConsolePrintf(char *fmt, ...)
{
#ifdef _DEBUG
char s[300];
va_list argptr;

BOOL flag = false;

va_start(argptr, fmt);
vsprintf(s, fmt, argptr);
va_end(argptr);

DWORD cCharsWritten;

if(__hStdOut)
flag = WriteConsole(__hStdOut, s, strlen(s), &cCharsWritten, NULL);

return flag;
#else
return FALSE;
#endif
}



【Xcoder】:
感谢楼上两位的参与,但是你们好像都没看清我的需求。
xazl(西北狼) 你最终用的是AllocConsole(), 这个会产生新窗口的。
lixiaosan(小三) 你那两个例子和我的需求好像正好是相反的,现在cmd是父进程,我的程序是子进程,显然我是不能改变cmd的。
main程序好像自然就能达到这种需求,只是不知道他怎么实现的。

【xazl】:
我看到你的需求了,但是不会,呵呵。

【pomelowu】:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcsample98/html/vcsmpconsole.asp

【hijack】:
通过“管道”应该能实现楼主的需求

广告
 
最新招聘信息

关于本站 - 网站导航 - 联系站长 - BUG报告 - 友情链接 - 赞助本站
Copyright© 1999-2008 Programfan.com. All Rights Reserved
论坛制作&维护:Hannibal    Email: webmaster@pfan.cn
最佳浏览效果:IE6.0+ 或 FireFox 1.5+ 分辨率:1024*768