Converting Windows-Filehandle to Purebasic-Filehandle

Windows specific forum
sepp
User
User
Posts: 10
Joined: Sat Jul 28, 2012 11:31 pm

Converting Windows-Filehandle to Purebasic-Filehandle

Post by sepp »

Hello all,

does anyone know if it is possible to convert a Windows-filehandle to a Purebasic-filehandle?
I want to read a logfile, but as ReadFile can not read shared-read I want to use CreateFile_.

I found a structure which seems to be a PureBasic-filehandle (http://www.purebasic.fr/german/viewtopic.php?t=1256), but when populating it and then calling a PureBasic-function with that filehandler it always says "[ERROR] #File object not initialized." Any ideas?

Code: Select all

Structure API_FileHandle
  FHandle.l
  Buffer.l
  BufferLen.l
  ReadPos.l
  DataInBuffer.l
  readed.l
EndStructure

Define myhFile.API_FileHandle

logfile.s = "C:\log.log"

hFile = CreateFile_(@logfile, #GENERIC_READ, #FILE_SHARE_READ Or #FILE_SHARE_WRITE, 0, #OPEN_EXISTING, #FILE_ATTRIBUTE_NORMAL, 0)
If hFile=#INVALID_HANDLE_VALUE
  Debug "CreateFile failed!"
  End
EndIf

myhFile\FHandle = hFile
myhFile\BufferLen = 4*1024
myhFile\Buffer=AllocateMemory(myhFile\BufferLen)
myhFile\readed=1

hFile = @myhFile
CloseFile(hFile)
Thanks,
Sepp
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Converting Windows-Filehandle to Purebasic-Filehandle

Post by Demivec »

sepp wrote:does anyone know if it is possible to convert a Windows-filehandle to a Purebasic-filehandle?
I don't think this is possible. You can convert from a PureBasic-filehandle to an OS-filehandle but not the other way around. CreateFile_() only assigns a filehandle and won't use a specified filehandle. That means you can't tell it to use a PureBasic-filehandle that has been converted to an OS-filehandle.

The simple solution is to use API for all operations with your file: CreateFile_(), ReadFile_(), WriteFile_(), and CloseFile_().
sepp
User
User
Posts: 10
Joined: Sat Jul 28, 2012 11:31 pm

Re: Converting Windows-Filehandle to Purebasic-Filehandle

Post by sepp »

Demivec wrote:
sepp wrote:does anyone know if it is possible to convert a Windows-filehandle to a Purebasic-filehandle?
I don't think this is possible. You can convert from a PureBasic-filehandle to an OS-filehandle but not the other way around. CreateFile_() only assigns a filehandle and won't use a specified filehandle. That means you can't tell it to use a PureBasic-filehandle that has been converted to an OS-filehandle.

The simple solution is to use API for all operations with your file: CreateFile_(), ReadFile_(), WriteFile_(), and CloseFile_().
That's a pitty, makes it more complicated :(

Thanks.
Post Reply