Silver
January 5th, 2006, 07:15
Hi guys,
I'm interested in a specific aspect of how VB works. To call win32api functions from VB you must declare them:
(from MSDN). The real API call looks like this:
Fine. VB only comes with a small set of built in types (string, integer etc). I'd like to understand how VB translates the built in types into win32api C compatible vars.
For example, the function above is easy. We could have a lookup table that says "HWND is a dword, which is pretty much int-compatible so we'll map HWND:Integer. LPCTSTR is a const string, so we'll map String:LPCTSTR" etc.
That's fine, but what happens with the more complex calls - the ones that take more exotic types and modify them. Consider this:
lpString receives the windowtext. So whilst at VB level the user does this:
Somewhere within the VB interpreter there must be logic that understands lpString receives a value rather than provides it, and thus lpString must be created on the heap before the call is dispatched...
But from the function declaration in VB there's absolutely no indication of this. There's nothing to clearly say "this LPTSTR receives a value", as opposed to all the other functions that have an LPTSTR param and provide a value. There's also nothing that links the last int (max chars to return) to the sizeof the LPTSTR. Hopefully my question makes sense...
VB must do this programmatically rather than through a predefined lookup table of win32 functions, because VB can do the same for any DLL you give it.
Does anyone know how VB does this?
I'm interested in a specific aspect of how VB works. To call win32api functions from VB you must declare them:
Code:
Declare Auto Function MBox Lib "user32.dll" Alias "MessageBox" ( _
ByVal hWnd As Integer, _
ByVal txt As String, _
ByVal caption As String, _
ByVal Typ As Integer) _
As Integer
(from MSDN). The real API call looks like this:
Code:
int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);
Fine. VB only comes with a small set of built in types (string, integer etc). I'd like to understand how VB translates the built in types into win32api C compatible vars.
For example, the function above is easy. We could have a lookup table that says "HWND is a dword, which is pretty much int-compatible so we'll map HWND:Integer. LPCTSTR is a const string, so we'll map String:LPCTSTR" etc.
That's fine, but what happens with the more complex calls - the ones that take more exotic types and modify them. Consider this:
Code:
int GetWindowText(HWND hWnd, LPTSTR lpString, int nMaxCount);
lpString receives the windowtext. So whilst at VB level the user does this:
Code:
Declare Function Lib "user32.dll" Alias "GetWindowText" (ByVal hWnd As Integer, ByRef lpString as String, ByVal nMaxCount as Integer)
GetWindowText(wnd, mystring$, 200)
Somewhere within the VB interpreter there must be logic that understands lpString receives a value rather than provides it, and thus lpString must be created on the heap before the call is dispatched...
But from the function declaration in VB there's absolutely no indication of this. There's nothing to clearly say "this LPTSTR receives a value", as opposed to all the other functions that have an LPTSTR param and provide a value. There's also nothing that links the last int (max chars to return) to the sizeof the LPTSTR. Hopefully my question makes sense...
VB must do this programmatically rather than through a predefined lookup table of win32 functions, because VB can do the same for any DLL you give it.
Does anyone know how VB does this?