From: Le Chaud Lapin on 16 Nov 2009 23:57 Hi All, See code below. Why does the second call fail? Do I misunderstand the MSDN documentation? http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx TIA, -Le Chaud Lapin- #include <windows.h> int main (int argc, char *argv[], char *envp[]) { // Please make sure the following file exists before running: TCHAR szFilePath[] = "C:\\FOO.DOCX"; HANDLE h1 = CreateFile ( szFilePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); HANDLE h2 = CreateFile ( szFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); DWORD dwError = GetLastError(); return 0; }
From: Uwe Sieber on 17 Nov 2009 03:07 Because it's opened for writing and the second call don't want to share write. With "FILE_SHARE_READ | FILE_SHARE_WRITE" it will work. Uwe Le Chaud Lapin wrote: > Hi All, > > See code below. Why does the second call fail? > > Do I misunderstand the MSDN documentation? > > http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx > > TIA, > > -Le Chaud Lapin- > > #include <windows.h> > > int main (int argc, char *argv[], char *envp[]) > { > // Please make sure the following file exists before running: > TCHAR szFilePath[] = "C:\\FOO.DOCX"; > > HANDLE h1 = CreateFile ( > szFilePath, > GENERIC_READ | GENERIC_WRITE, > FILE_SHARE_READ | FILE_SHARE_WRITE, > NULL, > OPEN_EXISTING, > FILE_ATTRIBUTE_NORMAL, > NULL); > > HANDLE h2 = CreateFile ( > szFilePath, > GENERIC_READ, > FILE_SHARE_READ, > NULL, > OPEN_EXISTING, > FILE_ATTRIBUTE_NORMAL, > NULL); > > DWORD dwError = GetLastError(); > > return 0; > }
From: David Lowndes on 17 Nov 2009 03:47 >See code below. Why does the second call fail? Because the first instance is opened for reading and writing, the second one needs to allow the same sharing. i.e. it should be: HANDLE h2 = CreateFile ( szFilePath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); You can test these usages of CreateFile using multiple instances of my CFTest utility: http://jddesign.co.uk/products/freeware/freeware_programs.htm#CFTest Dave
From: Uwe Sieber on 17 Nov 2009 06:20 David Lowndes wrote: > > You can test these usages of CreateFile using multiple instances of my > CFTest utility: > http://jddesign.co.uk/products/freeware/freeware_programs.htm#CFTest Here is mine :-) http://www.uwe-sieber.de/files/CreateFileTest.zip Uwe
|
Pages: 1 Prev: Undock computer Next: How to change the drive boot order by code? |