|
发表于 2024-3-4 20:51:55
|
显示全部楼层
搜到一英文网站:How to detect console vs GUI mode from a DLL,里面介绍了两种方案:- #include <windows.h>#include <tchar.h>int main(){ char *base = (char*)GetModuleHandle(0); // "returns a handle to the file used to create the calling process (.exe file)." IMAGE_DOS_HEADER *idh = (IMAGE_DOS_HEADER*)(base+0); IMAGE_FILE_HEADER *ifh =(IMAGE_FILE_HEADER*)(base+idh->e_lfanew+sizeof(DWORD)); IMAGE_OPTIONAL_HEADER *ioh =(IMAGE_OPTIONAL_HEADER*)(base+idh->e_lfanew+sizeof(DWORD)+sizeof(IMAGE_FILE_HEADER)) ; // const TCHAR *subsystem=0; switch (ioh->Subsystem) { case IMAGE_SUBSYSTEM_UNKNOWN: subsystem=_T("unknown"); break; case IMAGE_SUBSYSTEM_NATIVE: subsystem=_T("native"); break; case IMAGE_SUBSYSTEM_WINDOWS_CUI: subsystem=_T("console"); break; case IMAGE_SUBSYSTEM_WINDOWS_GUI: subsystem=_T("windows"); break; case IMAGE_SUBSYSTEM_OS2_CUI: subsystem=_T("os2"); break; case IMAGE_SUBSYSTEM_POSIX_CUI: subsystem=_T("posix"); break; case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: subsystem=_T("ce"); break; case IMAGE_SUBSYSTEM_XBOX: subsystem=_T("xbox"); break; default: subsystem=_T("???"); } MessageBox(0,subsystem,subsystem,MB_OK); return 0;}
复制代码 以及:- if(!GetConsoleTitle(NULL, 0) && GetLastError() == ERROR_SUCCESS) {// CUI} else {// GUI}
复制代码 |
|