Michael Russell
2005-11-01 22:24:12 UTC
I am not sure if this is the right place for this question or not. If not,
kindly direct me to the proper group.
I am having a problem with WriteFile() returning an error. I have whittled
this problem down to a short test program in C which fails under WinXP x64,
but works fine on WinXP x86. I see the problem whether I compile for a win32
or a win64 target. The quick summary is that if I use a size of 33525760 I
can use WriteFile() (after a CreateFile() with the FILE_FLAG_NO_BUFFERING
flag) just fine. If, however I use a file size of 33526272 or larger, then I get:
Insufficient system resources exist to complete the requested service.
I am running on a Dell Precision Workstation 670 w/ dual Xeon processors,
2GB RAM and using WinXP x64 SP1.
I have tried increasing the pagefile.sys size since someone suggested that,
but it did not help.
Here's the sample code:
--- CUT HERE - start of cf-test.cpp ---
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <Windows.h>
// Forward Declaration
void PrintError (void);
int main(int argc, char *argv[])
{
HANDLE hHandle = NULL;
char caFilename[512];
bool bRet = false;
unsigned char *ucpData = NULL;
// This size will work
// long lChunkSizeBytes = 33525760;
// This size will NOT work
long lChunkSizeBytes = 33526272;
unsigned long ulNumBytesWritten = 0;
printf("argc = %d\n", argc);
if (argc > 1)
sscanf(argv[1], "%ld", &lChunkSizeBytes);
strcpy(caFilename, "test.dat");
// Allocate Memory
printf("Allocating memory (%ld bytes)...\n", lChunkSizeBytes);
ucpData = (unsigned char *) VirtualAlloc(NULL,
lChunkSizeBytes,
MEM_COMMIT,
PAGE_READWRITE);
printf("done.\n");
// Open File
printf("Opening file '%s'...\n", caFilename);
hHandle = CreateFile(caFilename,
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_FLAG_NO_BUFFERING,
NULL);
if (hHandle == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Error Opening File\n");
PrintError();
//MemoryFree(&hMem, &ucpData);
exit(-1);
}
printf("done, handle = 0x%x.\n", hHandle);
// Write File
printf("Writing file...\n");
if (!WriteFile(hHandle, ucpData, lChunkSizeBytes, &ulNumBytesWritten,
NULL))
{
fprintf(stderr, "Error Writing File\n");
PrintError();
//MemoryFree(&hMem, &ucpData);
exit(-1);
}
printf("done.\n");
// Close File
printf("Closing file...\n");
if (!CloseHandle(hHandle))
{
fprintf(stderr, "Error Closing File\n");
PrintError();
//MemoryFree(&hMem, &ucpData);
exit(-1);
}
printf("done.\n");
// Free Memory
printf("Releasing Memory...\n");
if (!VirtualFree(ucpData, 0, MEM_RELEASE))
{
fprintf(stderr, "Error Freeing Memory\n");
PrintError();
//MemoryFree(&hMem, &ucpData);
exit(-1);
}
printf("done.\n");
}
void PrintError (void)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Display the error number & string
fprintf(stderr, " GetLastError(0x%x) = %s\n", GetLastError(),
(LPCTSTR) lpMsgBuf);
// Free the buffer.
LocalFree(lpMsgBuf);
return;
}
--- CUT HERE - end of cf-test.cpp ---
Compilation for x64 is done with the Platform SDK for Windows Server 2003 SP1
and selecting the Build Environment for XP x64 Retail. Then "cl cf-test.cpp
bufferoverflowU.lib". Thanks very much for any assistance.
-Michael Russell
***@mathtech.com
kindly direct me to the proper group.
I am having a problem with WriteFile() returning an error. I have whittled
this problem down to a short test program in C which fails under WinXP x64,
but works fine on WinXP x86. I see the problem whether I compile for a win32
or a win64 target. The quick summary is that if I use a size of 33525760 I
can use WriteFile() (after a CreateFile() with the FILE_FLAG_NO_BUFFERING
flag) just fine. If, however I use a file size of 33526272 or larger, then I get:
Insufficient system resources exist to complete the requested service.
I am running on a Dell Precision Workstation 670 w/ dual Xeon processors,
2GB RAM and using WinXP x64 SP1.
I have tried increasing the pagefile.sys size since someone suggested that,
but it did not help.
Here's the sample code:
--- CUT HERE - start of cf-test.cpp ---
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <Windows.h>
// Forward Declaration
void PrintError (void);
int main(int argc, char *argv[])
{
HANDLE hHandle = NULL;
char caFilename[512];
bool bRet = false;
unsigned char *ucpData = NULL;
// This size will work
// long lChunkSizeBytes = 33525760;
// This size will NOT work
long lChunkSizeBytes = 33526272;
unsigned long ulNumBytesWritten = 0;
printf("argc = %d\n", argc);
if (argc > 1)
sscanf(argv[1], "%ld", &lChunkSizeBytes);
strcpy(caFilename, "test.dat");
// Allocate Memory
printf("Allocating memory (%ld bytes)...\n", lChunkSizeBytes);
ucpData = (unsigned char *) VirtualAlloc(NULL,
lChunkSizeBytes,
MEM_COMMIT,
PAGE_READWRITE);
printf("done.\n");
// Open File
printf("Opening file '%s'...\n", caFilename);
hHandle = CreateFile(caFilename,
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_FLAG_NO_BUFFERING,
NULL);
if (hHandle == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Error Opening File\n");
PrintError();
//MemoryFree(&hMem, &ucpData);
exit(-1);
}
printf("done, handle = 0x%x.\n", hHandle);
// Write File
printf("Writing file...\n");
if (!WriteFile(hHandle, ucpData, lChunkSizeBytes, &ulNumBytesWritten,
NULL))
{
fprintf(stderr, "Error Writing File\n");
PrintError();
//MemoryFree(&hMem, &ucpData);
exit(-1);
}
printf("done.\n");
// Close File
printf("Closing file...\n");
if (!CloseHandle(hHandle))
{
fprintf(stderr, "Error Closing File\n");
PrintError();
//MemoryFree(&hMem, &ucpData);
exit(-1);
}
printf("done.\n");
// Free Memory
printf("Releasing Memory...\n");
if (!VirtualFree(ucpData, 0, MEM_RELEASE))
{
fprintf(stderr, "Error Freeing Memory\n");
PrintError();
//MemoryFree(&hMem, &ucpData);
exit(-1);
}
printf("done.\n");
}
void PrintError (void)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Display the error number & string
fprintf(stderr, " GetLastError(0x%x) = %s\n", GetLastError(),
(LPCTSTR) lpMsgBuf);
// Free the buffer.
LocalFree(lpMsgBuf);
return;
}
--- CUT HERE - end of cf-test.cpp ---
Compilation for x64 is done with the Platform SDK for Windows Server 2003 SP1
and selecting the Build Environment for XP x64 Retail. Then "cl cf-test.cpp
bufferoverflowU.lib". Thanks very much for any assistance.
-Michael Russell
***@mathtech.com