Author Topic: MASM 64bit - Cannot Get CreateFileW to work. CreateFileA works fine.  (Read 756 times)

nmccamy

  • Regular Member
  • *
  • Posts: 1
CreateFileW returns -1 and GetLastError returns 2 (The system cannot find the file specified.)

Running the latest VS Community 2017.
Using Windows 10 with the latest updates.

Tried these register settings as well:

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]
"LongPathsEnabled"=dword:00000001
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Policies]
"LongPathsEnabled"=dword:00000001

Code:

   ;-------------------------------------------------------------------------------------------
   extern ExitProcess:proc
   extern CreateFileA:proc
   extern CreateFileW:proc
   extern ReadFile:proc
   extern GetLastError:proc
   extern CloseHandle:proc
   ;----------------------------------------------------------------------------------------------
   GENERIC_READ         equ 080000000h
   FILE_ATTRIBUTE_NORMAL   equ 080h
   OPEN_EXISTING         equ 3
   OPEN_ALWAYS            equ 4
   ;-------------------------------------------------------------------------------------------
.data
align 1
   gFileName      db "D:\C# Projects\TestAsm\Simple.txt", 0      ;Simple.txt contains Hello
   ;gFileName      db "\\?\D:\C# Projects\TestAsm\Simple.txt", 0   ;Tried this too.
   gFileHeapPtr   db 10 Dup(0)
   ;-------------------------------------------------------------------------------------------
align 8
   gFileHandle dq 0
   gBytesRead   dq 0
   ;-------------------------------------------------------------------------------------------
.code
   ;-------------------------------------------------------------------------------------------
main proc
   mov rbp, rsp

   mov rcx, offset gFileName
   mov rdx, GENERIC_READ
   xor r8, r8
   xor r9, r9

   push 0
   push FILE_ATTRIBUTE_NORMAL
   push OPEN_EXISTING

   sub rsp, 32
   call CreateFileA   ; CreateFileA works fine.
   ;call CreateFileW   ; Returns -1
   ;call GetLastError   ; Returns 2 > The system cannot find the file specified.
   mov rsp, rbp

   mov gFileHandle, rax

   mov rcx, gFileHandle
   mov rdx, offset gFileHeapPtr
   mov r8, 5
   
   lea rax, gBytesRead
   mov r9, rax
   push 0

   sub rsp, 32
   call ReadFile
   mov rsp, rbp

   mov rcx, gFileHandle
   sub rsp, 32
   call CloseHandle
   mov rsp, rbp

   call ExitProcess
main endp
   ;-------------------------------------------------------------------------------------------
End
   ;-------------------------------------------------------------------------------------------


mabdelouahab

  • Member
  • ***
  • Posts: 332
Re: MASM 64bit - Cannot Get CreateFileW to work. CreateFileA works fine.
« Reply #1 on: February 13, 2017, 07:05:35 AM »
CreateFileW :is the unicode version of  function CreateFile


    mov rcx,  .ustr("D:\C# Projects\TestAsm\Simple.txt")
    ...
    call CreateFileW

[/b]

Code: [Select]
OPTION DOTNAME                         
.ustr macro sstr
LOCAL dstr,?Len,cur_Pos,ch_unq,tmpStr,cur_Line
?Len = @SizeStr(<sstr>)
?Len = ?Len - 2
?LenC = (?Len / 16) +1
cur_Pos = 1
cur_Line = 1
tmpStr CATSTR <>
.data
REPEAT ?LenC
IF cur_Pos EQ (?Len+1)
EXITM
ENDIF
repeat 16
cur_Pos=cur_Pos+1
ch_unq SubStr <sstr>,cur_Pos,1
IF cur_Pos GT ?Len
tmpStr CATSTR tmpStr,<!">,ch_unq,<!">
EXITM
ELSE
tmpStr CATSTR tmpStr,<!">,ch_unq,<!",>
ENDIF
endm
IF cur_Line EQ 1
dstr dw tmpStr 
ELSE
dw tmpStr 
ENDIF
cur_Line = cur_Line + 1
tmpStr CATSTR <>
ENDM
dw  0
.code
EXITM<OFFSET dstr >
endm

mineiro

  • Member
  • ***
  • Posts: 365
Re: MASM 64bit - Cannot Get CreateFileW to work. CreateFileA works fine.
« Reply #2 on: February 13, 2017, 10:15:52 AM »
you can try this too with full pathname

align 4
gFileName      dw "S","i","m","p","l","e",".","t","x","t",0      ;Simple.txt contains Hello
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4807
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: MASM 64bit - Cannot Get CreateFileW to work. CreateFileA works fine.
« Reply #3 on: February 13, 2017, 11:12:39 AM »
Converting the ANSI string to unicode is also a viable option.

Her is a macro that does it from the 64 bit version of MASM I am working on.

    ucode$ MACRO ansi$
    LOCAL buffer,pbuffer
      .data?
        buffer db 512 dup (?)
      .data
        pbuffer dq buffer
      .code
      invoke MultiByteToWideChar,CP_ACP,MB_PRECOMPOSED,ansi$,-1,pbuffer,256
      EXITM <pbuffer>
    Endm
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin: