|
Can't say a thing without seeing your code.
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
So, got no idea why this is not working but it simply not working.
SIZE_T BASE_BUFFER_SIZE = 512;
BYTE * bInitialBuffer = NULL;
bInitialBuffer = (BYTE *)VirtualAlloc(0,
BASE_BUFFER_SIZE, MEM_RESERVE, PAGE_READWRITE);
if(bInitialBuffer == NULL)
{
TRACE("VirtualAlloc failed!");
return NULL;
}
DWORD dwReadBytes;
while (FALSE != ReadFile(hDevice, bInitialBuffer, BASE_BUFFER_SIZE,
(LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0)
{
TRACE1("got buffer! %d",dwReadBytes );
}
if(dwReadBytes <= 0) TRACE("failed!");
GetLAstError() returns Invalid access to memory location.
I am dealing with virtual alloc for a first time :P
What am i doing wrong?
Thanks
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
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, like MEM_RESERVE | MEM_COMMIT ). At least the doc[^] says so.
You also failed to tell what's "not working". I'm assuming that your ReadFile call failed, and GetLastError returns invalid access to memory location. Reading that, I'm assuming that since you're just reserving memory with your VirtualAlloc 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)
{
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 calling ReadFile ?
"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
|
|
|
|
|
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)
{
}
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);
}
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
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 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.
|
|
|
|
|
For now i have solved the issue with simple text file and one time allocation
Now, it is time to read mp4 file into buffer and make a copy of it :X
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
|
|
|
|
|
It was very suspicious in fact! Yes, i have made a disaster mistake and it is corrected by now
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
void Print(const char* s_Format, ...)
{
va_list args;
va_start(args, s_Format);
char s_Out[1024];
sprintf(s_Out,s_Format, args);
Output += s_Out;
FILE *f = fopen("connection.log", "at");
fprintf(f, Output.c_str());
fclose(f);
return;
}
the s_Format is fine but the arguments are not working the string Output; is stored in the header
when i send Print("Sending File: %s\n",fpath); all i get is Sending File: ú· when it should be Sending File: test.ini
even when i do say Print("hello","hello"); i get hello then garbage
|
|
|
|
|
There basically are two ways to work with variable argument lists:
1.
you enumerate the list yourself, using va_arg, as is shown here[^].
2.
or you pass the list to a function that is designed to accept such a list.
sprintf() does not accept a va_list (which describes a list of arguments), it accepts an actual list of real arguments.
what you want here is supported by vsprintf(), see here[^].
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
|
|
|
|
|
yip working perfect i cant belive i missed that one out thanks 
|
|
|
|
|
You're welcome.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
modified on Friday, June 10, 2011 8:45 PM
|
|
|
|
|
Just downloaded latest version of CP Vanity, great work, congratulations.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
you're welcome 2.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
|
|
|
|
|
Try this -
va_list args;
va_start(args, s_Format);
char s_Out[1024];
sprintf(s_Out, s_Format, va_arg(args, char*));
|
|
|
|
|
this way also works 
|
|
|
|
|
Hi!
I've to open a web page inside a Dialog(not as a popup) using ShellExecute. I've to use ShellExecute why because Navigate() function works only if IE is present in a System.Is this possible with ShellExecute? Or is there any other alternative?
|
|
|
|
|
|
barneyman wrote: you can't use ShellExecute like that
Ok. I realized that after posting the topic. Now what I want is to open my web page in a separate browser window. I don't want to open inside an already opened window. How to do this with ShellExecute?
|
|
|
|
|