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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With