New topic: Getting a file handle using API declares?
<http://forums.realsoftware.com/viewtopic.php?t=32404> Page 1 of 1 [ 1 post ] Previous topic | Next topic Author Message rebrandsoftware Post subject: Getting a file handle using API declares?Posted: Mon Feb 08, 2010 3:46 pm Joined: Wed Mar 25, 2009 4:30 pm Posts: 31 Location: Alexandria, VA, USA Hi Everyone, I'm really stumped here. I'm trying to use the LockFile and UnlockFile API functions in kernel32. In order to do that it looks like I first need to get a handle for the file I want to lock. I must be doing something wrong because it keeps crashing on OpenFile(). Here is the code: Code: Const OF_READ = &H0 Const OF_WRITE = &H1 Const OFS_MAXPATHNAME = 128 Const INVALID_HANDLE_VALUE = -1 Soft Declare Function OpenFile Lib "kernel32" (lpFileName as cstring, lpReOpenBuff as OFSTRUCT, wStye as integer) As Integer 'Open a file and return its handle Soft Declare Function LockFile Lib "kernel32" (ByVal hFile As integer, ByVal dwFileOffsetLow As integer, ByVal dwFileOffsetHigh As integer, ByVal nNumberOfBytesToLockLow As integer, ByVal nNumberOfBytesToLockHigh As integer) As Integer 'Lock the file to prevent access and hopefully prevent copying/deleting Soft Declare Function UnlockFile Lib "kernel32" (ByVal hFile As integer, ByVal dwFileOffsetLow As integer, ByVal dwFileOffsetHigh As integer, ByVal nNumberOfBytesToUnlockLow As integer, ByVal nNumberOfBytesToUnlockHigh As integer) As Integer 'Unlock the file when I'm done Soft Declare Function CloseHandle Lib "kernel32" (ByVal hObject As integer) As Integer 'Close the handle when I'm done Sub CaptureFile(LockedFile as folderitem) dim iHandle as integer 'Stores the file handle dim OfSt as OFSTRUCT 'Must be passed to OpenFile function, see structure below if lockedFile <> nil then iHandle = openFile(LockedFile.AbsolutePath, OfSt, OF_READ) '<== Trying to get handle, crashes here MsgBox cstr(iHandle) 'see what was returned 'This commented code hasn't been tested, I'll get to it once I can get a file handle if iHandle <> INVALID_HANDLE_VALUE then 'iRet = LockFile(iHandle, 0, 0, 1, 0) end if end if End Sub OFSTRUCT is a structure in my module that looks like this: Code: cBytes as Byte fFixedDisk as Byte nErrCode as Integer Reserved1 as Integer Reserved2 as Integer szPathName(128) as Byte 'it won't let me use OFS_MAXPATHNAME in the structure so I just used 128 I'm not sure how to go about debugging this one. Any help would be very much appreciated! Thanks! -Mike P.S. The ultimate goal here is to reproduce this old VB Code, which I would apply to an EXE file that was already running, making it impossible to copy or delete the file until I unlocked it: Code: Function FileLock(sFilePathName As String, Optional iFreeFile As Integer = 0) As Integer Open sFilePathName For Random Shared As iFreeFile Lock #iFreeFile FileLock = iFreeFile End Function Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 1 post ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
