VirualAlloc and ReadFile
-
csrss wrote:
VirtualAlloc(0, BASE_BUFFER_SIZE, MEM_RESERVE, PAGE_READWRITE);
Just a hunch. The doc says that
MEM_RESERVE
flag Reserves a range of the process's virtual address space without allocating any actual physical storage in memory or in the paging file on disk. If you're actually going to do something with the memory, MEM_COMMIT may be a good option (you could combine flags, likeMEM_RESERVE | MEM_COMMIT
). At least the doc[^] says so. You also failed to tell what's "not working". I'm assuming that yourReadFile
call failed, andGetLastError
returns invalid access to memory location. Reading that, I'm assuming that since you're just reserving memory with yourVirtualAlloc
call, and not committing it before using, you're getting an invalid access error."Real men drive manual transmission" - Rajesh.
Yep, you are right. But here with this stuff i got more and more problems. For example, i cannot return bytes from function, (this code is a function, which should read a file into BYTE array and return this array among with nr of bytes read).
BYTE *ReadBytes(HANDLE hDevice, LPDWORD lpdwBytesRead)
{
SIZE_T BASE_BUFFER_SIZE = 512;BYTE \* bInitialBuffer = NULL; bInitialBuffer = (BYTE \*)VirtualAlloc(0, BASE\_BUFFER\_SIZE, MEM\_COMMIT | MEM\_RESERVE, PAGE\_READWRITE); if(bInitialBuffer == NULL) { // handle this shit TRACE("VirtualAlloc failed!"); return NULL; } DWORD dwReadBytes; while (FALSE != ReadFile(hDevice, bInitialBuffer, BASE\_BUFFER\_SIZE, (LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0) { TRACE1("got some buffer! %d", dwReadBytes); lpdwBytesRead += dwReadBytes; } return bInitialBuffer;
}
and if i call it:
DWORD read = 0;
BYTE *DataBuffer = ReadBytes(hFile, &read);read is always zero, and i got no bytes returned from function... And i cannot use just defined size (skip memory allocation). Help ;(
011011010110000101100011011010000110100101101110 0110010101110011
-
Yep, you are right. But here with this stuff i got more and more problems. For example, i cannot return bytes from function, (this code is a function, which should read a file into BYTE array and return this array among with nr of bytes read).
BYTE *ReadBytes(HANDLE hDevice, LPDWORD lpdwBytesRead)
{
SIZE_T BASE_BUFFER_SIZE = 512;BYTE \* bInitialBuffer = NULL; bInitialBuffer = (BYTE \*)VirtualAlloc(0, BASE\_BUFFER\_SIZE, MEM\_COMMIT | MEM\_RESERVE, PAGE\_READWRITE); if(bInitialBuffer == NULL) { // handle this shit TRACE("VirtualAlloc failed!"); return NULL; } DWORD dwReadBytes; while (FALSE != ReadFile(hDevice, bInitialBuffer, BASE\_BUFFER\_SIZE, (LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0) { TRACE1("got some buffer! %d", dwReadBytes); lpdwBytesRead += dwReadBytes; } return bInitialBuffer;
}
and if i call it:
DWORD read = 0;
BYTE *DataBuffer = ReadBytes(hFile, &read);read is always zero, and i got no bytes returned from function... And i cannot use just defined size (skip memory allocation). Help ;(
011011010110000101100011011010000110100101101110 0110010101110011
One step at a time. Have you progressed from your previous error, or not? With the
MEM_RESERVE
flag, do you still get an invalid access error while callingReadFile
?"Real men drive manual transmission" - Rajesh.
-
Yep, you are right. But here with this stuff i got more and more problems. For example, i cannot return bytes from function, (this code is a function, which should read a file into BYTE array and return this array among with nr of bytes read).
BYTE *ReadBytes(HANDLE hDevice, LPDWORD lpdwBytesRead)
{
SIZE_T BASE_BUFFER_SIZE = 512;BYTE \* bInitialBuffer = NULL; bInitialBuffer = (BYTE \*)VirtualAlloc(0, BASE\_BUFFER\_SIZE, MEM\_COMMIT | MEM\_RESERVE, PAGE\_READWRITE); if(bInitialBuffer == NULL) { // handle this shit TRACE("VirtualAlloc failed!"); return NULL; } DWORD dwReadBytes; while (FALSE != ReadFile(hDevice, bInitialBuffer, BASE\_BUFFER\_SIZE, (LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0) { TRACE1("got some buffer! %d", dwReadBytes); lpdwBytesRead += dwReadBytes; } return bInitialBuffer;
}
and if i call it:
DWORD read = 0;
BYTE *DataBuffer = ReadBytes(hFile, &read);read is always zero, and i got no bytes returned from function... And i cannot use just defined size (skip memory allocation). Help ;(
011011010110000101100011011010000110100101101110 0110010101110011
csrss wrote:
i cannot return bytes from function, (this code is a function, which should read a file into BYTE array and return this array among with nr of bytes read).
This has nothing to do with
VirtualAlloc
."Real men drive manual transmission" - Rajesh.
-
One step at a time. Have you progressed from your previous error, or not? With the
MEM_RESERVE
flag, do you still get an invalid access error while callingReadFile
?"Real men drive manual transmission" - Rajesh.
At this moment:
while (FALSE != ReadFile(hDevice, bInitialBuffer, BASE_BUFFER_SIZE,
(LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0)
{
TRACE1("got some buffer! %d", dwReadBytes);dwReadBytes contains right value, which is 106 bytes, that is exactly the size of my file (just a text file). Here :
lpdwBytesRead += dwReadBytes;
It tries to append them, but lpdwBytesRead contain some trash, even if it has been initialized outside the function to zero:
DWORD read = 0;
BYTE *DataBuffer = ReadBytes(hFile, &read);When function returns, read is zero and there is no buffer returned :(
011011010110000101100011011010000110100101101110 0110010101110011
-
csrss wrote:
i cannot return bytes from function, (this code is a function, which should read a file into BYTE array and return this array among with nr of bytes read).
This has nothing to do with
VirtualAlloc
."Real men drive manual transmission" - Rajesh.
I know it is not perfect :P (the code) I have never worked with unsigned type allocation before. (the only proper way for returning unsigned type from function i know is to declare buffer size and make this, BYTE, static) If i pass a pointer to BYTE to this function from calling function, it still returns empty BYTE buffer. I am out of ideas. Somebody, help? :P
011011010110000101100011011010000110100101101110 0110010101110011
-
I know it is not perfect :P (the code) I have never worked with unsigned type allocation before. (the only proper way for returning unsigned type from function i know is to declare buffer size and make this, BYTE, static) If i pass a pointer to BYTE to this function from calling function, it still returns empty BYTE buffer. I am out of ideas. Somebody, help? :P
011011010110000101100011011010000110100101101110 0110010101110011
-
Yep, you are right. But here with this stuff i got more and more problems. For example, i cannot return bytes from function, (this code is a function, which should read a file into BYTE array and return this array among with nr of bytes read).
BYTE *ReadBytes(HANDLE hDevice, LPDWORD lpdwBytesRead)
{
SIZE_T BASE_BUFFER_SIZE = 512;BYTE \* bInitialBuffer = NULL; bInitialBuffer = (BYTE \*)VirtualAlloc(0, BASE\_BUFFER\_SIZE, MEM\_COMMIT | MEM\_RESERVE, PAGE\_READWRITE); if(bInitialBuffer == NULL) { // handle this shit TRACE("VirtualAlloc failed!"); return NULL; } DWORD dwReadBytes; while (FALSE != ReadFile(hDevice, bInitialBuffer, BASE\_BUFFER\_SIZE, (LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0) { TRACE1("got some buffer! %d", dwReadBytes); lpdwBytesRead += dwReadBytes; } return bInitialBuffer;
}
and if i call it:
DWORD read = 0;
BYTE *DataBuffer = ReadBytes(hFile, &read);read is always zero, and i got no bytes returned from function... And i cannot use just defined size (skip memory allocation). Help ;(
011011010110000101100011011010000110100101101110 0110010101110011
csrss wrote:
lpdwBytesRead += dwReadBytes;
This looks suspect. You are incrementing the pointer instead of what the pointer points to. Is that your intent?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
At this moment:
while (FALSE != ReadFile(hDevice, bInitialBuffer, BASE_BUFFER_SIZE,
(LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0)
{
TRACE1("got some buffer! %d", dwReadBytes);dwReadBytes contains right value, which is 106 bytes, that is exactly the size of my file (just a text file). Here :
lpdwBytesRead += dwReadBytes;
It tries to append them, but lpdwBytesRead contain some trash, even if it has been initialized outside the function to zero:
DWORD read = 0;
BYTE *DataBuffer = ReadBytes(hFile, &read);When function returns, read is zero and there is no buffer returned :(
011011010110000101100011011010000110100101101110 0110010101110011
This piece of code works (you had trouble bringing the buffer to the calling function, which is resolved here)
#define BASE_BUFFER_SIZE 512
BOOL ReadBytes(HANDLE hFile, PBYTE bInitialBuffer)
{
DWORD dwReadBytes;
while (FALSE != ReadFile(hFile, bInitialBuffer, BASE_BUFFER_SIZE,
(LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0)
{
/*CString str;
str.Format(L"got buffer! %d",dwReadBytes);
AfxMessageBox(str);*/
}
if(dwReadBytes <= 0)
{
return FALSE;
}
return TRUE;
}void CMTest1Dlg::OnBnClickedOk()
{
HANDLE hFile = NULL;hFile = CreateFile(L"C:\\\\temp\\\\myfile.txt", GENERIC\_READ, 0, 0, OPEN\_EXISTING, 0, 0); if(INVALID\_HANDLE\_VALUE == hFile){ AfxMessageBox(L"CreateFile failed"); return; } BYTE \*buffer = NULL; buffer = (BYTE \*)VirtualAlloc(0, BASE\_BUFFER\_SIZE, MEM\_COMMIT | MEM\_RESERVE, PAGE\_READWRITE); if(NULL == buffer) { AfxMessageBox(L"VirtualAlloc failed!"); return; } BOOL bRead = ReadBytes(hFile, buffer); //use buffer here
}
"Real men drive manual transmission" - Rajesh.
-
I got it!!! YEah!
*lpdwBytesRead += dwReadBytes;
Its just too much on my head, too much...
011011010110000101100011011010000110100101101110 0110010101110011
Everything's good now? :) The piece of code I've given above in my other reply doesn't involve any pointer arithmetic.
"Real men drive manual transmission" - Rajesh.
-
Everything's good now? :) The piece of code I've given above in my other reply doesn't involve any pointer arithmetic.
"Real men drive manual transmission" - Rajesh.
-
csrss wrote:
lpdwBytesRead += dwReadBytes;
This looks suspect. You are incrementing the pointer instead of what the pointer points to. Is that your intent?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather