Win API call not working? [SOLVED]

Just starting out? Need help? Post your questions and find answers here.
davebar
User
User
Posts: 82
Joined: Fri Aug 31, 2018 9:23 am
Location: Australia

Win API call not working? [SOLVED]

Post 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.
Last edited by davebar on Tue Jul 30, 2019 2:57 pm, edited 1 time in total.
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Win API call not working?

Post by RSBasic »

This WinAPI function only works in ANSI mode.
Image
Image
davebar
User
User
Posts: 82
Joined: Fri Aug 31, 2018 9:23 am
Location: Australia

Re: Win API call not working?

Post by davebar »

RSBasic wrote:This WinAPI function only works in ANSI mode.
Can you please tell me how to set the ANSI mode?
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Win API call not working?

Post 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())
Image
Image
Justin
Addict
Addict
Posts: 832
Joined: Sat Apr 26, 2003 2:49 pm

Re: Win API call not working?

Post 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)
davebar
User
User
Posts: 82
Joined: Fri Aug 31, 2018 9:23 am
Location: Australia

Re: Win API call not working?

Post 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.
davebar
User
User
Posts: 82
Joined: Fri Aug 31, 2018 9:23 am
Location: Australia

Re: Win API call not working?

Post by davebar »

Thanks Justin, that works very nicely.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Win API call not working?

Post 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.
User avatar
NicTheQuick
Addict
Addict
Posts: 1227
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Win API call not working?

Post 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?
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
davebar
User
User
Posts: 82
Joined: Fri Aug 31, 2018 9:23 am
Location: Australia

Re: Win API call not working?

Post by davebar »

Thanks Mijikai,
I never cease to be amazed by the skill and depth of knowledge in this forum.
Post Reply