I am getting Error_Access_Denied
I want to change the file date to the current date. i.e. Touch it
; touch_file.asm
;
; Set Marker file to current date
;
include \masm32\include\masm32rt.inc
.CONST
.data
file db "c:\Windows_XP_Partition"
FSize dd 0
.data?
hFile HANDLE ?
FPointer dd ?
Numb dd ?
.code
start:
invoke CreateFile, addr file, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
.IF eax==INVALID_HANDLE_VALUE
invoke ExitProcess,0
.endif
mov hFile, eax
Invoke GetFileSize, hFile, NULL
.if eax == 0FFFFFFFFh
; code for failure
;
.endif
mov FSize, eax ; Save FileSize
Invoke GlobalAlloc, GMEM_FIXED, FSize
mov FPointer, eax
.IF eax == NULL ; If no pointer, error message
.ENDIF
Invoke ReadFile, hFile,ADDR FPointer,FSize, ADDR Numb, NULL
invoke CloseHandle, hFile ; Release handle fails if return value is 0
invoke GlobalFree,FPointer ; Release memory block
; error message if no valid handle
invoke ExitProcess,0
end start
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx
QuoteIf you call CreateFile on a file that is pending deletion as a result of a previous call to DeleteFile, the function fails. The operating system delays file deletion until all handles to the file are closed. GetLastError returns ERROR_ACCESS_DENIED.
Which call gives you the error? Also, you might want to zero-terminate the file name.
Quote from: sinsi on December 05, 2014, 06:38:35 PM
you might want to zero-terminate the file name.
Affects only the error message, if it displays the file name :biggrin:
Quote from: jj2007 on December 05, 2014, 06:53:18 PM
Quote from: sinsi on December 05, 2014, 06:38:35 PM
you might want to zero-terminate the file name.
Affects only the error message, if it displays the file name :biggrin:
Bad habit to fall into though...
Quote from: sinsi on December 05, 2014, 07:20:54 PMBad habit to fall into though...
C'mon, we are assembler coders, optimisation is a question of honour ;)
You're opening the file for writing and then try to read from it (without read access.)
You don't need to read from the file to change its timestamp, just open (CreateFile with GENERIC_WRITE), SetFileTime, and CloseHandle.
Error_Already_Exists
include \masm32\include\masm32rt.inc
.CONST
.data
file db "c:\Windows_XP_Partition"
FSize dd 0
.data?
hFile HANDLE ?
FPointer dd ?
Numb dd ?
fCRTime dd ?
fACTime dd ?
fWRTime dd ?
.code
start:
invoke CreateFile, addr file, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
.IF eax==INVALID_HANDLE_VALUE
invoke ExitProcess,0
.endif
mov hFile,eax
invoke SetFileTime, hFile, addr fCRTime, addr fACTime, addr fWRTime
invoke CloseHandle, hFile ; Release handle fails if return value is 0
invoke ExitProcess,0
QuoteIf the specified file exists, the function succeeds and the last-error code is set to ERROR_ALREADY_EXISTS (183).
hey guys...
little bit help form my side if Access denied error is returned from CreateFile function
1. use zero terminated string,
2. just read the specified description carefully in msdn about CreateFile at http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx). they have stated that
for opening handle to a Directory by using CreateFile:
An application cannot create a directory by using CreateFile, therefore only the OPEN_EXISTING value is valid for dwCreationDisposition for this use case. To create a directory, the application must call CreateDirectory or CreateDirectoryEx.
To open a directory using CreateFile, specify the FILE_FLAG_BACKUP_SEMANTICS flag as part of dwFlagsAndAttributes. Appropriate security checks still apply when this flag is used without SE_BACKUP_NAME and SE_RESTORE_NAME privileges.
When using CreateFile to open a directory during defragmentation of a FAT or FAT32 file system volume, do not specify the MAXIMUM_ALLOWED access right. Access to the directory is denied if this is done. Specify the GENERIC_READ access right instead.
so here is code as follows,
include \masm32\include\masm32rt.inc
.data
file db 'e:\pickme',0
.data?
hfile dd ?
.code
start:
cls
print chr$('pick me',13,10)
invoke CreateFile,offset file,FILE_GENERIC_WRITE OR FILE_GENERIC_READ,\
FILE_SHARE_READ,NULL,OPEN_EXISTING,\
FILE_FLAG_BACKUP_SEMANTICS,NULL
mov hfile,eax
print LastError$(),13,10
print str$(hfile),13,10
;your code to do.
invoke CloseHandle,hfile
exit
end start
worked for me. :t
Thanks shaikkareem, I will zero terminate it and see what happens.
I zero terminated the file name.
I get Error_Already_Exists.
??
.CONST
.data
file db "c:\Windows_XP_Partition",0
FSize dd 0
.data?
hFile HANDLE ?
FPointer dd ?
Numb dd ?
fCRTime dd ?
fACTime dd ?
fWRTime dd ?
.code
start:
invoke CreateFile, addr file, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
.IF eax==INVALID_HANDLE_VALUE
invoke ExitProcess,0
.endif
mov hFile,eax
invoke SetFileTime, hFile, addr fCRTime, addr fACTime, addr fWRTime
invoke CloseHandle, hFile ; Release handle fails if return value is 0
invoke ExitProcess,0
end start
If you get eax!=INVALID_HANDLE_VALUE, then this "error" isn't an error.
I get Error_Already_Exists.
Quote from: Magnum on December 15, 2014, 07:42:00 AM
I get Error_Already_Exists.
Sure, the same happens to me every time I try to open a file that exists. Which doesn't mean that's an error. It is an error
only if eax==INVALID_HANDLE_VALUE, otherwise it is not an error. So what do you get for eax???
Eax shows zero as it would if no error had occured.
It apparently worked once today.
I ran the program at 6:34 and 6:35 and refreshed explorer.
As the pictures shows, the file date and time were not changed.
Guess it is one of those mysteries. :-)
Maybe I will see if CreateFileEx does the same thing.
There's no mystery.
This took all of 3 minutes to write.
.586
.model flat, stdcall
option casemap:none
include windows.inc
include kernel32.inc
includelib kernel32.lib
.const
fname db "test.txt",0
.data?
hFile HANDLE ?
systime SYSTEMTIME <>
ftime FILETIME <>
.code
start:
invoke CreateFile, ADDR fname,GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
.IF (eax != INVALID_HANDLE_VALUE)
mov hFile,eax
;;;invoke GetLocalTime, ADDR systime;;;
invoke GetSystemTime, ADDR systime
invoke SystemTimeToFileTime, ADDR systime,ADDR ftime
invoke SetFileTime, hFile,NULL,NULL,ADDR ftime ;update modified time to current time
invoke CloseHandle, hFile
.ENDIF
invoke ExitProcess, NULL
end start
Edit: used GetSystemTime instead; silly mistake caused by rushing out code.
Windows has 'touch' to update the timestamp built-in, though it's a bizarre incantation (http://support.microsoft.com/kb/69581)
Quote from: adeyblue on December 16, 2014, 06:27:27 AM
Windows has 'touch' to update the timestamp built-in, though it's a bizarre incantation (http://support.microsoft.com/kb/69581)
Bizarre is an understatement:
QuoteThe following MS-DOS command updates the date and time stamps of a file named "EXAMPLE" without altering the contents of the file. This is similar to the TOUCH utility found in XENIX and in some third-party MS-DOS toolkits.
COPY /B EXAMPLE +,,
Oh Redmond :redface:
There is a Touch (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1055) command in MasmBasic, and it can take xmm0 as a second parameter to set an arbitrary FILETIME. Here is an exotic use for the instruction PAVGW - it sets the timestamp of Pic2.png to the average of Pic1 and Pic3:
include \masm32\MasmBasic\MasmBasic.inc ; download (http://masm32.com/board/index.php?topic=94.0)
Init
Touch "Pic2.png" ; set to current date and time
.if Exist("Pic2.png")
MsgBox 0, Cat$("Pic2 was modified on "+GfDate$()+", "+GfTime$()+\
CrLf$+"Verify the date now in Explorer"), "Touché:", MB_OK
.if Exist("Pic1.png")
sub esp, 4*16
mov edi, esp
lea esi, [esp+16]
movups [edi+SYSTEMTIME], GfGetInfo(m)
lea edx, [edi+SYSTEMTIME]
invoke FileTimeToSystemTime, edx, edi
.if Exist("Pic3.png")
movups [esi+SYSTEMTIME], GfGetInfo(m)
lea edx, [esi+SYSTEMTIME]
invoke FileTimeToSystemTime, edx, esi
movups xmm0, [edi]
movups xmm1, [esi]
pavgw xmm0, xmm1 ; Use AVERAGE to touch Pic2
lea edx, [edi+SYSTEMTIME]
movups [edx], xmm0
invoke SystemTimeToFileTime, edx, edi
movups xmm0, [edi]
Touch "Pic2.png", xmm0
void Exist("Pic2.png")
PrintLine "Modified ", GfDate$(), ", ", GfTime$()
.endif
add esp, 4*16
.endif
.endif
Inkey "OK?"
Exit
end startWinAPI fans who know their SYSTEMTIME know why pavgw works in this case :bgrin:
Quote
There's no mystery.
This took all of 3 minutes to write.
What happened, are you getting slow in your old age ? :biggrin:
Quote from: adeyblue on December 16, 2014, 06:27:27 AM
Windows has 'touch' to update the timestamp built-in, though it's a bizarre incantation (http://support.microsoft.com/kb/69581)
Thanks adeyblue.
I wanted to learn to do it using assembly.
I have been out of it for a while.
Quote from: Tedd on December 16, 2014, 12:21:49 AM
There's no mystery.
This took all of 3 minutes to write.
It failed.
I went with this.
cd\
COPY /B Windows_XP_Partition +,,
SetFileTime
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724933%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/ms724933%28v=vs.85%29.aspx)
Tedd used SetFileTime is his code.
I found this and am working on it.
' First, open the file C:\MyApp\test.txt for both read-level and
' write-level access, since we need to do both.
hFile = CreateFile("C:\MyApp\test.txt", GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ, ByVal CLng(0), OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
If hFile = -1 Then
Debug.Print "Could not open the file successfully -- aborting."
End ' terminate the program
End If
' Next, get the creation, last-access, and last-modification times.
retval = GetFileTime(hFile, ctime, atime, mtime)
' Get the system time (already in UTC) as a FILETIME structure.
GetSystemTimeAsFileTime mtime
' Set the retrieved creation and access times and the new modification
' time as the file's times.
retval = SetFileTime(hFile, ctime, atime, mtime)
' Close the file to free up resources.
retval = CloseHandle(hFile)
notice that SetFileTime wants 3 pointers to FILETIME structures
i suggest:
creation time: NULL - leave it as is
access time: pointer to current FILETIME structure
modify time: same pointer as access time
I don't understand how to make a pointer to current FILETIME structure.
if it's a local, use ADDR
if it's a global, use OFFSET
pointer = address
people will tell you that ADDR works for either
and that may be so, but i prefer to differentiate so that i know at a glance which i am using :t
Quote from: dedndave on December 17, 2014, 06:51:51 AM
people will tell you that ADDR works for either
and that may be so, but i prefer to differentiate so that i know at a glance which i am using :t
Yes ADDR works in both cases. But your practice is okay and well thought out.
Gunther
well - ADDR works for either in an INVOKE argument
but,
mov eax,addr dwGlobal
may not work - i don't recall, because i don't use that form - lol
certainly,
mov eax,addr dwLocal
is bad form - it should be
lea eax,dwLocal
It gives Error_Invalid_Parameter.
I can't tell which api is throwing that error.
.data
file db "test.txt",0
.data?
hFile HANDLE ?
fCRTime dd ? ; creation time
fACTime dd ? ; access time
fWRTime dd ? ; write time
systime SYSTEMTIME <>
.code
; notice that SetFileTime wants 3 pointers to FILETIME structures
;
; i suggest:
; creation time: NULL - leave it as is
; access time: pointer to current FILETIME structure
; modify time: same pointer as access time
start:
invoke CreateFile, addr file, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
.IF eax==INVALID_HANDLE_VALUE
invoke ExitProcess,0
.endif
mov hFile,eax
invoke GetFileTime,hFile, addr fCRTime, addr fACTime, addr fWRTime
invoke GetSystemTimeAsFileTime,ADDR systime
invoke SetFileTime, hFile, addr fCRTime, offset fACTime, offset fWRTime
invoke CloseHandle, hFile ; Release handle fails if return value is 0
invoke ExitProcess,0
end start
fCRTime FILETIME <> ; creation time
fACTime FILETIME <> ; access time
fWRTime FILETIME <> ; write time
FILETIME structures are essentially QWORD's
also...
GetSystemTimeAsFileTime wants a pointer to a FILETIME structure, not a SYSTEMTIME structure
your .if/.endif structure is a little clumsy
.IF eax!=INVALID_HANDLE_VALUE
;do stuff
.endif
invoke ExitProcess,0
The example I posted does work - I tested it, obviously.
If it does not work for you, the problem lies elsewhere and thus you should focus your efforts on finding why, not on reproducing the same code with minor variations as if that's going to magically make it work.
as a final thought....
you don't need to get the current file times
get the current time as FILETIME (use a single structure, ftime)
set the access and mod times using that single structure
invoke SetFileTime,hFile,NULL,offset ftime,offset ftime
Quote from: Tedd on December 17, 2014, 07:23:25 AM
The example I posted does work - I tested it, obviously.
If it does not work for you, the problem lies elsewhere and thus you should focus your efforts on finding why, not on reproducing the same code with minor variations as if that's going to magically make it work.
Your code did NOT work on my system.
My posts are designed to find out what is wrong.
Maybe spend more time helping than criticizing ??
you may want to watch the file sharing flags, Andy
if some other process may be accessing the file, use the appropriate flag set in both programs
Quote from: Magnum on December 17, 2014, 09:18:17 AM
Your code did NOT work on my system.
My posts are designed to find out what is wrong.
Maybe spend more time helping than criticizing ??
Let me just get this straight.
At 4:20pm, you copied and assembled the example code, created a file named "test.txt", and then ran the program. The modified time of the file is now 10:20am.
This is all just guessing, but I'm going to say that the file time has definitely changed.
Admittedly, the time is not the current time in your locale, but a difference of exactly 6 hours (your UTC offset) is a change nonetheless. That fault is mine -- I used
GetLocalTime instead of
GetSystemTime -- but other than that, the code clearly does change the modification time on your system.
So, go ahead and replace
GetLocalTime with
GetSystemTime, re-assemble, and re-test.
Maybe spend more time checking details than crying foul of supposed victimization.
There is no other program accessing the file.
Thanks a lot Tedd.
That change got it to working. :-)