[AccessD] OT: API calls

Jim Lawrence accessd at shaw.ca
Sun Feb 6 19:01:39 CST 2005


Hi David:

Thanks for your input. We, a C++ programmer and myself, have spent a few
hours combing the web and our conclusion, along with many other posting is
that this is a major bug. If used, in certain ways you can actually lock a
hard drive to the point where only a reboot will unlock it. Even on a XP2003
server the process can not be unlocked through the Computer Manager. MS has
a way of doing it but they do not give out that information... security
concerns???

If you want some fun, create a VB application, in MSVS (6 or .Net), open a
file using the CreateFile API, in debug mode, step through the program and
stop it just after you have successfully opened a file, any file. Try the
process again and it will show the file as locked. Fortunately, the process
does not damage a file in any way and when exiting MSVS or rebooting, all
locks are removed. 

If you would like to try this at home, do the following:

1. Create or copy a file, into you temp directory, like 'test.txt'.

2. Go into VBS and enter the following lines of code:
 
<code sample>
'Declarations

' Add these constants for clarity
Private Const GENERIC_WRITE = &H40000000
Private Const GENERIC_READ = &H80000000
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const OPEN_ALWAYS = 4
Private Const INVALID_HANDLE_VALUE = -1

' Creates a file, if it does not exist or just opens it if it does.
Private Declare Function CreateFile Lib "kernel32" _
        Alias "CreateFileA" (ByVal lpFileName As String, _
                             ByVal dwDesiredAccess As Long, _
                             ByVal dwShareMode As Long, _
                             ByVal lpSecurityAttributes As Long, _
                             ByVal dwCreationDisposition As Long, _
                             ByVal dwFlagsAndAttributes As Long, _
                             ByVal hTemplateFile As Long) As Long


Public Sub OpenTestFile(sFileName as String)
    Dim hFile As Long

    hFile = CreateFile(sFileName, GENERIC_WRITE Or GENERIC_READ, 0, _
                 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
    
    ' Upon any subsequent pass, on a particular file, 
    ' the process should fail.
    If hFile = INVALID_HANDLE_VALUE Then
	msgbox "Your test file is successfully locked."
    End if
End Sub


Private Sub Form_Load()
    	
    ' The file to lock
    OpenTestFile "c:\temp\test.txt"

End Sub
</code sample>

3. Either add a form with a single button that calls the OpenTestFile
subroutine or just run it in debug/interactive mode. (With a form it can be
compiled and ran anywhere. Impress your friends by locking various file on
yours or better yet their computer.) 

If anyone knows a solution there are dozens on the web just waiting.  

Have a good day and thanks David.
Jim

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of
dmcafee at pacbell.net
Sent: Sunday, February 06, 2005 2:15 PM
To: Access Developers discussion and problem solving
Subject: RE: [AccessD] OT: API calls

Upon executing the app that was inadvertently closed, can you check for last
opened app and close if in open state?

Kind of like (Do I dare say it?) word 97 recovery when shutting down
incorrectly and reopening Word, not the doc itself.

If this is in Access, can you open a hidden start up form that prevents the
app from closing until the file is itself closed?

David McAfee

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com]On Behalf Of Jim Lawrence
Sent: Sunday, February 06, 2005 1:45 PM
To: 'Access Developers discussion and problem solving'
Subject: [AccessD] OT: API calls


Hi All:

This is an OT subject but it has to do with opening and closing files. This
application that I wrote opens files using the CreateFile API and does a
fine job after the process is finished the CloseHandle API is used to tidy
up and everything works fine.

..but if the program is inadvertently directly closed, without going through
the appropriate step the file previously opened is not closed correctly. The
file can now not be opened by any apparent method and it requires a computer
boot to unlock it.  (The file in question is not damaged in anyway but the
system keeps the file locked.)

Does anyone know how to open/close such a problem file?

Any suggestions would be greatly appreciated. Code suggestions would of
course be best.

MTIA
Jim

PS Wrote a little pseudo editor that can view a file of any size and it
works great; with one exception.

--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com

-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com




More information about the AccessD mailing list