Problem with CreateFile_()

Just starting out? Need help? Post your questions and find answers here.
zgneng
User
User
Posts: 29
Joined: Sun Jun 28, 2015 9:04 am

Problem with CreateFile_()

Post by zgneng »

Code: Select all


q.l= CreateFile_("c:\888.txt", #GENERIC_READ,  0, 0, #OPEN_ALWAYS, #FILE_ATTRIBUTE_NORMAL, 0)

Global a.s{10000000}

Debug q

ReadFile_(q, @a, 9, #Null, #Null)


Do not use the PUREBASIC function(CreateFile AND ReadFile)

Using windows APi(CreateFile_,ReadFile)_

You will find BUG





OK

Code: Select all





Procedure.s GETFILETXT(PATH.l);文件名地址
  sbuff.l;返回长度
  Filehwnd.l= CreateFile_(PATH, #GENERIC_READ,  0, 0, #OPEN_ALWAYS, #FILE_ATTRIBUTE_NORMAL, 0)
  If Filehwnd
  fsize.l=GetFileSize_(Filehwnd,#Null) ;文件长度
  STRR.S=Space(fsize);填充长度
  ReadFile_(Filehwnd,@STRR,fsize,@sbuff,0)
  CloseHandle_(Filehwnd)
  EndIf
  ProcedureReturn STRR
EndProcedure




__________________________________________________
Title changed
29.02.2016
RSBasic
Last edited by zgneng on Mon Feb 29, 2016 8:46 am, edited 1 time in total.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: purebasic BUG

Post by Fred »

which bug ?
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: purebasic BUG

Post by falsam »

Fred wrote:which bug ?
Root disk acces ?

Try this code

Code: Select all

q.l= CreateFile_(GetHomeDirectory()+"Desktop/888.txt", #GENERIC_READ,  0, 0, #OPEN_ALWAYS, #FILE_ATTRIBUTE_NORMAL, 0)

Global a.s{10000000}

Debug q

ReadFile_(q, @a, 9, #Null, #Null)
Question : Why not use the native features of PureBasic?

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: purebasic BUG

Post by bbanelli »

I'm getting IMA (write error at address 0) for both OP's and falsam's example.
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: purebasic BUG

Post by falsam »

bbanelli wrote:I'm getting IMA (write error at address 0) for both OP's and falsam's example.
[Window 10] [PB 5.42 x86] No IMA. The file is created.

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
fryquez
Enthusiast
Enthusiast
Posts: 391
Joined: Mon Dec 21, 2015 8:12 pm

Re: purebasic BUG

Post by fryquez »

No,bug See MSDN

lpNumberOfBytesRead [out, optional]
A pointer to the variable that receives the number of bytes read when using a synchronous hFile parameter. ReadFile sets this value to zero before doing any work or error checking. Use NULL for this parameter if this is an asynchronous operation to avoid potentially erroneous results.

This parameter can be NULL only when the lpOverlapped parameter is not NULL.

For more information, see the Remarks section.
zgneng
User
User
Posts: 29
Joined: Sun Jun 28, 2015 9:04 am

Re: purebasic BUG

Post by zgneng »

fryquez wrote:No,bug See MSDN

lpNumberOfBytesRead [out, optional]
A pointer to the variable that receives the number of bytes read when using a synchronous hFile parameter. ReadFile sets this value to zero before doing any work or error checking. Use NULL for this parameter if this is an asynchronous operation to avoid potentially erroneous results.

This parameter can be NULL only when the lpOverlapped parameter is not NULL.

For more information, see the Remarks section.

No solution yet


powerbasic no problem
zgneng
User
User
Posts: 29
Joined: Sun Jun 28, 2015 9:04 am

Re: purebasic BUG

Post by zgneng »

falsam wrote:
Fred wrote:which bug ?
Root disk acces ?

Try this code

Code: Select all

q.l= CreateFile_(GetHomeDirectory()+"Desktop/888.txt", #GENERIC_READ,  0, 0, #OPEN_ALWAYS, #FILE_ATTRIBUTE_NORMAL, 0)

Global a.s{10000000}

Debug q

ReadFile_(q, @a, 9, #Null, #Null)
Question : Why not use the native features of PureBasic?
Want to know the problem
No solution yet

powerbasic no problem
User avatar
TI-994A
Addict
Addict
Posts: 2705
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: purebasic BUG

Post by TI-994A »

falsam wrote:[Window 10] [PB 5.42 x86] No IMA. The file is created.
Yes; no IMA (Windows 8.1 with PureBasic v5.41 LTS x64).
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
zgneng
User
User
Posts: 29
Joined: Sun Jun 28, 2015 9:04 am

Re: purebasic BUG

Post by zgneng »

TI-994A wrote:
falsam wrote:[Window 10] [PB 5.42 x86] No IMA. The file is created.
Yes; no IMA (Windows 8.1 with PureBasic v5.41 LTS x64).
Want to know the problem
No solution yet

powerbasic no problem
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: purebasic BUG

Post by Thunder93 »

You are going about this the wrong way.

You say there is a problem with PB and Win API usage, but you don't provide anything other than a bug cry. You expect a solution, you repeatedly posts that powerbasic has no problem. You aren't discovering a bug, you are discovering that you are lacking understanding of what's happening.

You trying to create a file 888.txt on the root drive. CreateFile_() is failing because of permissions restrictions. Of course ReadFile_() will fail next.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
infratec
Always Here
Always Here
Posts: 7591
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: purebasic BUG

Post by infratec »

Solution:

Use PB commands, no problem.
Or use PowerBasic.

Also no problem with this.
User avatar
mhs
Enthusiast
Enthusiast
Posts: 101
Joined: Thu Jul 02, 2015 4:53 pm
Location: Germany
Contact:

Re: purebasic BUG

Post by mhs »

infratec wrote:Solution:

Use PB commands, no problem.
Or use PowerBasic.

Also no problem with this.
+1 :lol:

PS: falsams code schould be working...
zgneng
User
User
Posts: 29
Joined: Sun Jun 28, 2015 9:04 am

Re: purebasic BUG

Post by zgneng »

mhs wrote:
infratec wrote:Solution:

Use PB commands, no problem.
Or use PowerBasic.

Also no problem with this.
+1 :lol:

PS: falsams code schould be working...

Code: Select all



Procedure.s GETFILETXT(PATH.l);文件名地址
  sbuff.l;返回长度
  Filehwnd.l= CreateFile_(PATH, #GENERIC_READ,  0, 0, #OPEN_ALWAYS, #FILE_ATTRIBUTE_NORMAL, 0)
  If Filehwnd
  fsize.l=GetFileSize_(Filehwnd,#Null) ;文件长度
  STRR.S=Space(fsize);填充长度
  ReadFile_(Filehwnd,@STRR,fsize,@sbuff,0)
  CloseHandle_(Filehwnd)
  EndIf
  ProcedureReturn STRR
EndProcedure

User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: purebasic resolved

Post by Thunder93 »

This is the first step into the right direction, my friend. However it usually helps to offer little description of the problem you experiencing, regardless how obvious it may be to you.

Code: Select all

Procedure.s GETFILETXT(*PATH)
  sbuff.l
  hFile.l= CreateFile_(*PATH, #GENERIC_READ,  0, 0, #OPEN_EXISTING, #FILE_ATTRIBUTE_NORMAL, 0)
  
  If hFile <> #INVALID_HANDLE_VALUE 
    fsize.l=GetFileSize_(hFile,#Null)
    STRR.S=Space(fsize)
    ReadFile_(hFile,@STRR,fsize,@sbuff,0)
    CloseHandle_(hFile)  

    If #PB_Compiler_Unicode : STRR = PeekS(@STRR, MemoryStringLength(@STRR), #PB_Ascii) : EndIf    

  Else
    STRR = "Oops! What Happened?!?! ;p"    
  EndIf
  
  ProcedureReturn STRR
EndProcedure


Debug GETFILETXT(@"C:\OutOfTheBox\888.txt")
Giving that no description of the problem was provided. I'll just post an example that works for me in ASCII and Unicode modes.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Post Reply