Starting with DLL function

  • Hi !
    I've never used a DLL function in my syntax yet but would like to know how to:


    1. find out list of functions / subs available in DLL
    2. corretly refer to function (by its name, lib name, arguments, passing args, Alias etc)


    I have installed a special add-in and noticed in one file there is a "Lib function" referred to .xll file,
    which is part of the installation package.
    But I wasn't able to find out the way how function is built


    Can you advise please ?

  • Re: Starting with DLL function


    A dll (Dynamic Link Library) is essentially a list of pre-written code that's available to use with other applications.


    Quote

    1. find out list of functions / subs available in DLL


    Basically, infinite. There are thousands, if not millions of available libraries out there than can be used in a whole array of languages - not just VBA. It entirely depends on what you want to do that can't be done natively in VBA.


    Quote

    2. corretly refer to function (by its name, lib name, arguments, passing args, Alias etc)


    The essential parts of declaring an external function/sub are as follows:


    [noparse][Public | Private] Declare [Function | Sub] name Lib "libname" [Alias "aliasname"] [([arglist])] [As type][/noparse] (ref: MSDN)


    So you need:


    - The Scope identifier (Public/Private)
    - The 'Declare' keyword
    - The type (Sub/Function)
    - The name (ideally, the same name as the original given in the dll)
    - The library (where the dll is hosted)
    - The Alias (if the name you have given is different from that in the dll, give the actual dll name here)
    - The arguments (if required)
    - The return value (if it's a function)


    Example:


    Code
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    
    Public Declare Function GetClassName Lib "USER32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long


    It actually gets a lot more complex, because depending on the type of office installation you are using you have to declare these functions/subs as "Pointer Safe" using the "PtrSafe" keyword and change data types for handles and pointers to a "LongPtr". You do this using something called conditional compilation, as the "LongPtr" data type isn't available on 32bit and will cause a compilation error.


    But that's another story for another time :)

  • Re: Starting with DLL function


    Hi S O,
    really thanks for a nice reference.
    I found some basic examples with common libraries like Kerner32, or User32.


    In one macro file I have some functions, e.g. one that connects and another that retrieves data from an OLAP cube,
    both referring to same library file with extension .XLL. (which is part of add-in installation)


    I was wondering, how to proceed, in order to find out about any other functions that I could use in conjunction with that add-in.
    Either to find it under Tools / References and use object browser in some way, or is there any sort of a "reader" to do that ?


    As for the data types and VBA version backwards compatibility, let's suppose these would be secondary in this case.


    Thanks!

  • Re: Starting with DLL function


    Basically, you can't.


    An XLL file is a compiled library (probably written in C++) but because the code is compiled it is unreadable in it's current form. The only way really is contact the developer/author of the file and ask them - have you tried googling the name of the file in case it's a commonly used library?

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!