Wierd issue with x64

Windows specific forum
PureGuy
Enthusiast
Enthusiast
Posts: 102
Joined: Mon Aug 30, 2010 11:51 am

Wierd issue with x64

Post by PureGuy »

I have no idea what going on with this, but I think it's a bug.
Following code works and gives messagerequester with "Return 0".

But if you remove the out commented "Print(Hex(nt))" at the end it fails.
It Only happens with x64 and the structures a correct, any ideas?

Code: Select all

EnableExplicit

CompilerIf #PB_Compiler_Unicode = 0 Or #PB_Compiler_Processor = #PB_Processor_x86
  MessageRequester("", "Please Set Unicode and x64 Compiler option!")
  End
CompilerEndIf

Structure _IO_STATUS_BLOCK Align #PB_Structure_AlignC
  StructureUnion
    Status.l
    Pointer.i
  EndStructureUnion
  Information.i
EndStructure

Structure _UNICODE_STRING Align #PB_Structure_AlignC
  usLength.w 
  usMaximumLength.w   
  usBuffer.i
EndStructure

Structure _OBJECT_ATTRIBUTES Align #PB_Structure_AlignC
  Length.l
  RootDirectory.i
  ObjectName.i
  Attributes.l
  SecurityDescriptor.i
  SecurityQualityOfService.i  
EndStructure

#FILE_CREATE = 2
#FILE_OPEN_IF = 3
#FILE_OVERWRITE_IF = 5
#FILE_NON_DIRECTORY_FILE = $40
#OBJ_CASE_INSENSITIVE    = $40
#CSIDL_DESKTOPDIRECTORY = $10


Procedure.s GetSpecialFolderLocation(Value.l)
  Protected Folder_ID,SpecialFolderLocation.s
  If SHGetSpecialFolderLocation_(0, Value, @Folder_ID) = 0
    SpecialFolderLocation = Space(#MAX_PATH)
    SHGetPathFromIDList_(Folder_ID, @SpecialFolderLocation)
    If SpecialFolderLocation
      If Right(SpecialFolderLocation, 1) <> "\"
        SpecialFolderLocation + "\"
      EndIf
    EndIf
    CoTaskMemFree_(Folder_ID)   
  EndIf
  ProcedureReturn SpecialFolderLocation.s
EndProcedure

Define Allo.q, hfile, io._IO_STATUS_BLOCK, ufilePath._UNICODE_STRING, sfilePath.s, obj._OBJECT_ATTRIBUTES, nt.l, sDesktop.s

sDesktop = GetSpecialFolderLocation(#CSIDL_DESKTOPDIRECTORY)
sfilePath = "\??\" + sDesktop + "testfile.txt"
RtlInitUnicodeString_(ufilePath, @sfilePath)

obj\Attributes = #OBJ_CASE_INSENSITIVE
obj\Length = SizeOf(_OBJECT_ATTRIBUTES)
obj\ObjectName = ufilePath

nt = NtCreateFile_(@hfile, #STANDARD_RIGHTS_ALL, obj, io, @Allo, #FILE_ATTRIBUTE_NORMAL, #FILE_SHARE_READ, #FILE_OVERWRITE_IF, #FILE_NON_DIRECTORY_FILE, 0, 0)
If nt = 0
  NtClose_(hfile)
  NtDeleteFile_(obj)
EndIf

MessageRequester("", "Return: " + Hex(nt, #PB_Long))

If 1 = 3
  OpenConsole()
  ;Print(Hex(nt))
  Input()
EndIf

User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Wierd issue with x64

Post by bbanelli »

PureGuy wrote:I have no idea what going on with this, but I think it's a bug.
Following code works and gives messagerequester with "Return 0".

But if you remove the out commented "Print(Hex(nt))" at the end it fails.
It Only happens with x64 and the structures a correct, any ideas?
It works on 5.4 b8@W7 x64 without a problem and returns STATUS_SUCCESS for NtCreateFile.
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
PureGuy
Enthusiast
Enthusiast
Posts: 102
Joined: Mon Aug 30, 2010 11:51 am

Re: Wierd issue with x64

Post by PureGuy »

With the Print, it returns "Return: 80000002" here, also Windows7 x64, 5.40b8.
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Wierd issue with x64

Post by Fred »

I can't reproduce it here, this is strange.
PureGuy
Enthusiast
Enthusiast
Posts: 102
Joined: Mon Aug 30, 2010 11:51 am

Re: Wierd issue with x64

Post by PureGuy »

Thanks for looking at it.

I also tested in Windows 10 Virtualbox.
It always happens here even in the final exe.
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Wierd issue with x64

Post by Fred »

Does it happen with 5.31 as well ?
PureGuy
Enthusiast
Enthusiast
Posts: 102
Joined: Mon Aug 30, 2010 11:51 am

Re: Wierd issue with x64

Post by PureGuy »

Yes, i get the same problem in all versions 5.40, 5.31 and also in the old 5.24 LTS

BTW: if I replace the MessageRequester() with

MessageBox_(0, Hex(nt, #PB_Long), "Return", 0)

it works again.
fryquez
Enthusiast
Enthusiast
Posts: 362
Joined: Mon Dec 21, 2015 8:12 pm

Re: Wierd issue with x64

Post by fryquez »

I had the same problem with native API's in PB.

A small code change and you will get STATUS_DATATYPE_MISALIGNMENT error.
Your structures are correctly aligned, but the whole structure might be wrong aligned in memory.

AllocateMemory() does not seem to have that problem.

So change your structures

obj._OBJECT_ATTRIBUTES --> *obj._OBJECT_ATTRIBUTES = AllocateMemory(SizeOf(_OBJECT_ATTRIBUTES))
Post Reply