Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Opening file with FOPEN
Message
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
01134285
Message ID:
01134558
Views:
13
CreateFile() API can be used for opening an existing file. Though it requires some coding:
#DEFINE GENERIC_WRITE 0x40000000
#DEFINE GENERIC_READ 0x80000000
#DEFINE FILE_SHARE_WRITE 2
#DEFINE FILE_SHARE_READ 1
#DEFINE OPEN_EXISTING 3
#DEFINE FILE_ATTRIBUTE_NORMAL 0x00000080
#DEFINE INVALID_HANDLE_VALUE -1

DECLARE INTEGER CloseHandle IN kernel32 INTEGER hObject
DECLARE INTEGER GetLastError IN kernel32

DECLARE INTEGER CreateFile IN kernel32;
	STRING lpFileName, INTEGER dwDesAccess, INTEGER dwShareMode,;
	STRING @lpSecurAttr, INTEGER dwCreatDisp, INTEGER dwFlagsAndAttrs,;
	INTEGER hTemplateFile

LOCAL hFile, cFilename, cSecurityAttributes
cFilename = "c:\temp\test.png"  && put valid filename

*!*	typedef struct _SECURITY_ATTRIBUTES {
*!*	   DWORD nLength;
*!*	   LPVOID lpSecurityDescriptor;
*!*	   BOOL bInheritHandle;
*!*	} SECURITY_ATTRIBUTES,  *PSECURITY_ATTRIBUTES

cSecurityAttributes = PADR(CHR(12), 4, CHR(0)) +;
	REPLICATE(CHR(0), 4) +;
	PADR(CHR(1), 4, CHR(0))

hFile = CreateFile(m.cFilename,;
	BITOR(GENERIC_WRITE, GENERIC_READ),;
	BITOR(FILE_SHARE_WRITE, FILE_SHARE_READ),;
	@cSecurityAttributes,;
	OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0) 

IF hFile <> INVALID_HANDLE_VALUE
	? "File handle:", hFile
	= CloseHandle(hFile)  && close file when no longer needed
ELSE
	* 2=ERROR_FILE_NOT_FOUND
	* 3=ERROR_PATH_NOT_FOUND
	* 5=ERROR_ACCESS_DENIED
	? "Error:", GetLastError()
ENDIF
cSecurityAttributes takes care about (a) inheritance flag.

BITOR(FILE_SHARE_WRITE, FILE_SHARE_READ) is equivalent to OF_SHARE_DENY_NONE when using OpenFile() API -- so it solves (b).

If I'm not mistaken, the handle returned by CreateFile() API is different from the one FOPEN() returns. That means hFile can not be used with VFP low-level file functions.
Previous
Reply
Map
View

Click here to load this message in the networking platform