petermat1
Dec 9, 2016
ABCs of APIs 11 - Translating documentation for DLLs in MS "C" into Liberty BASIC
PetermatABCs of APIs 11 - Translating documentation for DLLs in MS "C" into Liberty BASIC | Introduction
Introduction
I am afraid this note is not by your friendly expert Alyce Watson, but by ignoramus Petermat. Why? Because Alyce did not write this, I needed it, and all I can do is to try to find my way. If I fail maybe someone will be encouraged to pick up and correct the note. (This is a wiki, right?).Why is the note necessary? Because Microsoft documents it's API's using C / C++
The First Example.
The following LB code is due to Alyce Watson at http://libertybasicuniversity.com/lbnews/nl109/browse.htm .
I don't intend to go into the semantics or use of this - just its translation.
calldll #shell32, "SHGetPathFromIDList",_The MS definition at https://msdn.microsoft.com/en-us/library/windows/desktop/bb762194(v=vs.85).aspx is:
lpIDList as long,_
sPath$ as ptr,_
r as long
BOOL SHGetPathFromIDList(This is C++ code. SHGetPathFromIDList is the name of a function - just like an LB function - and the comma delimited items inside the () are the two function parameters. A C++ function returns a value just like an LB function - which
_In_ PCIDLIST_ABSOLUTE pidl,
_Out_ LPTSTR pszPath
);
The Second Example.
Now let's start with the C++ definition and code from https://msdn.microsoft.com/en-us/library/windows/desktop/bb762115(v=vs.85).aspx
Looking at this code we see that the return is of type "PIDLIST" (whatever that is), and there is one parameter which is used as an input by function<span style="font-family: Consolas,Courier,monospace; font-size: 14px;">PIDLIST_ABSOLUTEPIDLIST_ABSOLUTE SHBrowseForFolder(
_In_ LPBROWSEINFO lpbi
);</span>
SHBrowseForFolder. The definition is headed " The Windows Shell Shell Reference LPBROWSEINFO is "A pointer to a BROWSEINFO structure" - which at least tells us it is of type struct from an LB perspective. And lastly, the definition states that the return is "a PIDL that specifies the location of the selected folder" - so this is an address and hence needs to be 'long'. A PIDL is a "Pointer to an Item iDentifier List". Windows Shell need to know the identity of all the items it manages. Most, but not all, of these "items" are files with an associated file path. Shell deals with all its items by assigning a unique id number to each item and storing all these ids in an Identifier list. For all purposes associated with files, each PIDL has a one to one relationship with a specific file path.
calldll #shell32, "SHBrowseForFolder",_All very well - but what about this struct? The BROWSEINFO structure is defined in
lbpi struct,_
result as long
https://msdn.microsoft.com/en-us/library/windows/desktop/bb773205(v=vs.85).aspx
as shown below on the left
<span style="font-family: Consolas,Courier,monospace; font-size: 14px;"><span style="color: #0000ff;"> C++ definition Equivalent LB codeTranslating this is (mostly) straightforward - and produces the LB code on the right above. I have no idea what the "BROWSEINFO, *PBROWSEINFO, *LPBROWSEINFO" text is about - it appears not to be necessary to the translation into LB (Anyone?)
typedef struct</span> _browseinfo { STRUCT BrowseInfo,_
HWND hwndOwner; hWndOwner As uLong,_
PCIDLIST_ABSOLUTE pidlRoot; pIDLRoot As Long,_
LPTSTR pszDisplayName; pszDisplayName As Long,_
LPCTSTR lpszTitle; lpszTitle$ As ptr,_
UINT ulFlags; ulFlags As Long,_
BFFCALLBACK lpfn; lpfnCallback As Long,_
LPARAM lParam; lParam As Long,_
<span style="color: #0000ff;">int</span> iImage; iImage As Long
} BROWSEINFO, *PBROWSEINFO, *LPBROWSEINFO</span>
The End . . ..
Well that's it as far as translation from C++ API calls to LB is concerned. "But" do I hear you cry "I don't understand all the terms in the struct you just defined, or what to do with a PIDL, or, or, or . . . !" Well yes, to be honest, that's where the hard work starts. There is no 'one size fits all' explanation from here on in. You just have to dive into the https://msdn.microsoft.com/en-us/library/windows/desktop/ jungle and try to make sense of what you need. For example you can follow up on PIDLs at https://msdn.microsoft.com/en-us/library/windows/desktop/cc144090(v=vs.85).aspx . This is very helpful on this topic, but includes terms you may not understand (I don't) like, "UNC path", " Namespace Extensions.", "SHITEMID structure", relative and single- level PIDLs, etc., and you are likely to have to follow up on "Getting a Folder's ID," at https://msdn.microsoft.com/en-us/library/windows/desktop/bb776914(v=vs.85).aspx , which may well lead to yet further explanations on yet further web pages - and so on, and on. so now you know why we get paid the big bucks!