The MASM Forum

General => The Campus => Topic started by: Magnum on December 05, 2014, 04:10:44 PM

Title: Error_Acess_Denied
Post by: Magnum on December 05, 2014, 04:10:44 PM
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
Title: Re: Error_Acess_Denied
Post by: gelatine1 on December 05, 2014, 06:17:36 PM
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.
Title: Re: Error_Acess_Denied
Post by: sinsi on December 05, 2014, 06:38:35 PM
Which call gives you the error? Also, you might want to zero-terminate the file name.
Title: Re: Error_Acess_Denied
Post by: 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:
Title: Re: Error_Acess_Denied
Post by: sinsi on December 05, 2014, 07:20:54 PM
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...
Title: Re: Error_Acess_Denied
Post by: jj2007 on December 05, 2014, 08:02:38 PM
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 ;)
Title: Re: Error_Acess_Denied
Post by: Tedd on December 05, 2014, 11:38:16 PM
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.
Title: Re: Error_Acess_Denied
Post by: Magnum on December 06, 2014, 08:10:04 AM
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
Title: Re: Error_Acess_Denied
Post by: sinsi on December 06, 2014, 04:56:15 PM
QuoteIf the specified file exists, the function succeeds and the last-error code is set to ERROR_ALREADY_EXISTS (183).
Title: Re: Error_Acess_Denied
Post by: shaikkareem on December 06, 2014, 05:36:55 PM
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
Title: Re: Error_Acess_Denied
Post by: Magnum on December 06, 2014, 06:30:57 PM
Thanks shaikkareem, I will zero terminate it and see what happens.

Title: Re: Error_Acess_Denied
Post by: Magnum on December 15, 2014, 06:35:55 AM
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
Title: Re: Error_Acess_Denied
Post by: jj2007 on December 15, 2014, 06:41:09 AM
If you get eax!=INVALID_HANDLE_VALUE, then this "error" isn't an error.
Title: Re: Error_Acess_Denied
Post by: Magnum on December 15, 2014, 07:42:00 AM
I get Error_Already_Exists.
Title: Re: Error_Acess_Denied
Post by: jj2007 on December 15, 2014, 08:08:26 AM
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???
Title: Re: Error_Acess_Denied
Post by: Magnum on December 15, 2014, 11:46:28 AM
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.
Title: Re: Error_Acess_Denied
Post by: Tedd on December 16, 2014, 12:21:49 AM
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.
Title: Re: Error_Acess_Denied
Post by: 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)
Title: Re: Error_Acess_Denied
Post by: jj2007 on December 16, 2014, 10:43:19 AM
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 start


WinAPI fans who know their SYSTEMTIME know why pavgw works in this case :bgrin:
Title: Re: Error_Acess_Denied
Post by: hutch-- on December 16, 2014, 10:52:00 AM
Quote
There's no mystery.
This took all of 3 minutes to write.
What happened, are you getting slow in your old age ?  :biggrin:
Title: Re: Error_Acess_Denied
Post by: Magnum on December 16, 2014, 01:22:57 PM
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.
Title: Re: Error_Acess_Denied
Post by: Magnum on December 16, 2014, 02:25:16 PM
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 +,,

Title: Re: Error_Acess_Denied
Post by: dedndave on December 16, 2014, 03:21:46 PM
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)
Title: Re: Error_Acess_Denied
Post by: Magnum on December 16, 2014, 03:48:28 PM
Tedd used SetFileTime is his code.

Title: Re: Error_Acess_Denied
Post by: Magnum on December 17, 2014, 05:23:53 AM
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)
Title: Re: Error_Acess_Denied
Post by: dedndave on December 17, 2014, 05:57:01 AM
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
Title: Re: Error_Acess_Denied
Post by: Magnum on December 17, 2014, 06:22:01 AM
I don't understand how to make a pointer to current FILETIME structure.
Title: Re: Error_Acess_Denied
Post by: dedndave on December 17, 2014, 06:47:40 AM
if it's a local, use ADDR
if it's a global, use OFFSET

pointer = address
Title: Re: Error_Acess_Denied
Post by: 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
Title: Re: Error_Acess_Denied
Post by: Gunther on December 17, 2014, 06:54:58 AM
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
Title: Re: Error_Acess_Denied
Post by: dedndave on December 17, 2014, 06:58:56 AM
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
Title: Re: Error_Acess_Denied
Post by: Magnum on December 17, 2014, 07:07:23 AM
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
Title: Re: Error_Acess_Denied
Post by: dedndave on December 17, 2014, 07:09:55 AM
fCRTime FILETIME <> ; creation time
fACTime FILETIME <> ; access time
fWRTime FILETIME <> ; write time


FILETIME structures are essentially QWORD's
Title: Re: Error_Acess_Denied
Post by: dedndave on December 17, 2014, 07:13:21 AM
also...
GetSystemTimeAsFileTime wants a pointer to a FILETIME structure, not a SYSTEMTIME structure
Title: Re: Error_Acess_Denied
Post by: dedndave on December 17, 2014, 07:15:18 AM
your .if/.endif structure is a little clumsy
.IF eax!=INVALID_HANDLE_VALUE

;do stuff

.endif

invoke ExitProcess,0
Title: Re: Error_Acess_Denied
Post by: 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.
Title: Re: Error_Acess_Denied
Post by: dedndave on December 17, 2014, 07:27:24 AM
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
Title: Re: Error_Acess_Denied
Post by: Magnum on December 17, 2014, 09:18:17 AM
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 ??



Title: Re: Error_Acess_Denied
Post by: dedndave on December 17, 2014, 10:04:55 AM
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
Title: Re: Error_Acess_Denied
Post by: Tedd on December 17, 2014, 10:54:22 AM
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.
Title: Re: Error_Acess_Denied
Post by: Magnum on December 17, 2014, 10:54:31 AM
There is no other program accessing the file.

Title: Re: Error_Acess_Denied
Post by: Magnum on December 17, 2014, 11:34:09 AM
Thanks a lot Tedd.

That change got it to working. :-)