C++/VB - CreateFile Fails - Second Execution On Same File

Asked By Le Chaud Lapin
17-Nov-09 04:16 AM
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-


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;
}
GetLastError
(1)
CFTest
(1)
SzFilePath
(1)
CreateFile
(1)
DwError
(1)
Lapin
(1)
Secondcall
(1)
Thesecond
(1)
  Uwe Sieber replied to Le Chaud Lapin
17-Nov-09 03:07 AM
Because it is opened for writing and the second
call do not want to share write. With
work.

Uwe
  David Lowndes replied to Le Chaud Lapin
17-Nov-09 03:47 AM
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
  Uwe Sieber replied to David Lowndes
17-Nov-09 06:20 AM
Here is mine :-)
http://www.uwe-sieber.de/files/CreateFileTest.zip


Uwe
Create New Account
help
StretchBlt fails wih FALSE return but GetLastError returns 0 C++ / VB In my code, in the WM_PAINT handler of a dialog, I device context. The call fails with a return of FALSE, so then I immediately call GetLastError in order to determine why it is failing. GetLastError now returns 0. How can I find out why StretchBlt is failing ? If I try right-26, m_rectPage.bottom-26, m_hdcSrc, 0, 0, 6400, 3200, SRCCOPY); if (sret = = FALSE) { lec = GetLastError(); } if (lec) { TextOut(hdc, 20, 20, _T("Error"), 5); } DeleteObject((HGDIOBJ) hBrush); DeleteObject((HGDIOBJ) hBrushShad a memory DC. The StretchBlt fails but all the other device context actions succeed. The GetLastError returns 0. The issue is: Why would StretchBlt ever return FALSE and GetLastError then return 0. It becomes hard figuring out what is wrong in that case. According the return value is zero. Windows NT / 2000 / XP: To get extended error information, call GetLastError. " I discovered: 1) If the source memory DC is created from a printer DC, the StretchBlt to the target window client DC fails, with GetLastError returning0. 2) If the source memory DC is created from a screen DC, the StretchBlt
schService; TCHAR szPath[MAX_PATH]; if( !GetModuleFileName( NULL, szPath, MAX_PATH ) ) { printf("Cannot install service (%d) \ n", GetLastError()); return; } / / Get a handle to the SCM database. schSCManager = OpenSCManager( NULL, / / local computer NULL, / / ServicesActive database SC_MANAGER_ALL_ACCESS); / / full access rights if (NULL = = schSCManager) { printf("OpenSCManager failed (%d) \ n", GetLastError()); return; } / / Create the service. schService = CreateService( schSCManager, / / SCM database SERVICENAME, / / name of service SERVICENAME, / / service dependencies NULL, / / LocalSystem account NULL); / / no password if (schService = = NULL) { printf("CreateService failed (%d) \ n", GetLastError()); CloseServiceHandle(schSCManager); return; } else printf("Service installed successfully \ n"); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); } / / / / Purpose: / / Starts NULL, / / ServicesActive database SC_MANAGER_ALL_ACCESS); / / full access rights if (NULL = = schSCManager) { printf("OpenSCManager failed (%d) \ n", GetLastError()); return; } / / Get a handle to the service schService = OpenService( schSCManager, / / SCM database szSvcName, / / name of service SERVICE_ALL_ACCESS); / / full access if (schService = = NULL) { printf("OpenService failed (%d) \ n", GetLastError()); CloseServiceHandle(schSCManager); return; } / / Attempt to start the service. if (!StartService( schService, / / handle to service 0 number of arguments NULL) ) / / no arguments { printf("StartService failed (%d) \ n", GetLastError()); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return; } else printf("Service start pending. . . \ n"); / / Check the status until database SC_MANAGER_ALL_ACCESS); / / full access rights if (NULL = = schSCManager) { / / _tprintf(_T("OpenSCManager failed, error: %u \ n"), ::GetLastError() ); return; } } if( schService = = 0 ) { schService = ::OpenService( schSCManager, SERVICENAME , SC_MANAGER_ALL_ACCESS ); if( schService = = 0 ) { / / _tprintf( SERVICENAME _T schService ! = 0 ) { BOOL ok = ::DeleteService( schService ); if( ok ) { _tprintf( _T("Service deleted \ n") ); } else { DWORD dwError = ::GetLastError(); _tprintf( _T("DeleteService failed, error: %u \ n"), dwError ); } schService = 0; } if( schSCManager ! = 0 ) } Thanks
a handle to the console output buffer and tries to duplicate the handle returned by CreateFile for usage within the same process. The first call to DuplicateHandle(), which uses the real GetCurrentProcess (), GetCurrentProcess (), GetCurrentProcess (), &hMainProc, 0, FALSE, DUPLICATE_SAME_ACCESS)) { fprintf (stderr, "Dup process fails with %lu \ n", GetLastError ()); return 1; } hMainProc = OpenProcess (PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId ()); if (!hMainProc) { fprintf (stderr, "Open process fails with %lu \ n", GetLastError ()); return 1; } out = CreateFile ("CONOUT$", GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, 0); if (out = = INVALID_HANDLE_VALUE) { fprintf (stderr, "Create stdout fails with %lu \ n", GetLastError ()); return 1; } if (!DuplicateHandle (hMainProc, out, hMainProc, &err, 0, TRUE, DUPLICATE_SAME_ACCESS)) fprintf (stderr, "Dup stdout with real proc handle fails with %lu \ n", GetLastError ()); else fprintf (stderr, "Dup stdout with real proc handle works \ n"); if (!DuplicateHandle (GetCurrentProcess (), out 0, TRUE, DUPLICATE_SAME_ACCESS)) fprintf (stderr, "Dup stdout with pseudo proc handle fails with %lu \ n", GetLastError ()); else fprintf (stderr, "Dup stdout with pseudo proc handle works \ n"); return 0; } = = = SNAP = = = Corinna Cygwin Project Co-Leader Red Hat Win32 Kernel Discussions XP (1) GetCurrentProcessId (1) GetCurrentProcess (1) GetLastError (1) DuplicateHandle (1) OpenProcess (1) CreateFile (1) HMainProc (1) | Why on earth does duplicating a
Hi, I have opened a USB device using its PnP name retrieved using the the CreateFile() call, Then tried to call the SetFilePointer() to set beyond the actual end of file Discussions SetupDiGetDeviceInterfaceDetail (1) SetupDiEnumDeviceInterfaces (1) SetFilePointerEx (1) SetEndOfFile (1) PNewFilePointer (1) SetFilePointer (1) RetSetFilePtr (1) GetLastError (1) Hi Lloyd, code please, . . . Regards Ken - - - -- -- -- -- -- -- -- -- -- -- -- Beste Gr?sse / Best regards / Votre bien devoue LPGUID) &GUID_DEVINTERFACE_DISK, Index, ∫erfaceData); . . . . . . . status = 3D SetupDiGetDeviceInterfaceDetail (hDevInfo, ∫erfaceData, NULL, 0, &reqSize, NULL); . . . . . . . hDevice = 3D CreateFile(interfaceDetailData-> DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); / / All the above calls succeeds LPGUID) &GUID_DEVINTERFACE_DISK, Index, ∫erfaceData); . . . . . . . status = 3D SetupDiGetDeviceInterfaceDetail (hDevInfo, ∫erfaceData, NULL, 0, &reqSize, NULL); . . . . . . . hDevice = 3D CreateFile(interfaceDetailData-> DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); / / All the above calls succeeds is only 128MB!! Hi, did you try FILE_END on SFP? What do you get on GetLastError() and SFP return Code, . . .for FILE_BEGIN and FILE_END? Can you drop ReadFile / WriteFile Operations on the Device Object after open it with CreateFile, just to make sure your Handle is valid for these operations, . . . Regards Kerem - - - -- -- -- -- -- -- -- -- -- -- -- Beste Gr Open-Source Projects: http: / / entwicklung.junetz.de - -- -- -- -- -- -- -- -- -- -- -- I tried ReadFile() on the "handle" got from CreateFile, and it returns valid data. . . When I try from FILE_END, it returns "ERROR_INVALID_FUNCTION" You may this, but have you tried code like this? if(!SetFilePointerEx( h, 0, &pNewFilePointer, FILE_END )) { dwErr = GetLastError(); / / handle error } As others have mentioned, the behavior will be device dependent and file pointers
function. But it fails (and returns zero ) so no window is created. When I call GetLastError just after CreatEx call return 0 means no ERROR. Why?? Doesn't failure of CreateEx function set the error code?? Which functions set the error code (which is retrived by GetLastError) ?? VC MFC Discussions APIFor (1) CreateWindowEx (1) PostNcDestroy (1) GetLastError (1) CreateEx (1) CreatEx (1) CWnd (1) Newcomer (1) The documentation of CWnd::CreateEx() does Or post your code here. - - David Wilkinson Visual C++ MVP It makes sense to call GetLastError after calling raw Win32 API For example, if CreateWindowEx call (inside CWnd::CreateEx body) fails before CWnd::CreateEx returns FALSE. I think these functions may alter the error state, making GetLastError calls after CWnd::CreateEx meaningless. Giovanni As pointed out, CWnd::CreateEx does not set the function. But it fails (and returns zero ) so no window is created. When I call GetLastError just after CreatEx call r