Search code examples
windowsassembly64-bit

What is the GENERIC_WRITE constant hex value in windows?


I'm trying to open a file for writing using the CreatFileA system call in assembly in x64, however in order to do so I need to specify the desired access. I don't know what the constant is for GENERIC_WRITE, the GENERIC_READ constant is 80000000h.

; create the file
lea rcx, fileName
mov rdx, 40000000h
xor r8, r8
xor r9, r9
mov QWORD PTR [rsp+48h-28h], 2
mov QWORD PTR [rsp+48h-20h], 80h
mov QWORD PTR [rsp+48h-18h], 0
call CreateFileA
mov FD2, rax

; write to the new file
lea rcx, FD2
lea rdx, buffer
mov r8, len
lea r9, written
mov QWORD PTR [rsp+48h-28h], 0
call WriteFile
mov writeResult, rax

Solution

  • Turning the comments into an answer so this can get closed out.

    As Michael points out, the bits that make up the Access Mask are defined here.

    Using that we see that GENERIC_READ is 0x80000000 and GENERIC_WRITE is 0x40000000.

    Generally speaking, you should probably look at Windows' headers to get the definitive and most up-to-date values for all Windows constants. This one is in Winnt.h.

    Addressing the follow-on question, your assembler code to load the handle to be passed to WriteFile is incorrect. You are saving the value returned from CreateFile using

    mov FD2, rax
    

    But then you load it back using

    lea rcx, FD2
    

    lea is going to return a pointer to the handle, not the handle itself.

    So, for once, Windows was really being helpful when it returned the The handle is invalid error message. (By implication) it told which parameter was the source of the problem, and (roughly) what the problem was.