释放代码如下:
bool FreeExeResource()
{
DWORD dwWritten = 0;
HMODULE hInstance = ::GetModuleHandle(NULL);
// Find the binary file in resources
// 添加资源的过程:
// 在资源上选择添加资源-〉自定义-〉"bin"-〉添加资源-〉导入-〉*.exe
// 保存后.rc文件自动加入一行:IDR_BIN2 BIN "MiniControl.exe"
HRSRC hSvcExecutableRes = ::FindResource(hInstance, MAKEINTRESOURCE(IDR_BIN2), _T("BIN"));
HGLOBAL hSvcExecutable = ::LoadResource(hInstance, hSvcExecutableRes);
LPVOID pSvcExecutable = ::LockResource(hSvcExecutable);
if(pSvcExecutable == NULL)
return FALSE;
DWORD dwSvcExecutableSize = ::SizeofResource(hInstance, hSvcExecutableRes);
TCHAR szSvcExePath[_MAX_PATH];
strcpy(szSvcExePath, "tmpExe.exe");
HANDLE hFileSvcExecutable = CreateFile(szSvcExePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hFileSvcExecutable == INVALID_HANDLE_VALUE)
return FALSE;
WriteFile(hFileSvcExecutable, pSvcExecutable, dwSvcExecutableSize, &dwWritten, NULL);
CloseHandle(hFileSvcExecutable);
return dwWritten == dwSvcExecutableSize;
}
启动一个进程可以使用:
ShellExecute
WinExec
CreateProcess