• Welcome to Powerbasic Museum 2020-B.
 

ProgEx12 - Simple Example Of Win Api Calling in PowerBASIC - GetCurrentDirectory

Started by Frederick J. Harris, November 06, 2009, 03:35:53 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frederick J. Harris


'ProgEx12  -- This is a PowerBASIC program using the Console Compiler 5.02.
'If you don't have the Console Compiler you could use MsgBox or better an
'output log file to display the output.
'
'The first thing you should note about this program is that it is incredibly
'similiar to the C program ProgEx11.  Here is the function call again in C
'for easy comparison purposes...
'
' C           --  dwReturn = GetCurrentDirectory(MAX_PATH,szBuffer);
'
' PowerBASIC  --  dwReturn = GetCurrentDirectory(%MAX_PATH,szBuffer)
'
'For one thing they are the exact number of characters; the only difference
'being the obligatory semicolon at the end of the C function call balanced
'by the obligatory '%' symbol in front of the MAX_PATH equate in PowerBASIC.
'There are some differences though that absolutely need to be understood if
'one wishes to freely use and understand Microsoft C documentation and the
'Windows Api.  And of course the difference concerns what else but strings!
'For you see, unlike with the C programming language which has no variable
'type specially created to contain strings in the BASIC sense, PowerBASIC
'does have several types of string variables for your use to make coding
'easier.  The PowerBASIC Asciiz String Type isn't syntactically an array
'as the C char szBuffer[MAX_PATH] type is.  If one were to try to come up with
'a close PowerBASIC approximation of the C szBuffer[] array of the last program
'one would probably use PowerBASIC's Byte data type and do a declaration
'something like this...
'
' Local byteVar() As Byte
'
' Redim byteVar(%MAX_PATH) As Byte
'
'...and in that way have a true array which could contain bytes interpretable
'as asci character codes.  And in fact PowerBASIC could output such a string
'to the screen - but you would need to set a Asciiz Ptr variable to the address
'of the first byte of the array and be sure to place a null terminator at the
'end and work with it that way (that's rather awkward, to be sure).  But none
'of this nonsense was needed as PowerBASIC's Asciiz data type surficed for our
'needs rather simply - or so it seems.  However, you need to have a perfect
'understanding of how this worked 'behind the scenes' so to speak.
'
'If you recall back in ProgEx07 and ProgEx08 we showed how C passes the address
'of a variable to a function that needs that address through the C pointer
'mechanism.  The GetCurrentDirectory function clearly is an example of a
'function that needs an address because it needs to know where to stuff the
'desired directory path.  What the C compiler did was set up the function to
'copy the base address of the szBuffer array to a temporary location on the
'stack so that it could be retrieved by the executable code of the function.
'With PowerBASIC, on the other hand, the passing mechanism is more like that
'of ProgEx08 where the C++ pass by reference method was used.  In that method,
'if you recall, we simply placed a variable in the parameter position of the
'function call, and the compiler silently passed the variable's address to the
'function without explicitely using the C '&' (address of) operator to take the
'address first.  Exactly the same thing happens in the PowerBASIC program below
'where we simply placed our szBuffer Asciiz variable in the GetCurrentDirectory
'function and PowerBASIC passed the address of that variable to the function.
'The function itself doesn't care how it gets a valid address where it can
'place its output.  It doesn't even care what language is used.  All it needs
'is the address of the buffer and its size.  This is an important concept and
'you might want to review examples 6, 7, and 8, and then come back to these
'two programs to compare the effects.
'
'After you are satisfied with this material we'll move on to Dll in C, C++ and
'PowerBASIC.
#Compile Exe
#Dim All
#Include "Win32Api.inc"


Function PBMain() As Long
 Local dwReturn,dwBufferLength As Dword
 Local szBuffer As Asciiz*%MAX_PATH

 dwReturn = GetCurrentDirectory(%MAX_PATH,szBuffer)
 Print "%MAX_PATH        = " %MAX_PATH
 Print "dwReturn         = " dwReturn
 Print "szBuffer         = " szBuffer
 Print "Varptr(szBuffer) = " Varptr(szBuffer)
 Waitkey$

 PBMain=0
End Function

'Output
'============================================================
'%MAX_PATH        =  260
'dwReturn         =  41
'szBuffer         = C:\Code\Dev-Cpp\Projects\CPrimer\ProgEx12
'Varptr(szBuffer) =  1244508