Announcement

Collapse
No announcement yet.

Using WIN32API

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Fred Harris
    replied
    A Simple Example

    Here's a simple Console Compiler example I put together quick to illustrate what I was saying above using something concrete as opposed to expousing generalities. I was going to use the Api counterpart to the PowerBASIC function that returns the current directory ( CurDir$ ), but in searching for it I spotted this instead - GetSystemDirectory(). I never used that but for this example its as good as anything. Here's what I put together...

    Code:
    'Below is Microsoft's C description of GetSystemDirectory().  The function returns the length
    'of the system directory.  The buffer itself that contains the system directory is an 'output'
    'parameter (now there's a whole concept there that needs to be understood!).  For this function
    'to work it needs to be 'fed'.  The food you give it is a chunk of memory where it can put your
    'directory string.  You give it that memory by dimensioning an Asciiz fixed length string as
    'part of the setup in Main().  The 'way' you give it that string is by giving it the address
    'of the 1st byte of the string by putting the lpBuffer variable as the 1st parameter in the
    'function call.  Here is the PowerBASIC declaration of GetSystemDirectory()...
    
    'DECLARE FUNCTION GetSystemDirectory LIB "KERNEL32.DLL" ALIAS "GetSystemDirectoryA" (lpBuffer AS ASCIIZ, BYVAL nSize AS DWORD) AS DWORD
    '
    'Note lpBuffer As Asciiz.  Since there is no 'override' on the parameter, PowerBASIC will pass to
    'the function exactly what the Api specifies, i.e., ...Pointer to the buffer to receive the null
    '-terminated string containing the path.
    
    'Another very important concept to understand there!  And look at the 2nd parameter.  It has
    'what is termed an 'override' in front of it, i.e., 'byval'.  The reason for this is that its an
    ''input' parameter, and just simply a value needs to be passed.  Here is the function description...
    '==================================================================================================
    'UINT GetSystemDirectory
    '(
    '  LPTSTR lpBuffer,  // address of buffer for system directory
    '  UINT uSize        // size of directory buffer
    ');
    '
    'Parameters
    '
    'lpBuffer    Pointer to the buffer to receive the null-terminated string containing the path. This
    '            path does not end with a backslash unless the system directory is the root directory.
    '            For example, if the system directory is named WINDOWS\SYSTEM on drive C, the path
    '            of the system directory retrieved by this function is C:\WINDOWS\SYSTEM.
    '
    'uSize       Specifies the maximum size of the buffer, in characters. This value should be set to
    '            at least MAX_PATH.
    
    'If the function succeeds, the return value is the length, in characters, of the string copied to
    'the buffer, not including the terminating null character. If the length is greater than the size
    'of the buffer, the return value is the size of the buffer required to hold the path.
    
    'If the function fails, the return value is zero. To get extended error information,
    'callGetLastError.
    '===================================================================================================
    
    'Some observations of a general nature to take home with you is that Api functions almost always
    'return information through function parameters - not return values.  This is another concept that
    'is probably new to you, and bears considerable rumination upon.  So all in all, this little
    'example rally shows/introduces many important concepts that need to be fully understood.  But
    'the concepts themselves are not unique to this one function - they apply to the several thousand
    'of them.
    
    
    #Compile Exe
    #Dim All
    #Include "Win32Api.inc"
    
    Function PBMain() As Long
      Local lpBuffer As Asciiz*%MAX_PATH
      Local uSize,iReturn As Dword
    
      uSize=%MAX_PATH
      iReturn=GetSystemDirectory(lpBuffer,uSize)
      If iReturn Then
         Print "The Function Call Succeeded!"
         Print "GetSystemDirectory() = "lpBuffer
         Print "iReturn              = "iReturn
      Else
         Print "The Function Call Failed!"
      End If
      WaitKey$
    
      PBMain=0
    End Function

    Leave a comment:


  • Fred Harris
    replied
    Hi John!

    I'd like to hazzard a guess as to why you aren't finding exactly what you are looking for in terms of Win32Api help.

    It is because there is a definite pattern to the translation of C Api variables and function prototypes to their PowerBASIC equivalent. The pattern is so 'set in stone' and invariable that it would render any attempt to do the translation you are looking for as a silly endeavor. Once the base or fundamental programming concepts are understood, the translation of the variable or function to its PowerBASIC equivalent becomes trivial. The concepts that need to be understood are basically the respective methods of C and PowerBASIC with reference to passing function parameters, i.e., ByVal or ByRef. Also, pointers need to be understood. This is a concept somewhat more associated with C and C++, but with PowerBASIC a pointer is passed every time a function call is made (if there are any ByRef parameters), whether the programmer knows it or not. Finally, in the PowerBASIC documentation are tables showing how the variables compare, i.e., C unsigned int, PowerBASIC DWORD, etc.

    I guess that is a lot. The other aspect of the issue is having the right tools to quickly and efficiently get your work done. In the example you mentioned above with POINTS or POINTAPI, by using the Lynx Project Explorer (freeware), I was able to locate those variables in about five or six seconds. The latter mentioned tool 'docks' to your programming editor either left or right, and displays the files of your project in an explorer like view where one can drill down to Types, Declares, Equates, etc. For example, I imediately came up with this in terms of the specific Type you gave as an example...

    Code:
    TYPE POINTAPI
      x AS LONG
      y AS LONG
    END TYPE
    
    TYPE POINTL
      x AS LONG
      y AS LONG
    END TYPE
    
    TYPE POINTS
      x AS INTEGER
      y AS INTEGER
    END TYPE
    As you can see, there is only so much you can do with a point.

    So the way to look at it is somewhat like learning a second or third language. When one ventures beyond the confines of knowing just one spoken language, what you are really learning by choosing a second or third language to learn is not just another language, but the fundamentals of linguistics itself, i.e., those symbolic structures common to human communication.

    In using the Windows Api you are doing exactly the same thing but the medium is computer memory, not speech communication. To push the analogy, what you are looking for is akin to a traveler in a foreign land with one of those quick translation dictionaries that tell you how to say 'food' in the local dialect, and thereby eliminate the necessity of learning the native's tongue. That will only get you so far and no further.

    Leave a comment:


  • William Johns
    replied
    No more Greek for this geek!

    PB/CC programming is a hobby for me. This language contains enough
    commands to satisfy my needs. So, in the future, I will only resort to API
    calls in emergencies, such as the Windows Vista's lack of "Fullscreen
    Mode" screwing up the PB/CC "CONSOLE SET SCREEN" command.

    When a PB/CC compile fails it usually gives me a 1-liner suggesting
    what syntax caused the error. When the compiler chokes on an API
    command all I get is "Compile Failed". That's no help at all.
    So I hit the F1 key and the Win32 Programmer's Reference comes up.
    It is written in the "C" language--not in PB/CC. Also, it has no coding
    examples to show me what the command should look like.
    EXAMPLE: Petxold Chapter 6 CONNECT.BAS
    "STATIC points() AS POINTAPI"
    The Programmer's Reference gives me:
    "typedef struct tagPOINTS { // pts
    SHORT x;
    SHORT y;
    } POINTS;"

    Help with specific commands has been offered in this thread...
    It has been invaluable...thanks to all contributors. But I am still
    looking for an explanation (in PB/CC...not PB/WIN or C) and examples
    for SUB/FUNCTION API calls. The original question in this thread:
    "Aren't there better descriptions of, and better help with syntax for,
    calls to the SUBS/FUNCTIONS in WIN32API?".

    It looks like the best resource so far has been reading postings
    in this PB/CC Forum. This Forum has "saved my bacon" more than once!

    Leave a comment:


  • Donald Darden
    replied
    You can get an updated Win32API include file in the download section on the PowerBasic website. They do a good job keeping it up to date.

    Example/Sample code is often the best way to see how API calls are used together. The forum Search tool is handy for finding discussions and code related to your interest. In addition, you can build up from code that approximates what you want to do.

    There are books written that give more details on the APIs, but PowerBasic includes a help reference in the IDE if you use F1 on a non-BASIC term. It is written in the C language syntax, but so are most of the books out there that cover the API. It is just something to deal with, and methods for converting C constructs into PowerBasic,

    Lance Edmonds wrote a table for translating C headers (like Basic Include files) into PB equivalent types, but I can't find it anymore. It was a big help. However, some people prefer to approach this from a VB5 or VB6 reference.

    I did find this: http://www.softpedia.com/get/Program...-Toolkit.shtml

    It includes a tool called hdr2inc.exe, which is suppose to convert header files to include files. Note however, that it probably is better suited to C headers, as C++ conversions are likely harder or less successful.

    Leave a comment:


  • Jerry Fielden
    replied
    If you need some samples/example, you can make your own using this little PBCC style forms that somebody made in PBCC a few years ago.

    Most of the examples were DDT from these forums, converted to SDK to be able to compile with PBCC. Some like poloygon, star, rounded rectangle, oval shaped windows came from other places.

    Its located is this high dollar site here, just turn you anti popper on, these people love advertising. http://fieldens.tripod.com/id13.html

    Leave a comment:


  • Michael Mattias
    replied
    You can always make MSGBOX a PB/CC function...
    Code:
    #IF %DEF(%PB_CC32)
    
      #IF NOT %DEF(%WINAPI)
         DECLARE FUNCTION MessageBox LIB "USER32.DLL" ALIAS "MessageBoxA" (BYVAL hWnd AS DWORD, lpText AS ASCIIZ, lpCaption AS ASCIIZ, BYVAL dwType AS DWORD) AS LONG
         %MB_OK                        = &H00000000&
         %MB_TASKMODAL                 = &H00002000&
      #ENDIF
    
     FUNCTION MSGBOX (szText AS ASCIIZ, OPTIONAL BYVAL STYLE AS DWORD, OPTIONAL szCaption AS ASCIIZ) AS LONG
    
        LOCAL hWnd AS LONG, uType AS LONG, dwCap AS DWORD
    
        uType = IIF& (STYLE=%NULL,%MB_OK OR %MB_TASKMODAL, STYLE)
        dwCap =   VARPTR(szCaption)  ' if null, Windows uses "Error"
        FUNCTION = MessageBox (BYVAL %NULL, szText, BYVAL dwCap, BYVAL uType)
    
      END FUNCTION
    #ENDIF   ' if %DEF (%Pb_CC32)
    .. or even do "yes or no" message boxes...
    Code:
    #COMPILE   EXE
    #DEBUG     ERROR ON
    #REGISTER  NONE
    #TOOLS     OFF
    #DIM       ALL
    
    #INCLUDE "WIN32API.INC"
    
    FUNCTION PBMAIN() AS LONG
    
     LOCAL hWnd AS LONG, szText AS ASCIIZ * %MAX_PATH, szCaption AS ASCIIZ * %MAX_PATH, uTYPE AS LONG
     LOCAL iRET AS LONG
    
     STDOUT "Getting User Response..."
     STDOUT ""
    
     hWnd  = GetDesktopWindow()
     szTExt =  "Do you want to do this?"
     szCaption= "User Response Required"
     uType    =  %MB_ICONQUESTION OR %MB_YESNO
    
     iRet     = MEssageBox (hWnd, szText, szCaption, uType)
     IF iRet  = %IDYES THEN
          CALL YEs
     ELSEIF iRet = %IDNO THEN
          CALL No
     ELSE
         STDOUT "You must have cancelled, because that's the only choice except 'yes' or 'no'
     END IF
     STDOUT ""
     STDOUT "Any Key To Exit"
     WAITKEY$
    
    END FUNCTION
    
    SUB YES ()
    
        STDOUT "Doing the 'YES' Thing"
    
    END SUB
    
    
    
    SUB NO()
    
        STDOUT "Doing the 'NO' Thing
    
    END SUB

    Leave a comment:


  • Kev Peel
    replied
    The first two lines of code you mentioned won't compile in PB/WIN either.

    The difference is caused by syntactical changes in the compiler since the original code was translated, try these instead:

    Code:
       Local sizeA as APISIZE
     
       [..]
     
       DIM points(<count>) AS STATIC POINTAPI ' You need a <count> here
    
       [..]
     
       ? szPoemRes ' This is both PB/WIN and PB/CC compatible (displays text).
    The best resource for PowerBASIC code is in this forum, try searching the "source code" section for keyword "PBCC" or "PB/CC" and see if there is anything that might be related to what you need to do. There are a number of full applications for PB/CC.

    If you do get stuck when working with some code, you can always post another question, and another, and another, etc

    Leave a comment:


  • William Johns
    replied
    Baby steps

    Thanks, Fred, for the slap up-side of th' head. I missed it!
    You wrote a good tutorial.

    I have been hard at work trying to run the examples in
    Petzold and Turner, with mixed results. Examples of the
    roadblocks:
    "Local sizeA as SIZE" Petzold Ch. 4\JUSTIFY.BAS

    "STATIC points() AS POINTAPI" Petzold Ch. 6\CONNECT.BAS

    "MSGBOX szPoemRes" Petzold Ch. 9\POEPOEM.BAS

    It is interesting exploring the "grey area" between PB/CC and PB/WIN.

    Bill

    Leave a comment:


  • Fred Harris
    replied
    John,

    Windows GUI (i.e., PBWin programs) programs will compile and run fine with the console compiler (and produce GUI output). If you don't believe me, follow my first link I gave you above, and at the bottom of that first tutorial is a GUI program that produces a window. It will compile with the console compiler. I was pretty sure it would, but I just checked it again.

    Unless your like expectancy is somewhere around a thousand years, or you are a very fast reader and understander, I doubt that two gigabyte download will be terribly helpful. I expect Mr. Turner's work, as well as the Petzold examples, will be the way to go.

    Leave a comment:


  • William Johns
    replied
    I appreciate the help!

    Thanks for all the suggestions! I am in the process of pursuing
    all of them:
    I spent about half an hour on the Microsoft site suggested above.
    I didn't see a CD, but did see a 2GB download that would take about
    2 hours.
    All the code examples I downloaded (Petzold, Turner,etc.), and
    the suggested books on the Powerbasic website, were written in
    PB/WIN so none will run with my PB/CC compiler. They suggest the
    syntax required to call items in WIN32API.INC, so all is not lost.
    I agree that trying to understand all the Functions contained in
    WIN32API.INC would be ridiculous. I wonder if there is a sub-set
    of them that are really useful to PB/CC programmers? Also, taking
    the reverse approach..."I need to do this, so I'll punch up a sub-list
    of only those Functions which might be what I need."
    So, I am back to looking at the Samples which are part of the
    PB/CC load, and an outdated WIN32API.INC file.

    Again, I really appreciate the specific help, and the continuing
    stream of ideas this Forum generates!

    Bill

    Leave a comment:


  • Michael Mattias
    replied
    Trying to grasp the use of the WinApi starting from an alphabetized list of functions is going to be the long way.

    There have many times been posted here recommended books on Windows' programming; the two most-often mentioned are Petzold's and Richter's.

    You might try a search here on "book" and/or "Petzold" and/or "Richter" to find the complete threads for these book discussions.

    MCM

    Leave a comment:


  • StanHelton
    replied
    Originally posted by William Johns View Post
    Aren't there better descriptions of, and better help with syntax
    for, calls to the SUBS/FUNCTIONS in WIN32API?

    Bill
    Originally posted by Michael Mattias View Post
    Your wish is my command.... http://msdn.microsoft.com/library/default.asp

    Free, too.
    IF you can get in! I've been monopolizing their bandwidth for the last 2 days. The learning curve is a little steep, but I having fun!

    Leave a comment:


  • Michael Mattias
    replied
    Aren't there better descriptions of, and better help with syntax
    for, calls to the SUBS/FUNCTIONS in WIN32API?
    Your wish is my command.... http://msdn.microsoft.com/library/default.asp

    Free, too.

    Leave a comment:


  • Fred Harris
    replied
    This may help even if you aren't interested in GUI programming, as I try to cover parameter passing to the Win32 Api...

    http://www.jose.it-berater.org/smffo...0&topic=1243.0

    To get you started I'll disect FindWindowEx() a bit for you. Here is its prototype from the docs...

    Code:
    HWND FindWindowEx
    (
      HWND hwndParent,      // handle to parent window
      HWND hwndChildAfter,  // handle to a child window
      LPCTSTR lpszClass,    // pointer to class name
      LPCTSTR lpszWindow    // pointer to window name
    );
    A HWND is a PB Dword. The 1st two parameters should be self-explanatory. A LPCTSTR is a long pointer to a constant T string. Don't worry too much about that right now. What you need to place there is the address of the string buffer where the 1st character of the dynamic string actually resides. You would get that with Byval Strptr(PB Dynamic String). The reason for this is that the Strptr() function returns the address of the buffer into which PB copied you string. The Byval is required because PB's standard way of passing parameters to functions is to pass the address of the variable put in the function call. If you already have your data in a dynamic string you would end up passing the string descriptor to the function without the Byval, not the string data. Hope this helps!

    Leave a comment:


  • William Johns
    started a topic Using WIN32API

    Using WIN32API

    #INCLUDE WIN32API.INC

    Windows Vista's lack of "Fullscreen Mode" rendered the
    CONSOLE SET SCREEN command ineffective. A Forums' contributor
    offered a 4-line patch which fixes the CONSOLE SCREEN command
    beautifully. It uses the "FindWindow" and "ShowWindow" functions
    contained in WIN32API.

    I can no longer resist using the WIN32API resource. I now face
    that stiff learning curve, however. I have studied the Windows SDK
    in WINHELP, \WINAPI\readme.txt, and WIN32API.INC . But I'm a
    newbie who thinks a BYVAL is a 2-shelled mollusk. My question:

    Aren't there better descriptions of, and better help with syntax
    for, calls to the SUBS/FUNCTIONS in WIN32API?

    Bill
Working...
X