Tech Support > Microsoft Windows > Development Resources > Service opening a shared file with CreateFile
Service opening a shared file with CreateFile
Posted by Agata Staniak on October 29th, 2005


I have a strange problem, which I don't understand

I have computer A (Windows 2000) and computer B (Windows 98).
B is connected to a measuring machine and has a program that gets the
results from the machine and writes them to file E:\file.txt (on computer B)
I wrote a simple service that is working on computer A. All it has to do is
simply open the file on computer B, read it and write its contents to a
database. The service also checks the timestamp of the file to determine if
the file has changed and try to read it only if the file has really been
modified.
To make the file.txt available to computer A, the whole disk E on computer B
is a read-only share. So computer A sees the file as "\\CompB\E\file.txt".

Now, in the service I use:

HANDLE h;
while (service is running)

{

// try to open the file

h = CreateFile("\\\\CompB\\E\\file.txt", GENERIC_READ, FILE_SHARE_READ |
FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

if (h == INVALID_HANDLE_VALUE)
{
// log GetLastError
Sleep(SLEEP);
continue;
}

// check if the file has changed and do some reading if so

CloseHandle(h);

}

The service is running in the context of local system account.

And it works... sometimes. It works for a few iterations of the loop, reads
the file perfectly, and then in one iteration, when the file changes (some
data is added to it), it gets INVALID_HANDLE (GetLastError reports 5 -
Access is denied). And once it gets this error, it never successes in
opening this file again. And I have no idea, why the error occurs

Anyone has any ideas? Thanks in advance!


Posted by Kellie Fitton on October 30th, 2005


Hi,

IF the entire diskDrive E:\ has been set as a read-only share,
how the program on computer B is able to create the textFile
E:\file.txt on computer B ?

Kellie.

Posted by Agata Staniak on October 30th, 2005



"Kellie Fitton" <KELLIEFITTON@YAHOO.COM> wrote in message
news:1130646851.055954.57670@g44g2000cwa.googlegro ups.com...
It is read-only through the network only. For computer B it is simply a
local file, and as there are no NTFS-like permissions in Win98, the program
can write to the disk whatever it wants to. Read-only permission is only for
the share \\CompB\E\ (seen on computer A), not for E:\ (seen on computer B).

And what I can add: the problem I described above occurs only when A is
Win2000 and B is Win98. I made a test on my home computers, where A was
WinXP and B was WinXP too - and everything worked perfectly (as long as NTFS
permissions were OK). So I got really confused because my service seems to
be OK. I can't simulate the company's environment at home, but I'm going to
install Win98 at home now, and make a test: A is going to be WinXP, B is
going to be Win98. Maybe that will help me understand, what's going wrong in
company and why the service works at home on WinXP-WinXP and stops working
in company, on Win2000-Win98... I always thought opening a file for reading
is the simpliest thing on Earth to be coded. Now I know I was wrong...