Liberty BASIC Community Forum
« Browse for folder problems »

Welcome Guest. Please Login or Register.
Jan 22nd, 2018, 6:08pm

Rules|Home|Help|Search|Recent Posts|Notification

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Browse for folder problems  (Read 293 times)
Roger Thomas
Junior Member

member is offline


Homepage PM

Posts: 71
xx Browse for folder problems
« Thread started on: Mar 6th, 2017, 11:27pm »

Alright, I have been having a problem with this in my player for a long time. In the player, everything works okay except for a random non continue-able error ever so often. It might work good 1 time or it might go 100 times before it crashes. I can't use debug for this in my player because it will crash every time. I do switch back and forth in my player from wait to scan when adding files and different things. Don't ask why, it just ended up that way when I needed to implement callbacks. ???
Works good except for Add Folder.
I don't think that's the main problem because this code will still cause a noncontinuable error ever so often. This code here also has a stack overflow which my player does not have. I'm just tired of looking at this piece of code and need some help with it.
This is not the exact code from the player, but very similar without the extras.

Can anyone find out why it throws a non-continuable crash ever so often?
It's the only main bug I have left.(I hope.)


'set up constants for api calls
 MAX.PATH = 260

open "test folder" for window as #main
handle = hwnd(#main)

 dlgCaption$ = "Test Program"

'create struct for api call
pszDisplayName$ = space$(MAX.PATH)
Struct BrowseInfo,_
    hWndOwner As long,_
    pIDLRoot As long,_
    pszDisplayName$ As ptr,_
    lpszTitle$ As Ptr,_
    ulFlags As ulong,_
    lpfnCallback As long,_
    lParam As long,_
    iImage As long,_
    dlgCaption as ptr, _ 'these two are not part of the normal BROWSEINFO struct
    defFldr as ptr

  BrowseInfo.hWndOwner.struct = handle'Set the owner window, optional
  'set up text to display on the dialog:
    txt1$="Folder Browser!" + crlf$
    txt1$=txt1$+"Please choose a Folder!"+chr$(0)
    BrowseInfo.lpszTitle$.struct = txt1$'fill the struct item with the text

  BrowseInfo.ulFlags.struct = BIF.RETURNONLYFSDIRS'Return only if the user selected a directory
  BrowseInfo.pIDLRoot.struct = 0

  BrowseInfo.dlgCaption.struct = dlgCaption$

  BrowseInfo.defFldr.struct = DefaultDir$'folder to open

  callback lpfn, BrowseCallbackProc(long,ulong,long,long),long
  BrowseInfo.lpfnCallback.struct = lpfn

open "ole32" for dll as #ole
'Initialize COM before using SHBrowseForFolder
    calldll #ole, "CoInitialize",_
                   0 as ulong,_
                   ret as long

    calldll #shell32, "SHBrowseForFolder",_'Show the 'Browse for folder' dialog
        BrowseInfo as struct,_
        lpIDList as long

       sPath$ = space$(MAX.PATH) + chr$(0)
        'Get the path from the IDList
        calldll #shell32, "SHGetPathFromIDList",_
            lpIDList as long,_
            sPath$ as ptr,_
            r as long
        'the folder name is now in sPath$
print sPath$
if r <> 1 then print "Cancelled"

       'free the block of memory
        calldll #ole, "CoTaskMemFree",_
                       lpIDList as long,_
                       r as long
close #ole
BrowseForFolder = 1 'scan while finding files

close #main

[Scan] ' Scan here for input
'for i = BrowseForFolder to 0 step -0.05
'BrowseForFolder = i
    'if i <= 0.05 then exit for
'next i
CallDLL #kernel32, "Sleep",1 As ULong,ret As Void
   GoTo [Scan]

Function BrowseCallbackProc(hWnd,uMsg,lParam,lPdata)
    select case uMsg
        Caption$ = winstring(BrowseInfo.dlgCaption.struct)
       calldll #user32, "SetWindowTextA", hWnd as long, _
        Caption$ as ptr, result as long
        def$ = winstring(BrowseInfo.defFldr.struct)
       if def$ <> "" then
        Calldll #user32, "PostMessageA", hWnd As long, _
        BFFM.SETSELECTION As long, 1 As long, def$ as ptr, result As long
       end if
    end select
End Function

A couple things I commented out I was just testing.

User IP Logged

Global Moderator

member is offline


Graphics = goosebumps!


Gender: Male
Posts: 5618
xx Re: Browse for folder problems
« Reply #1 on: Mar 7th, 2017, 01:23am »

It is generally well known that you must use ulong whenever you pass or use a Windows handle. So in your struct definition and wherever you call an api ensure any handle is type ulong.

It's a quirk of Liberty, in other languages it makes no difference if they are long or ulong. It's to do with hwnd() and how it formats the retuned handle, if you print the result on error you will see the handle is negative, this should never happen.

I ran the code, got the error. I changed hwnd and hwndowner to ulong, seems to work now
« Last Edit: Mar 7th, 2017, 03:41am by Rod » User IP Logged

Roger Thomas
Junior Member

member is offline


Homepage PM

Posts: 71
xx Re: Browse for folder problems
« Reply #2 on: Mar 7th, 2017, 08:46am »

I'll change that and try it in my program. It seems to me that I remember discussing this before. Surely I tried changing the handle type. Maybe not.
I kept thinking in my program that it was waiting during the callback and debug wouldn't work in liberty because of it?

Thanks a lot. I'll try it and see, it'll take a while before I know, probably.


User IP Logged


member is offline



Gender: Male
Posts: 630
xx Re: Browse for folder problems
« Reply #3 on: Mar 7th, 2017, 12:28pm »

Read the two "sticky" topics on this very same board, the one LONG vs ULONG, Alyce explain there the difference.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls