Author Topic: HFILE  (Read 946 times)

Biterider

  • Member
  • ***
  • Posts: 254
  • ObjAsm32 - ObjAsm64
    • ObjAsm64
HFILE
« on: November 20, 2017, 04:34:17 AM »
Hi
Moving some part of my old code to x64 I stumbled over the HFILE definition in WinBase.inc


Code: [Select]
_HFILE_DEFINED EQU <>
HFILE  typedef DWORD
endif


Shouldn't it be a HANDLE = 64 bit?


Regards, Biterider

Vortex

  • Member
  • *****
  • Posts: 1873
Re: HFILE
« Reply #1 on: November 20, 2017, 04:52:07 AM »
Hi Biterider,

Reading windef.h supplied with Pelles C :

Code: [Select]
typedef int HFILE;
sizeof(int)=4

Testing this Pelles C code ( 64-bit project ) :

Code: [Select]
#include <stdio.h>
#include <windows.h>

int main()
{
printf("sizeof(HFILE)=%llu",sizeof(HFILE));
return 0;
}

Output :
Code: [Select]
sizeof(HFILE)=4

jj2007

  • Member
  • *****
  • Posts: 8893
  • Assembler is fun ;-)
    • MasmBasic
Re: HFILE
« Reply #2 on: November 20, 2017, 07:55:47 AM »
Right. I stumbled over this some time ago, it is indeed very odd:
HFILE
Quote
A handle to a file opened by OpenFile, not CreateFile.
This type is declared in WinDef.h as follows:
typedef int HFILE;

Note the "OpenFile, not CreateFile". It seems a legacy thing...
HFILE WINAPI OpenFile(
HANDLE WINAPI CreateFile(

The VS header files obscure it, as usual, maybe these are the original definitions:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include\WTypes.h
typedef INT HFILE;

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include\WinNT.h
typedef PVOID HANDLE;

But there is also this definition in WinDef.h:
#ifndef _MAC
typedef int HFILE;
typedef HICON HCURSOR;      /* HICONs & HCURSORs are polymorphic */
#else
typedef short HFILE;
DECLARE_HANDLE(HCURSOR);    /* HICONs & HCURSORs are not polymorphic */
#endif

So if you are on a Mac, it's a short :biggrin:

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5944
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: HFILE
« Reply #3 on: November 20, 2017, 08:02:47 AM »
File handles in Win64 are 64 bit. Either,

hFile dq ?  ; .DATA section
or
LOCAL hFile :QWORD
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

jj2007

  • Member
  • *****
  • Posts: 8893
  • Assembler is fun ;-)
    • MasmBasic
Re: HFILE
« Reply #4 on: November 20, 2017, 08:25:59 AM »
File handles in Win64 are 64 bit. Either,

hFile dq ?  ; .DATA section
or
LOCAL hFile :QWORD


With the exception of the handle returned by OpenFile. Test it yourself...

include \Masm32\MasmBasic\Res\JBasic.inc
.data?
ofs OFSTRUCT <>
Init   ; OPT_64 1              ; put 0 for 32 bit, 1 for 64 bit assembly
  PrintLine Chr$("This code was assembled with ", @AsmUsed$(1), " in ", jbit$, "-bit format")
  ; int 3
  usedeb=1
  jinvoke OpenFile, Chr$("\Masm32\qEditor.exe"), addr ofs, OF_READ
  ; towards the end of OpenFile:
  ; 00000000779E0953   | 8B C7              | mov eax, edi
  mov rsi, offset ofs.szPathName
  deb 4, "Handle", rax, $Err$(), $rsi
  jinvoke CloseHandle, rax
  Inkey "hit any key"
EndOfCode


Output:
Code: [Select]
This code was assembled with ml64 in 64-bit format
Handle
rax     60
$Err$()
Operazione completata.

$rsi    C:\Masm32\qEditor.exe
hit any key