Announcement

Collapse
No announcement yet.

Kernel32 calls from vba

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

  • Kernel32 calls from vba

    I've been able to use PBDLL to set up dll's returning values from functions I've written, called from Excel97/VBA. But every time I try to put a regular Kernel32 call in a PBDLL it compiles OK but the call gives a 'File Not found' from Excel. Tests seem to indicate that it's Kernel32 that the Excel call isn't finding. For example, I have the following in my BAS module:

    TYPE SYSTEMTIME
    wYear AS INTEGER
    wMonth AS INTEGER
    wDayOfWeek AS INTEGER
    wDay AS INTEGER
    wHour AS INTEGER
    wMinute AS INTEGER
    wSecond AS INTEGER
    wMilliseconds AS INTEGER
    END TYPE


    DECLARE SUB GetSystemTime LIB "KERNEL32.DLL" (lpsystemtime AS SYSTEMTIME)


    FUNCTION TRYACALL()EXPORT AS INTEGER
    DIM Greenwich AS SYSTEMTIME

    GetSystemTime Greenwich
    TRYACALL = Greenwich.wMinute

    END FUNCTION

    And the following call from Excel(the declare is correct)

    Sub lasttry()
    Dim retint As Integer
    retint = TRYACALL
    MsgBox CStr(retint)
    End Sub

    This type of call works on my other PBDLL procedures.


    I have tried, (I believe) every conceivable capitalization scheme for the DLLs and functions inside them. Dependency walker indicates that the compiled DLL is OK. The DLL is in winnt/system32. Qualified path to it doesn't work either.

    So what simple thing am I missing?

    TIA-

    William Arnold



  • #2
    First, VB and VBA often misreport a DLL load failure error if the DLL was not able to be loaded for any reason. In this case the likely cause is miscapitalization of the Kernal32 function you wish to use - the real error is likely to be a "Missing Export" error.

    The solution is to add an ALIAS clause to your API declaration. By default, PB/DLL (and PB/CC) capitalize function and sub names that are exported and imported. Without an ALIAS clause, it does not matter how you capitalize the function name in your declaration, as it will be converted to uppercase during compilation.

    The easy solution is to just utilize the WIN32API.INC file in your PB/DLL code, rather than rewriting API declarations from scratch.

    To cut to the chase, your code:
    DECLARE SUB GetSystemTime LIB "KERNEL32.DLL" (lpsystemtime AS SYSTEMTIME)

    Would need to be rewritten as:
    DECLARE SUB GetSystemTime LIB "KERNEL32.DLL" ALIAS "GetSystemTime" (lpsystemtime AS SYSTEMTIME)

    or delete this line altogether, and add:
    #INCLUDE "WIN32API.INC"
    to the top of the DLL code, just below the #COMPILE DLL line.


    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      That's got it, Lance. Thanks-wa

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

      Comment

      Working...
      X