我发生了一些奇怪的事情,其中 printf 命令(在类构造函数内)没有输出到控制台窗口。
你知道为什么会这样吗?
一些相关信息:
- 我的项目是本机 Win32 窗口应用程序(不是 Win32 控制台项目)
- 我使用 AllocConsole() 和 _open_osfhandle() 打开控制台窗口。
- 我从中调用 printf 命令的构造函数是 Singleton 类的一部分,在私有构造函数中我调用静态函数 isTVManagerTaskScheduled()。
- 如果我在构造函数之外使用 printf ,那么它可以正常工作,即将数据打印到控制台窗口。
- 控制台窗口仅用于调试我的 Win32 应用程序。
- 我正在使用 C++ Visual Studio 2010 Express
- 如果我不从构造函数中调用静态函数 isTVManagerTaskScheduled() 那么 printf 的工作正常。
你知道为什么会这样吗?
我的代码:
// Public Static Class Variables //
const tstring TVManager::TASK_NAME = _T("TV Manager");
const tstring TVManager::TASK_TIME_STAMP = _T("2012-03-22T13:46:00");
// Private constructor
TVManager::TVManager(HWND hwnd)
{
mainHwnd = hwnd;
bool res = isTVManagerTaskScheduled();
std::cout << "Res: " << res << endl; // does not print to console
_tprintf(_T("RES: %d\n"), res); // does not print to console
if (!res) {
_tprintf(_T("hit\n"));
EasyTaskScheduler::ScheduleTaskAtLogon(TASK_NAME, CPP_Utilities::getProcessPath(), TASK_TIME_STAMP);
}
}
// Public Static function //
bool TVManager::isTVManagerTaskScheduled()
{
std::vector <tstring> curTasks = EasyTaskScheduler::RetrieveScheduledTasks();
tstring defTaskName = CPP_Utilities::toLower( TASK_NAME );
for (int i=0; i<=curTasks.size(); i++) {
tstring task = CPP_Utilities::toLower(curTasks.at(i));
// The following printf doesn't get printed to console
_tprintf(_T("size %d, Task %d: %s\n"), curTasks.size(), i, task.c_str());
if (task.find( defTaskName ) != npos) {
_tprintf(_T("returning true\n"));
return true;
}
}
_tprintf(_T("returning false\n"));
return false;
}
// Public static function
TVManager* TVManager::getInstance(HWND hwnd)
{
static TVManager instance(hwnd);
return &instance;
}
// Usage: Inside main window proceedure
case WM_CREATE:
{
CPP_Utilities::openConsoleWindow();
tvManager = TVManager::getInstance(hwnd);
}
break;