简体   繁体   中英

Use DLL in excel-vba

i would love to know how to declare and use a dll in vba. I have a code written in C and i would like to use some data from an excel sheet to test my c-code. How is it possible? Many thanks in advance

You have to declare the function in your VBA Module: For a Command:

[Public | Private] Declare Sub name Lib "libname" [Alias "aliasname"] [([arglist])]

For a Function Call:

[Public | Private] Declare Function name Lib "libname" [Alias "aliasname"] [([arglist])] [As type]

A few examples form one of my Apps:

Public Declare Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Declare Function WM_apiGetDeviceCaps Lib "gdi32" Alias _
    "GetDeviceCaps" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Declare Function WM_apiGetDesktopWindow Lib "user32" Alias _
    "GetDesktopWindow" () As Long
Declare Function WM_apiGetDC Lib "user32" Alias _
    "GetDC" (ByVal hwnd As Long) As Long
Declare Function WM_apiReleaseDC Lib "user32" Alias _
    "ReleaseDC" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Declare Function WM_apiGetSystemMetrics Lib "user32" Alias _
    "GetSystemMetrics" (ByVal nIndex As Long) As Long
Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" _
    (ByVal lpBuffer As String, nSize As Long) As Long

Edited to Add PtrSafe information: See MSDN PtrSafe Language Reference

When running in 64-bit versions of Office Declare statements must include the PtrSafe keyword. The PtrSafe keyword asserts that a Declare statement is safe to run in 64-bit development environments. Adding the PtrSafe keyword to a Declare statement only signifies the Declare statement explicitly targets 64-bits, all data types within the statement that need to store 64-bits (including return values and parameters) must still be modified to hold 64-bit quantities using eitherLongLong for 64-bit integrals orLongPtr for pointers and handles.

Within the top of your module contains the declarations section. This is where you will typically find your Option Statement(s) if they exist.

It is in this same area you would declare your DLL functions, which you would do outside any procedure.

In the days before the release of 64 bit MS Office, you would simply declare your DLL procedure with the following parameters:

[Bracket Statements] are optional
Function is interchangeable with Sub - You would not use [As Type] on a sub

[Public|Private] Declare Function "PublicName" Lib LibName _ 
    [Alias "AliasName"] [(Arguments)] [As Type].

Here is an example of the sleep API:

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

However, MS released 64-bit versions of their software, and this came with a new version of VBA, known as VBA7 . The problem is that 64-bit OS and software use different memory pointers and handles, therefore it was important that VBA knows that the declare statement is safe to run in a 64-bit environment. In order for you to let VBA know this, you would need to modify your Function's data types to generally be of type LongPtr for memory pointers as opposed to Long (or quantities should be of type LongLong .

You would inform VBA that your declaration is correct using the following statement:

[Private|Public] Declare PtrSafe Function...

Example of the same Sleep API declared as PtrSafe:

Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)

What if you are unsure if the end user is using the new VBA7? Fortunately for you, If...Then statements are available in your declarations if prefixed by # . You can make your code work for any VBA environment by using the above declaration:

#If VBA7 Then
    Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM