




/***************************
由于这是win32控制台应用程序,添加了资源后,
在stafx.h中要#include "resource.h"
并且该程序用到了MFC中的类
选择工程(项目)属性,修改一下MFC的使用,在共享dll中使用MFC
项目 ---- 属性 ---- 配置属性 ---- 常规 -----MFC使用 ---在共享DLL中使用MFC
然后还需要包含头文件afxwin.h
最后程序就可以运行了
程序运行时,会自动在当前目录下生成一个out.doc文件,并自动启动Word打开它。
注意ShellExecute()函数的使用,它用以执行一条系统命令,如打开某个文件。
***************************/ #include "stdafx.h" #include <afxwin.h> CWinApp theApp; int _tmain(int argc, _TCHAR* argv[]) { int nRetCode = 0; if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { _tprintf(_T("错误: MFC 初始化失败\n")); nRetCode = 1; } else { HRSRC hRes = FindResource(theApp.m_hInstance, MAKEINTRESOURCE(IDR_WORD1), _T("WORD")); DWORD len = SizeofResource(theApp.m_hInstance, hRes); HGLOBAL hg = LoadResource(theApp.m_hInstance, hRes); LPVOID lp = (LPSTR)LockResource(hg); //将资源中的内容保存至某个Word文件中 CFile file; file.Open(_T("out.doc"), CFile::modeWrite | CFile::modeCreate); char * cp = (char *)lp; for(int i = 0; i < len; i++) { file.Write(cp++, 1); } CString filePath = file.GetFilePath(); file.Close(); //打开该文件 ShellExecute(NULL, _T("open"), filePath, NULL, NULL, SW_SHOW); FreeResource(hg); } return nRetCode; }
