[OT?] CreateFileMapping errors 87 and 1006

T

testerman

Greetings!

I suppose you could help me with these errors I get or at least direct
me to a more appropriate newsgroup.

So, the problem is as follows:

hFile = CreateFile((LPCWSTR)"somefile.ext", GENERIC_READ|GENERIC_WRITE,
NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

// As far as I see, works as supposed

hMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE, NULL, NULL,
(LPCWSTR)"somename");

// Always NULL and GetLastError() returns 87 or 1006

I also noticed this: upon changing OPEN_EXISTING to OPEN_ALWAYS in
CreateFile(), a unnamed file is still created, even if "somefile.ext"
exists in the project folder! The first call (with OPEN_EXISTING) causes
error 87 and the second (with OPEN_ALWAYS) causes error 1006 to be
generated. Any suggestions? I thank you in advance.
 
C

Carl Daniel [VC++ MVP]

testerman said:
Greetings!

I suppose you could help me with these errors I get or at least direct
me to a more appropriate newsgroup.

So, the problem is as follows:

hFile = CreateFile((LPCWSTR)"somefile.ext",
GENERIC_READ|GENERIC_WRITE, NULL, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);

Why are you casting an ANSI string to a pointer to wide-char? This does NOT
result in a Unicode string! Try L"somefile.txt" or _T("somefile.txt")
instead of the cast.

-cd
 
T

testerman

Carl said:
Why are you casting an ANSI string to a pointer to wide-char? This does NOT
result in a Unicode string! Try L"somefile.txt" or _T("somefile.txt")
instead of the cast.

Yes, that was the problem. Thank you.
 
T

testerman

testerman said:
Yes, that was the problem. Thank you.

BTW now that I have the memory-mapped file available, how would you - in
terms of speed - process it for say getting all the strings delimited by
sequences of spaces? For example:

word1{spaces-or-tabs}word2{spaces-or-tabs}word3 ... wordx\n

TIA!
 
S

Sheng Jiang[MVP]

use strtok

--
Sheng Jiang
Microsoft MVP in VC++
testerman said:
BTW now that I have the memory-mapped file available, how would you - in
terms of speed - process it for say getting all the strings delimited by
sequences of spaces? For example:

word1{spaces-or-tabs}word2{spaces-or-tabs}word3 ... wordx\n

TIA!
 
T

testerman

Sheng said:
use strtok

We may be off-topic, but I'm curious now... So lets say I have done the
following:

(1) Created a file handle:

hFile = CreateFile(L"somefile.ext", GENERIC_READ, NULL, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

(1) Used it to create a memory-mapped file:

hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, NULL, NULL,
L"somefile.ext");

(3) Created a view and assigned it to a pointer:

char * str = (char *)MapViewOfFile(hMapping, FILE_MAP_READ, NULL, NULL,
NULL);

And steps (1) to (3) work as they should. However, when I try to apply
strtok() in the following manner, for example:

char * pch;

pch = strtok(str," ");
while (pch != NULL)
{
cout << pch << endl;
pch = strtok(NULL," ");
}

it breaks down on first strtok() call and the debugger says pch is a
<Bad Ptr> and "CXX0030: Error: expression cannot be evaluated". Your
opinion?
 
C

Carl Daniel [VC++ MVP]

testerman said:
We may be off-topic, but I'm curious now... So lets say I have done
the following:

(1) Created a file handle:

hFile = CreateFile(L"somefile.ext", GENERIC_READ, NULL, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

(1) Used it to create a memory-mapped file:

hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, NULL, NULL,
L"somefile.ext");

(3) Created a view and assigned it to a pointer:

char * str = (char *)MapViewOfFile(hMapping, FILE_MAP_READ, NULL,
NULL, NULL);

And steps (1) to (3) work as they should. However, when I try to apply
strtok() in the following manner, for example:

char * pch;

pch = strtok(str," ");
while (pch != NULL)
{
cout << pch << endl;
pch = strtok(NULL," ");
}

it breaks down on first strtok() call and the debugger says pch is a
<Bad Ptr> and "CXX0030: Error: expression cannot be evaluated". Your
opinion?

Are you SURE the data is null terminated? Sounds like strtok ran off the
end of the mapped window without encountering a null.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top