Last active
November 8, 2018 06:13
-
-
Save comoc/d80ec9c174d4808bad470408444912c3 to your computer and use it in GitHub Desktop.
WindowsのGUIアプリやDLL内からのprintfを画面表示する方法 ref: https://qiita.com/comocc/items/4604bea440018dfb5bd1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdio.h> | |
__declspec(dllexport) void __cdecl Function1(void) { | |
printf_s("%s\n", __func__); /* 標準出力 */ | |
/* または */ | |
fprintf_s(stdout, "%s\n", __func__); /* 標準出力 */ | |
} | |
__declspec(dllexport) void __cdecl Function2(void) { | |
fprintf_s(stderr, "%s\n", __func__); /* 標準エラー出力 */ | |
} | |
/* 新規にコンソールを開く */ | |
void CreateConsole(void) { | |
FILE* fp; | |
AllocConsole(); | |
freopen_s(&fp, "CONOUT$", "w", stdout); /* 標準出力(stdout)を新しいコンソールに向ける */ | |
freopen_s(&fp, "CONOUT$", "w", stderr); /* 標準エラー出力(stderr)を新しいコンソールに向ける */ | |
} | |
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { | |
switch (fdwReason) { | |
case DLL_PROCESS_ATTACH: | |
CreateConsole(); | |
break; | |
} | |
return TRUE; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment