MFC - 释放文件到指定文件夹

时间:2021-07-22 12:27:50
 1 // 释放文件到指定的文件夹
 2 BOOL ReleaseRes(LPCTSTR szDLLFullPath, UINT uResID, LPCTSTR szResType)
 3 {
 4     if (uResID<=0 || !szResType)
 5     {
 6         return FALSE;
 7     }
 8 
 9     HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCE(uResID), szResType);
10     if ( NULL == hRsrc )
11     {
12         return FALSE;
13     }
14 
15     DWORD dwSize = SizeofResource(NULL, hRsrc);
16     if ( dwSize <= 0)
17     {
18         return FALSE;
19     }
20 
21     HGLOBAL hGlobal = LoadResource(NULL, hRsrc);
22     if ( NULL == hGlobal )
23     {
24         return FALSE;
25     }
26 
27     LPVOID pBuffer = LockResource(hGlobal);
28     if ( NULL ==pBuffer )
29     {
30         return FALSE;
31     }
32 
33     HANDLE hFile = CreateFile(szDLLFullPath, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
34     if (INVALID_HANDLE_VALUE == hFile )
35     {
36         return FALSE;
37     }
38 
39     DWORD dwWrited = 0;
40     if (FALSE == WriteFile(hFile, pBuffer, dwSize, &dwWrited, NULL) )
41     {
42         AfxMessageBox(_T("写入失败!"));
43         return FALSE;
44     }
45 
46     UnlockResource(hGlobal);
47     CloseHandle(hFile);
48     return TRUE;
49 }