6.如何直接运行一个资源中的程序
bool Run()
{
CFile f;
char* pFileName = "Execution.exe";
if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, NULL ) )
{
AfxMessageBox("Can not create file!");
return 0;
}
CString path = f.GetFilePath();
HGLOBAL hRes;
HRSRC hResInfo;
//获取应用实例
HINSTANCE insApp = AfxGetInstanceHandle();
//寻找EXE资源名
hResInfo = FindResource(insApp,(LPCSTR)IDR_EXE4,"EXE");
hRes = LoadResource(insApp,hResInfo ); // Load it
DWORD dFileLength = SizeofResource( insApp, hResInfo ); //计算EXE文件大小
f.WriteHuge((LPSTR)hRes,dFileLength); //写入临时文件
f.Close();
HINSTANCE HINSsd = ShellExecute(NULL, "open",path, NULL, NULL,
SW_SHOWNORMAL);> //运行它.
return 1;
} |
7.如何遍历整个目录
#include <windows.h>
#include <shlobj.h>
//浏览目录.
void BrowseFolder( void )
{
TCHAR path[MAX_PATH];
BROWSEINFO bi = { 0 };
bi.lpszTitle = ("递归调用所有目录");
LPITEMIDLIST pidl = SHBrowseForFolder ( &#38;bi );
if ( pidl != 0 )
{
// 获取目录路径
SHGetPathFromIDList ( pidl, path );
//设置为当前路径
SetCurrentDirectory ( path );
//搜索所有子目录
SearchFolder( path );
// 释放内存
IMalloc * imalloc = 0;
if ( SUCCEEDED( SHGetMalloc ( &#38;imalloc )) )
{
imalloc->Free ( pidl );
imalloc->Release ( );
}
}
//搜索其下所有子目录及文件.
void SearchFolder( TCHAR * path )
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
TCHAR filename[ MAX_PATH + 256 ];
TCHAR pathbak[ MAX_PATH ];
//复制初始用户选择目录
strcpy( pathbak, path );
//寻找第一个文件
hFind = FindFirstFile ( "*.*", &#38;FindFileData );
//搜索所有文件及子目录
do
{
if ( hFind != INVALID_HANDLE_VALUE )
{
//如果是当前目录或父目录,跳过
if ( ! ( strcmp( FindFileData.cFileName, "." ) ) ||
! ( strcmp( FindFileData.cFileName, ".." ) ) )
{
continue;
}
//恢复初始用户选择目录
strcpy( path, pathbak );
//列出所有发现的文件
sprintf( path, "%s\\%s", path, FindFileData.cFileName );
//如果 SetCurrentDirectory
成功的话,则它是一个目录,递归调用继续搜索子目录
if ( ( SetCurrentDirectory( path ) ) )
{
SearchFolder( path );
}
//插入文件及路径名到列表框m_listbox_hwnd中
SendMessage( m_listbox_hwnd, LB_ADDSTRING, 0, path );
//<--INSERT WHAT YOU WANT DONE HERE!
}
}
while ( FindNextFile ( hFind, &#38;FindFileData ) &#38;&#38; hFind !=
INVALID_HANDLE_VALUE );
FindClose ( hFind );
} |
查看本文来源