E
extrinsic
Unregistered / Unconfirmed
GUEST, unregistred user!
我想在NT4.0下打开1.6G文件的File Mapping, 用Delphi实现如下:
var
FileHandle, MapHandle: Integer;
MemPointer: Pointer;
begin
// Open the file
FileHandle := FileOpen(FileName, fmOpenRead or fmShareDenyWrite);
if FileHandle < 0 then
raise EFOpenError.CreateResFmt(@SFOpenError, [FileName]);
// Create file map
try
// Open the file mapping
MapHandle := CreateFileMapping(FileHandle, nil, PAGE_READONLY, 0, 0, nil);
if MapHandle = 0 then
raise Exception.CreateResFmt(@SFMapError, [FileName]); // 此处出错
finally
FileClose(FileHandle);
end;
// View file map
try
MemPointer := MapViewOfFile(MapHandle, FILE_MAP_READ, 0, 0, 0); // note: the last but one parameters are not determined
if MemPointer = nil then
raise Exception.CreateResFmt(@SFMapViewError, [FileName]);
finally
CloseHandle(MapHandle);
end;
Result := MemPointer;
end;
但是执行到CreateFileMapping返回得到的总是0, 调用GetLastError返回错误号8,即
内存不够。但是我在同一操作系统下,用VC代码实现就可以正常打开, 如下:
HANDLE hFile, hFileMapping;
LPBYTE pFileData;
DWORD dwIndex, dwSize, dwError;
CString str;
hFile = CreateFile(m_strFile, GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
if (hFile == INVALID_HANDLE_VALUE) {
dwError = GetLastError();
str.Format("Cannot open the file: %d", dwError);
AfxMessageBox(str);
return;
}
// Get File Size
dwSize = GetFileSize(hFile, NULL);
// Open File Mapping for the DLL file
hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (hFileMapping == NULL) {
dwError = GetLastError();
str.Format("Cannot map the DLL file: %d", dwError);
AfxMessageBox(str);
return;
}
// Map view of file and get start pointer
pFileData = (LPBYTE)MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
if (pFileData == NULL) {
dwError = GetLastError();
str.Format("Cannot create map view of the file: %d", dwError);
AfxMessageBox(str);
return;
}
二者同是调用API,为什么Dephi不行而 VC可以呢?我用的是Delphi 6.0, VC 6.0 + sp5
var
FileHandle, MapHandle: Integer;
MemPointer: Pointer;
begin
// Open the file
FileHandle := FileOpen(FileName, fmOpenRead or fmShareDenyWrite);
if FileHandle < 0 then
raise EFOpenError.CreateResFmt(@SFOpenError, [FileName]);
// Create file map
try
// Open the file mapping
MapHandle := CreateFileMapping(FileHandle, nil, PAGE_READONLY, 0, 0, nil);
if MapHandle = 0 then
raise Exception.CreateResFmt(@SFMapError, [FileName]); // 此处出错
finally
FileClose(FileHandle);
end;
// View file map
try
MemPointer := MapViewOfFile(MapHandle, FILE_MAP_READ, 0, 0, 0); // note: the last but one parameters are not determined
if MemPointer = nil then
raise Exception.CreateResFmt(@SFMapViewError, [FileName]);
finally
CloseHandle(MapHandle);
end;
Result := MemPointer;
end;
但是执行到CreateFileMapping返回得到的总是0, 调用GetLastError返回错误号8,即
内存不够。但是我在同一操作系统下,用VC代码实现就可以正常打开, 如下:
HANDLE hFile, hFileMapping;
LPBYTE pFileData;
DWORD dwIndex, dwSize, dwError;
CString str;
hFile = CreateFile(m_strFile, GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
if (hFile == INVALID_HANDLE_VALUE) {
dwError = GetLastError();
str.Format("Cannot open the file: %d", dwError);
AfxMessageBox(str);
return;
}
// Get File Size
dwSize = GetFileSize(hFile, NULL);
// Open File Mapping for the DLL file
hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (hFileMapping == NULL) {
dwError = GetLastError();
str.Format("Cannot map the DLL file: %d", dwError);
AfxMessageBox(str);
return;
}
// Map view of file and get start pointer
pFileData = (LPBYTE)MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
if (pFileData == NULL) {
dwError = GetLastError();
str.Format("Cannot create map view of the file: %d", dwError);
AfxMessageBox(str);
return;
}
二者同是调用API,为什么Dephi不行而 VC可以呢?我用的是Delphi 6.0, VC 6.0 + sp5