AllocConsole() 不显示 cout

2021-12-25 00:00:00 dll c++

我有一个 DLL,我在其中使用 AllocConsole() 和 cout 来显示用于调试目的的数据.
它曾经工作得很好,但自从我将编译器 (Visual Studio 2012) 更新到最新版本后,dll 只显示控制台,而不显示打印/输出.
我不知道为什么会这样.
有什么想法吗?

I have a DLL where I use AllocConsole() and cout to display data for debugging purposes.
It used to work fine but since I updated my compiler (Visual Studio 2012) to the latest the dll just shows the console but not the prints/couts.
I am out of idea's as to why this is happening.
Any idea's?

我的部分代码

__declspec(dllexport) INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved)
{
    switch(Reason)
    {
    case DLL_PROCESS_ATTACH:    
        AllocConsole();

        DisableThreadLibraryCalls(hDLL);

        //
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourAttach(&(PVOID&)pSend, MySend);
        if(DetourTransactionCommit() == NO_ERROR)
             cout << "[" << MySend << "] successfully detoured." << endl;

但没有显示任何内容.

推荐答案

我依稀记得您可能需要将标准输出重定向到控制台.不过我可能是错的(因为你的代码更早运行):

I vaguely recall that you might need to redirect the stdout to the console. I might be wrong though (since you had your code working earlier):

AllocConsole();
freopen("CONOUT$", "w", stdout);
std::cout << "This works" << std::endl;

相关文章