c ++ CreateFile cause of memory can not be written error

SzFileName LPCWSTR;
szFileName = argv [2];
// LPCWSTR szFileName = L "test.txt"; // file name can be modified according to their needs, or with variable input
HANDLE hFile = CreateFile (szFileName, GENERIC_READ , FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
// open sharing, to avoid the need to read the rest of this file
IF (INVALID_HANDLE_VALUE = hFile)!
{
DWORD dwFileSize = GetFileSize (hFile, NULL);
PBYTE pBuffer = (PBYTE) malloc (dwFileSize + 2);
int iLen = 0;
IF (the ReadFile (hFile, pBuffer, dwFileSize, & dwFileSize, NULL)!)
{
Free (pBuffer);
return FALSE;
}
// the CloseHandle (hFile);
pBuffer [dwFileSize] = ' \ 0 ';
pBuffer [dwFileSize +. 1] =' \ 0 ';
//std::wstring srcEncode;
srcEncode = DetectEncode(pBuffer, dwFileSize);
CloseHandle(hFile);
free(pBuffer);

 

HANDLE hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

This sentence, reading and writing files to choose FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE mode, otherwise it may lead to repeated opening and closing memory can not be written error that is run through when the exe program, call the debug command finds.

Guess you like

Origin www.cnblogs.com/liuzhaoyzz/p/11410283.html