ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Using C++ Dll library (https://www.excelbanter.com/excel-programming/315022-using-c-dll-library.html)

No Name

Using C++ Dll library
 
I have a C++ DLL libarary (not COM) that I want to use in
VBA. Is it possible to use this library in VBA. If so,
how?

Jim Rech

Using C++ Dll library
 
I don't think it matters terribly what language a DLL was created in. As
far as I know you need to know what functions a DLL exports, what parameters
it expects, what it returns and then you need to "declare" these things.

Here's an example of using a Windows DLL (advapi32.dll):

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Sub ShowUserName()
MsgBox UserName
End Sub

Function UserName() As String
Dim Buffer As String * 256
Dim BuffLen As Long
BuffLen = 256
If GetUserName(Buffer, BuffLen) Then _
UserName = Left(Buffer, BuffLen - 1)
End Function

--
Jim Rech
Excel MVP
wrote in message
...
|I have a C++ DLL libarary (not COM) that I want to use in
| VBA. Is it possible to use this library in VBA. If so,
| how?



Chip Pearson

Using C++ Dll library
 
Its worth noting that the C++ DLL function(s) must have been
compiled with the __stdcall option. Otherwise, you can't call
them.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Jim Rech" wrote in message
...
I don't think it matters terribly what language a DLL was
created in. As
far as I know you need to know what functions a DLL exports,
what parameters
it expects, what it returns and then you need to "declare"
these things.

Here's an example of using a Windows DLL (advapi32.dll):

Declare Function GetUserName Lib "advapi32.dll" Alias
"GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Sub ShowUserName()
MsgBox UserName
End Sub

Function UserName() As String
Dim Buffer As String * 256
Dim BuffLen As Long
BuffLen = 256
If GetUserName(Buffer, BuffLen) Then _
UserName = Left(Buffer, BuffLen - 1)
End Function

--
Jim Rech
Excel MVP
wrote in message
...
|I have a C++ DLL libarary (not COM) that I want to use in
| VBA. Is it possible to use this library in VBA. If so,
| how?






All times are GMT +1. The time now is 07:48 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com