1

From my understanding if the share mode is set to 0, I will not be able to open the file again.

By default I've set it to 3, but even setting it to 2 ( FILE_SHARE_WRITE ) outputs the same error.

So that makes me think that the parameters are correct.

The data:

FileName:
 db 'testWriteFile.txt',0

The code:

    //create file
    push 0 // hTemplateFile null
    push 80 //dwFlagsAndAttributes 'FILE_ATTRIBUTE_NORMAL'
    push 4  // dwCreationDisposition OPEN_ALWAYS
    push 0 // lpSecurityAttributes null
    push 3 // dwShareMode FILE_SHARE_READ | FILE_SHARE_WRITE -> allow other programs to read and write
    push C // dwDesiredAccess GENERIC_WRITE
    push FileName //lpFileName
    call CreateFileA

    call GetLastError

What could be the cause?

Edit: CreateFIleA reference https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
Germ
  • 117
  • 6

1 Answers1

2
push 0 // hTemplateFile null
push 80 //dwFlagsAndAttributes 'FILE_ATTRIBUTE_NORMAL'
push 4  // dwCreationDisposition OPEN_ALWAYS
push 0 // lpSecurityAttributes null
push 3 // dwShareMode FILE_SHARE_READ | FILE_SHARE_WRITE -> allow other programs to read and write
push C // dwDesiredAccess GENERIC_WRITE
push FileName //lpFileName
call CreateFileA

The values that you push are meant to be hexadecimal numbers. You must append a suitable affix to obtain these.

  • The FILE_ATTRIBUTE_NORMAL constant is 00000080h.
  • The OPEN_ALWAYS constant is 00000004h.
  • The FILE_SHARE_READ and FILE_SHARE_WRITE constants are 00000001h and 00000002h. So to set both use 00000003h.
  • The GENERIC_READ and GENERIC_WRITE constants are 80000000h and 40000000h. So to set both use 0C0000000h.
    push 0          // hTemplateFile null
    push 80h        // dwFlagsAndAttributes FILE_ATTRIBUTE_NORMAL
    push 4          // dwCreationDisposition OPEN_ALWAYS
    push 0          // lpSecurityAttributes null
    push 3          // dwShareMode FILE_SHARE_READ | FILE_SHARE_WRITE
    push 0C0000000h // dwDesiredAccess GENERIC_READ | GENERIC_WRITE
    push FileName   // lpFileName
    call CreateFileA
Sep Roland
  • 33,889
  • 7
  • 43
  • 76