释放资源
转载请注明来源:https://www.cnblogs.com/hookjc/
FreeMyData(MAKEINTRESOURCE(IDR_CURTOM1),"custom","c:\\windows\\Test.exe")
BOOL CTestDlg::FreeMyData(LPCTSTR ResName, LPCTSTR ResType, LPCTSTR FileName)
{
BOOL bRet=TRUE;
HINSTANCE hInst=AfxGetInstanceHandle();
//先判断我们指定的资源是否存在
HRSRC hResInfo = FindResource(hInst,ResName,ResType);
if(hResInfo == NULL)
return FALSE;
HANDLE hFile = NULL;
DWORD dwWritten = 0;
//开始调入指定的资源到内存
HGLOBAL hResData = LoadResource(hInst,hResInfo);
LPVOID pvResData = LockResource(hResData);
DWORD dwResSize = SizeofResource(hInst,hResInfo);
//再创建目标文件并保存数据至文件
hFile = CreateFile(FileName,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL);
WriteFile(hFile, pvResData, dwResSize, &dwWritten, NULL);
if(hFile == INVALID_HANDLE_VALUE)//创建文件失败
{
FreeResource(hResData);
return FALSE;
}
//释放有关内存资源
CloseHandle(hFile);
FreeResource(hResData);
return TRUE;
}