Skip to content

Commit 1309a00

Browse files
committed
文件读取接口也优化下
1 parent a14d176 commit 1309a00

1 file changed

Lines changed: 29 additions & 26 deletions

File tree

DmMain/src/Common/DMHelper.cpp

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ namespace DM
1111
HMODULE hMod = GetModuleHandleW(NULL);
1212
if (NULL == hMod)
1313
{
14-
LOG_ERR("[mid]hMod is NULL\n");
15-
break;
14+
LOG_ERR("[mid]hMod is NULL\n");
15+
break;
1616
}
1717

18-
wchar_t szPath[MAX_PATH] = {0};
18+
wchar_t szPath[MAX_PATH] = { 0 };
1919
if (NULL == GetModuleFileNameW(hMod, szPath, MAX_PATH))
2020
{
2121
LOG_ERR("[mid]GetModuleFileNameW fail\n");
2222
break;
2323
}
2424

25-
if (wcslen(szPath)>=dwSize)
25+
if (wcslen(szPath) >= dwSize)
2626
{
2727
LOG_ERR("[mid]dwSize too small\n");
2828
break;
@@ -35,10 +35,10 @@ namespace DM
3535
return bRet;
3636
}
3737

38-
bool GetRootDirW(wchar_t *pszPath, DWORD dwSize)
38+
bool GetRootDirW(wchar_t* pszPath, DWORD dwSize)
3939
{
4040
bool bRet = false;
41-
do
41+
do
4242
{
4343
HMODULE hMod = GetModuleHandleW(NULL);
4444
if (NULL == hMod)
@@ -47,21 +47,21 @@ namespace DM
4747
break;
4848
}
4949

50-
wchar_t szPath[MAX_PATH] = {0};
50+
wchar_t szPath[MAX_PATH] = { 0 };
5151
if (NULL == GetModuleFileNameW(hMod, szPath, MAX_PATH))
5252
{
5353
LOG_ERR("[mid]GetModuleFileNameW fail\n");
5454
break;
5555
}
56-
wchar_t *p = wcsrchr(szPath,L'\\');
56+
wchar_t* p = wcsrchr(szPath, L'\\');
5757
if (NULL == p)
5858
{
5959
LOG_ERR("[mid]wcsrchr fail\n");
6060
break;
6161
}
62-
*(++p)='\0';
62+
*(++p) = '\0';
6363

64-
if (wcslen(szPath)>=dwSize)
64+
if (wcslen(szPath) >= dwSize)
6565
{
6666
LOG_ERR("[mid]dwSize too small\n");
6767
break;
@@ -74,16 +74,16 @@ namespace DM
7474
return bRet;
7575
}
7676

77-
bool GetRootFullPath(const wchar_t *pszSrcPath, wchar_t *pszDestPath, DWORD dwSize)
77+
bool GetRootFullPath(const wchar_t* pszSrcPath, wchar_t* pszDestPath, DWORD dwSize)
7878
{
7979
bool bRet = false;
80-
do
80+
do
8181
{
82-
if (NULL == pszSrcPath||NULL == pszDestPath ||0 >= dwSize)
82+
if (NULL == pszSrcPath || NULL == pszDestPath || 0 >= dwSize)
8383
{
8484
break;
8585
}
86-
wchar_t szExeDir[MAX_PATH] = {0};
86+
wchar_t szExeDir[MAX_PATH] = { 0 };
8787
if (!GetRootDirW(szExeDir, MAX_PATH))
8888
{
8989
LOG_ERR("[mid]-GetRootDirW fail\n");
@@ -101,17 +101,21 @@ namespace DM
101101
return bRet;
102102
}
103103

104-
bool CheckFileExistW(const wchar_t *pszFilePath)
104+
bool CheckFileExistW(const wchar_t* pszFilePath)
105105
{
106106
WIN32_FILE_ATTRIBUTE_DATA fattr = { 0 };
107107
return GetFileAttributesExW(pszFilePath, GetFileExInfoStandard, &fattr) && 0 == (fattr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
108108
}
109109

110-
DWORD GetFileSizeW(const wchar_t *pszFilePath)
110+
DWORD GetFileSizeW(const wchar_t* pszFilePath)
111111
{
112-
struct _stat64 st;
113-
if (0 == _wstat64(pszFilePath, &st))
114-
return st.st_size;
112+
WIN32_FILE_ATTRIBUTE_DATA fattr = { 0 };
113+
LARGE_INTEGER size;
114+
if (GetFileAttributesExW(pszFilePath, GetFileExInfoStandard, &fattr) && 0 == (fattr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
115+
size.LowPart = fattr.nFileSizeLow;
116+
size.HighPart = fattr.nFileSizeHigh;
117+
return size.QuadPart;
118+
}
115119
return 0;
116120
}
117121

@@ -127,14 +131,13 @@ namespace DM
127131
break;
128132
}
129133

130-
FILE *fp = NULL;
131-
_wfopen_s(&fp, pszFilePath, L"rb");
132-
if (!fp)
133-
{
134+
HANDLE fileHandle = ::CreateFileW(pszFilePath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, nullptr);
135+
if (fileHandle == INVALID_HANDLE_VALUE)
134136
break;
135-
}
136-
dwReadSize = (DWORD)fread(*ppBuf, 1, dwSize, fp);
137-
fclose(fp);
137+
138+
DWORD bytesRead = 0;
139+
dwReadSize = ::ReadFile(fileHandle, *ppBuf, dwSize, &bytesRead, nullptr);
140+
CloseHandle(fileHandle);
138141

139142
bRet = true;
140143
} while (FALSE);

0 commit comments

Comments
 (0)