Announcement

Collapse
No announcement yet.

Declares in WinAPI (maybe mistake ?)

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

    Declares in WinAPI (maybe mistake ?)

    Hi,

    I encountered the following in the latest win32api.inc file.
    Code:
    DECLARE FUNCTION SysAllocString LIB "OLEAUT32.DLL" ALIAS "SysAllocString" (sz AS ANY) AS DWORD
    DECLARE FUNCTION SysStringLen LIB "OLEAUT32.DLL" ALIAS "SysStringLen" (bstr AS ANY) AS DWORD
    When I use this as they are I get very weird results. However when I change it to the following
    Code:
    DECLARE FUNCTION SysAllocString LIB "OLEAUT32.DLL" ALIAS "SysAllocString" (sz AS ASCIIZ) AS DWORD
    DECLARE FUNCTION SysStringLen LIB "oleaut32.DLL" ALIAS "SysStringLen" (BYVAL hBSTR AS DWORD) AS DWORD
    There are no problems.

    Any ideas. I think all the ones that state AS ANY should be changed to
    AS DWORD except for the SysAllocString



    ------------------
    So here we are, this is the end.
    But all that dies, is born again.
    - From The Ashes (In This Moment)

    #2
    The funny part is, if you remove them from the win32api.inc, you'll still able to call these.

    The compiler adds them anyway so , another declaration is useless.

    I think you should use Byval StrPtr() or VarPtr() to be safe.
    +, use the SysAllocStringByte[Len] stuff for single byte..



    ------------------
    http://www.hellobasic.com
    hellobasic

    Comment


      #3
      Just my opinion, but whenever you encounter a DECLARE with one or more parameters defined 'AS ANY', take a break to think about it, and maybe read the SDK help file for the function.

      AS ANY forces the call to pass the address of the variable or expression used in the CALL (or the implicit CALL when the procedure is a FUNCTION) unless an override is made at the point of the call.

      Some of the conversions in the PB versions of the header (*.inc) files required a decision by the person doing the conversion, and the path they chose may not have agreed with the way you would have converted the "C" language reference had you done it yourself. This is not to say those conversions are incorrect - it's just that absent the 'in-line casting' available in C/C++, PB needs to make a decision about conversion.

      MCM
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


        #4
        Well put. "AS ANY" is a good hint to slow down and read the docs.
        It means there are different ways of calling the routine, and you
        need to sort out the implications before choosing one.

        SysAllocString takes a Unicode string parameter. This is not
        compatible with AS ASCIIZ or AS STRING declarations (AS STRING is
        "close", but uses ANSI rather than Unicode). PowerBASIC does not
        support Unicode directly, so you will need to handle these as
        generic pointers (DWORD or LONG values).

        The definition (bstr AS ANY) means exactly the same thing
        as (BYVAL hBSTR AS DWORD), by the way... the former just
        provides a better indication that you're passing a "mystery value",
        by reference, where the latter may give the false impression that
        you're just passing a number by value.

        ------------------
        Tom Hanlin
        PowerBASIC Staff

        Comment

        Working...
        X
        😀
        🥰
        🤢
        😎
        😡
        👍
        👎