News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Error_Acess_Denied

Started by Magnum, December 05, 2014, 04:10:44 PM

Previous topic - Next topic

Magnum

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
Take care,
                   Andy

Ubuntu-mate-18.04-desktop-amd64

http://www.goodnewsnetwork.org

gelatine1

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.

sinsi

Which call gives you the error? Also, you might want to zero-terminate the file name.

jj2007

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:

sinsi

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...

jj2007

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 ;)

Tedd

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.
Potato2

Magnum

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
Take care,
                   Andy

Ubuntu-mate-18.04-desktop-amd64

http://www.goodnewsnetwork.org

sinsi

QuoteIf the specified file exists, the function succeeds and the last-error code is set to ERROR_ALREADY_EXISTS (183).

shaikkareem

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. 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

Magnum

Thanks shaikkareem, I will zero terminate it and see what happens.

Take care,
                   Andy

Ubuntu-mate-18.04-desktop-amd64

http://www.goodnewsnetwork.org

Magnum

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
Take care,
                   Andy

Ubuntu-mate-18.04-desktop-amd64

http://www.goodnewsnetwork.org

jj2007

If you get eax!=INVALID_HANDLE_VALUE, then this "error" isn't an error.

Magnum

I get Error_Already_Exists.
Take care,
                   Andy

Ubuntu-mate-18.04-desktop-amd64

http://www.goodnewsnetwork.org

jj2007

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???