- 注册时间
- 2007-12-26
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 92615
- 在线时间
- 小时
|
楼主 |
发表于 2010-8-31 09:07:01
|
显示全部楼层
搜到一英文网站: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
- }
复制代码 |
|