Tech Articles


Calling SHGetKnownFolderPath from PowerBuilder


Up through Windows XP, if you wanted to get the physical location of certain defined folders (e.g. the users Documents folder), you would use the SHGetFolderPath function in the Windows API.  Roland Smith has examples of using that on his Topwiz Software site.  That function continues to work in later versions of Windows, although it's basically a wrapper for the SHGetKnownFolderPath function.

SHGetFolderPath uses CSIDL values, whereas SHGetKnownFolderPath uses KnownFolderID GUIDs.  One difference is that there are a lot more KnownFolderIDs than there are CSIDLs.  That means some of the defined folder locations you may want to get the physical location for can't be accessed through the older SHGetFolderPath method.  And that's exactly the situation I ran into.


I needed to find the user's Downloads folder, and there isn't a CSIDL value for that.  So, I needed to see how to call the SHGetKnownFolderPath function from PowerBuilder.  As with many OLE and Windows API calls, it can save you a lot of time if you can find some Visual Basic code that does what you need to do, and you can convert the syntax.  And fortunately, I found some that did this.



So, with a little tweaking, I ended up with this:

//Local External Functions:

 Function Long CLSIDFromString (ref string lpszGuid, ref blob pGuid ) Library "ole32"   
 Function Long SHGetKnownFolderPath( ref blob nFolder,int dwFlags,uLong hToken , ref long pszPath) LIBRARY "Shell32.dll"   
 Subroutine CopyMemory ( ref String pDest, long pSrc, long ByteLen) Library "kernel32" Alias For "RtlMoveMemory"   
 Subroutine CoTaskMemFree ( long hMem) Library "ole32"   
 Function Long lstrlenW ( long ptr) Library "kernel32"   

//PowerScript

 blob     guid  
 long     ll_rc, ll_folderpointer, ll_folderlen  
 string ls_folder, ls_folder_id  
 ls_folder_id = '{374DE290-123F-4565-9164-39C4925E467B}'  
 IF CLSIDFromString (ls_folder_id, guid ) = 0 THEN  
      IF SHGetKnownFolderPath ( guid, 0, 0, ll_folderpointer ) = 0 THEN  
           IF ll_folderpointer <>0 THEN  
                ll_folderlen = lstrlenW ( ll_folderpointer )  
                If ll_folderlen <> 0 THEN  
                     ls_folder = Space ( ll_folderlen )                      
                     CopyMemory ( ls_folder, ll_folderpointer, ll_folderlen * 2 )  
                END IF  
           END IF  
           CoTaskMemFree ( ll_folderpointer )  
      END IF  
 END IF  
 MessageBox ( "Downloads Folder", ls_folder )  


Hope you find that useful as well.

 

 

 

 

 

 

Comments (0)
There are no comments posted here yet

Find Articles by Tag

Deployment Icon Model JSONParser Interface DataWindow JSON .NET DataStore Event PDFlib .NET Assembly TLS/SSL ODBC Windows 10 Icons C# Service Git PowerBuilder PowerScript (PS) License RichTextEdit Control Platform Database Painter CoderObject Error Syntax Event Handling Graph Open Source API DevOps OAuth CI/CD BLOB Database Table Data SqlExecutor TortoiseGit Mobile Testing iOS PBDOM Performance RibbonBar SVN Android Database Connection Script Web API WebBrowser Array Text Web Service Proxy Transaction Azure Jenkins HTTPClient .NET Std Framework JSONGenerator SQL CrypterObject Database Profile 64-bit Source Code Repository SOAP SnapDevelop OrcaScript PFC Encryption SQL Server MessageBox InfoMaker GhostScript Automated Testing Class Branch & Merge Event Handler Bug Debug Filter Resize Debugger Authorization RibbonBar Builder Menu Elevate Conference UI Themes 32-bit UI Modernization Window ActiveX PDF Database Object PostgreSQL DLL TFS PostgreSQL ODBC driver Design WinAPI Windows OS Import SqlModelMapper Messagging Sort External Functions Trial Database Table Schema Expression Excel Configuration Oracle PowerServer Web RESTClient Database Table OAuth 2.0 Outlook Linux OS Validation PowerBuilder Compiler XML Charts SDK Stored Procedure DragDrop Data UI IDE PowerServer Mobile Encoding JSON Source Control Installation DataType NativePDF DataWindow OLE Migration Variable PowerBuilder (Appeon) Import JSON COM Application REST TreeView Database SnapObjects Debugging File Export JSON Export Authentication