Page 1 of 1

Win API call not working? [SOLVED]

Posted: Tue Jul 30, 2019 1:09 pm
by davebar
Can anyone suggest why the following call always returns false?

Code: Select all

EnableExplicit
Define Buffer$ = Space(256)
Define Result

Result = SearchTreeForFile_("D:\Temp\", "Dummy.txt", @Buffer$)
If Result=0
  MessageRequester("", "No file with this name was found.", 0)
Else
  MessageRequester("", "First found file: " + Buffer$, 0)
EndIf
PB5.71b2 on Win 7 x64 The referenced file definitely exists.

Re: Win API call not working?

Posted: Tue Jul 30, 2019 1:24 pm
by RSBasic
This WinAPI function only works in ANSI mode.

Re: Win API call not working?

Posted: Tue Jul 30, 2019 1:44 pm
by davebar
RSBasic wrote:This WinAPI function only works in ANSI mode.
Can you please tell me how to set the ANSI mode?

Re: Win API call not working?

Posted: Tue Jul 30, 2019 1:49 pm
by RSBasic
This is not possible in the newer PB version. You need the Unicode function of SearchTreeForFile_(). I don't know if a Unicode function exists.

Why don't you use the native function of PureBasic? (ExamineDirectory())

Re: Win API call not working?

Posted: Tue Jul 30, 2019 2:14 pm
by Justin
You can call the unicode version, (i changed the file name):

Code: Select all

EnableExplicit
Define Buffer$ = Space(256)
Define Result
Define.i hlib

Prototype p_SearchTreeForFileW(rt.s, ip.s, ob.i)
Global.p_SearchTreeForFileW SearchTreeForFileW

hlib = OpenLibrary(#PB_Any, "Dbghelp.dll")
SearchTreeForFileW = GetFunction(hlib, "SearchTreeForFileW")

Result = SearchTreeForFileW("c:\", "test.bin", @Buffer$)
If Result=0
  MessageRequester("", "No file with this name was found.", 0)
Else
  MessageRequester("", "First found file: " + Buffer$, 0)
EndIf

CloseLibrary(hlib)

Re: Win API call not working?

Posted: Tue Jul 30, 2019 2:22 pm
by davebar
The native function ExamineDirectory() would have been my first choice, but I wanted to check out if your Win API example [1] might have been a better option
[1] https://www.rsbasic.de/aktualisierung/w ... 0suchen.pb
Many thanks for taking the time to help.

Re: Win API call not working?

Posted: Tue Jul 30, 2019 2:28 pm
by davebar
Thanks Justin, that works very nicely.

Re: Win API call not working?

Posted: Tue Jul 30, 2019 2:36 pm
by Mijikai
Another solution:

Code: Select all

EnableExplicit

Import "imagehlp.lib"
  SearchTreeForFile(Path.p-Ascii,File.p-ascii,*Result)
EndImport

Global *buffer

*buffer = AllocateMemory(#MAX_PATH)

If *buffer
  If SearchTreeForFile("D:\Temp\","Dummy.txt",*buffer)
    MessageRequester("", "First found file: " + PeekS(*buffer,-1,#PB_Ascii), 0)
  Else   
    MessageRequester("", "No file with this name was found.", 0)
  EndIf
  FreeMemory(*buffer)
EndIf

End
Or grab the proper lib from MSVS and import the W version directly.

Re: Win API call not working?

Posted: Tue Jul 30, 2019 2:42 pm
by NicTheQuick
I am just curious. But is there also a WinAPI function which makes use of the indexed files for a faster search experience?

Re: Win API call not working?

Posted: Tue Jul 30, 2019 2:47 pm
by davebar
Thanks Mijikai,
I never cease to be amazed by the skill and depth of knowledge in this forum.