Announcement

Collapse
No announcement yet.

Win32API.inc question (with long story)

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

  • Win32API.inc question (with long story)

    I wanted to an alternate function for users with Windows earlier than XP SP1. Looked at PB command CODEPTR, but that only returns pointer to proc that it is in. Looking in the PSDK I found GetModuleHandle, which when passed a nul pointer gives handle to the module it is called from. Passing a zero gave String expected error when compiled. Passing $NUL returned a zero handle.

    Looking in Win32API.inc makes problem obvious. My fix was to make another declare for GetModuleHandle without changing the Win32API.inc file (see code).

    It works, I'm happy. Then got to thinking maybe the Win32API.inc was out of date and I wouldn't have to use the modified declare. I was surprised that all the file dates in Win32API.zip in PB download section are 1 Feb 2005! (though file sizes don't match)

    My question Really no changes since 2005, or are file dates forced back to 1 Feb 2005? Why different file sizes if no changes?

    Code:
    'from Win32API.inc dated 1 Mar 2005:
    'declare function GetModuleHandle lib "KERNEL32.DLL" _
                                                 alias "GetModuleHandleA" _
    '                                     (lpModuleName as asciiz) as dword
    
    'Modified declare in my dot bas to avoid duplicate name error:
    declare function GetModuleHandleDY lib "KERNEL32.DLL" _
                                                 alias "GetModuleHandleA" _
                            (byval lpModuleName as asciiz pointer) as dword
    
    '-----------------------------------------------------------------------
    function FakeSystemTimes alias "FakeSystemTimes" (byref I as filetime, _
                                                      byref K as filetime, _
                                                      byref U as filetime) _
                                                              export as long
      'will have other code to populate the 3 FileTime UDTs
    
      msgbox "Made it to Fake"
    end function
    '-----------------------------------------------------------------------
    function pbmain()
      local hLib as dword 'handle for Kernel32.dll
     
    
      PBFormsInitComCtls (%ICC_WIN95_CLASSES or %ICC_DATE_CLASSES or _
        %ICC_INTERNET_CLASSES)
     
      hLib = LoadLibrary ("Kernel32.dll")
      if hLib then '
        'rem next line to force error
        'gpGetSystemTimes = GetProcAddress(hLib, "GetSystemTimes")
        if gpGetSystemTimes = 0 then 'error, not XP SP1 or later
        
          gpGetSystemTimes = GetProcAddress(GetModuleHandleDY(0), _
                                            "FakeSystemTimes")
          gpGetTickCount = GetProcAddress(hLib, "GetTickCount") 'used in fake
          'msgbox str$(GetModuleHandle(0))
          'msgbox str$(gpGetSystemTimes)
          'exit function
        end if
      else 'error, can't load Kernel32.dll (unlikely, but bullet proof)
        function = -2
        exit function
      end if
    
      ShowDIALOG1 %HWND_DESKTOP
      FreeLibrary(hLib)
    end function
    Cheers,
    Last edited by Dale Yarker; 12 Apr 2008, 03:52 AM. Reason: no horizontal scroll bar in code box
    Dale

  • #2
    Looking in the PSDK I found GetModuleHandle, which when passed a nul pointer gives handle to the module it is called from. Passing a zero gave String expected error when compiled. Passing $NUL returned a zero handle.
    When a parameter is declared as asciiz, passing an empty string has the same effect that passing a null pointer.

    So instead of declaring an alternate function and using

    Code:
          gpGetSystemTimes = GetProcAddress(GetModuleHandleDY(0), "FakeSystemTimes")
    you can use

    Code:
          gpGetSystemTimes = GetProcAddress(GetModuleHandle(""), "FakeSystemTimes")
    or you can use BYVAL to override parameter checking:

    Code:
          gpGetSystemTimes = GetProcAddress(GetModuleHandle(BYVAL 0), "FakeSystemTimes")
    Forum: http://www.jose.it-berater.org/smfforum/index.php

    Comment


    • #3
      Thanks. Both ("") and (byval 0) work like you said.

      With PSDK saying pointer and Win32API.inc saying ASCIIZ, it didn't occur to me to try (""). Somehow it got into my head that that would only clear a dynamic string.

      Just looked in help for CALL and (byval 0) is just short of half way down.

      ----------------------------------------------------

      Any idea on Win32API updates? I know I've seen a couple important corrections posted here since early 2005. And Microsoft must have changed or added SOMETHING in 3 years!

      Cheers,
      Dale

      Comment


      • #4
        When a parameter is declared as asciiz, passing an empty string has the same effect that passing a null pointer.
        Passing a LITERAL empty string passes BYVAL %NULL; passing an ASCIIZ variable which happens to be currently null passes the address of that variable.

        THIS passes what you want to any function with an ASCIIZ parameter:

        Code:
        MACRO bvaz(asciizString) = BYVAL IIF(lstrlen(asciizString), VARPTR(AsCIIZString), %NULL)
        
        
        ...
           LOCAL sz AS ASCIIZ * something 
        
           sz = "anything" or  sz="" [null] 
           CALL FunctionName  (bvaz(sz))
        MCM
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment

        Working...
        X