有些时候,我们需要在程序运行的时候查看相关的信息,但又不想输出懂日志文件中,这时可以用控制台输出的方式进行显示信息
void InitConsoleWindow()
{
int nCrt = 0;
FILE* fp;
AllocConsole();
nCrt = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
fp = _fdopen(nCrt, "w");
*stdout = *fp;
setvbuf(stdout, NULL, _IONBF, 0);
}
{
int nCrt = 0;
FILE* fp;
AllocConsole();
nCrt = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
fp = _fdopen(nCrt, "w");
*stdout = *fp;
setvbuf(stdout, NULL, _IONBF, 0);
}
以下红色部分是我初始化函数中添加的
BOOL CSerialPortptestDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
BOOL CSerialPortptestDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
CString str;
int Index;
HKEY hKey;
LONG ret;
OSVERSIONINFO osvi;
BOOL bOsVersionInfoEx;
char keyinfo[100],comm_name[200],ValueName[200];
int i;
DWORD sType,Reserved,cbData,cbValueName;
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
InitConsoleWindow();
int Index;
HKEY hKey;
LONG ret;
OSVERSIONINFO osvi;
BOOL bOsVersionInfoEx;
char keyinfo[100],comm_name[200],ValueName[200];
int i;
DWORD sType,Reserved,cbData,cbValueName;
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
InitConsoleWindow();
printf( "str = %s\n ", "debug");
调用此函数后会弹出一个CONSOLE,然后printf的东西就会出现在上面。
转自:http://blog.chinaunix.net/uid-20672257-id-2973396.html