在console程序中使用MFC
转
http://www.cnblogs.com/cvbnm/archive/2011/02/28/1966761.html
在SDK程序中使用MFC中的辅助类
由于MFC中的辅助类,如CFileDialog,CFileFind,CString等使用起来非常的方便 如果用API来完成相应的工作,则需要自己完成大量的重复工作,使用MFC的辅助类 可以节省大量的开发时间,具体方法如下:
1.加入相应的头文件
由于在SDK程序中一定要包含windows.h头文件,所以在使用MFC中的类时, 如加入afx.h一类的头文件会有一个提示与windows.h相冲突,解决的办法是, 去掉windows.h,然后在所有的.h文件前加入
#include "stdafx.h " #include <afxwin.h> // MFC core and standard components #include <afxext.h> // MFC extensions #include <afxdisp.h> // MFC Automation classes #include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls #include <afxcmn.h> |
注意一定要在所有的头文件之前加入这几行,而且顺序最好不要改变,否则会有大量的错误提示
2。 更改编译设置
在Project-> Setting-> General 中选Use MFC in a Shared DLL或者 Use MFC in static Library 并把project-> Setting-> C/C++ 中的Use runing-time library 由Single-Threaded改为相应的Multithreaded
3. LINK : fatal error LNK1104: cannot open file "mfc42ud.lib"
缺少文件,把缺少的文件手动拷贝到VC安装目录即可;mfc42ud.lib (u means unicode、d means debug),去掉unicode也是一个办法。
另外,在使用VS创建console应用时,使用wazard直接选择使用MFC并改用多字符集也可以创建使用MFC控制台程序。
在CWinApp::InitInstance()中添加:
NtQueryInformationProcess = (PROCNTQSIP)GetProcAddress(GetModuleHandle(L"ntdll.dll"),
"NtQueryInformationProcess");
HANDLE hProcess;
PROCESS_BASIC_INFORMATION pbi;
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,GetCurrentProcessId());
NtQueryInformationProcess(hProcess, ProcessBasicInformation, (PVOID)&pbi,
sizeof(PROCESS_BASIC_INFORMATION), NULL);
CloseHandle(hProcess);
AttachConsole(pbi.InheritedFromUniqueProcessId);
freopen("CONOUT$","w",stdout); // 重定向输出
用printf函数就可以把信息输出到启运它的那个CMD窗口。


浙公网安备 33010602011771号