VBA Calling C++
Create a new project
language - C++, platform - Windows, project - Library
Dynamic Link Library (DLL)
delete all the source files
delete all the header files
Project Properties
change your drop-downs to "ALL" for both
Configuration Properties > General
check - C++ Langauge Standard = Default (ISO C++ 14 Standard)
Configuration Properties > Debugging
check - Command = C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE
Configuration Properties > C/C++ > All Options
check Precompiled Header = Not Using Precompiled Headers
check Precompiled Header File = blank
Configuration Properties > Linker > All Options
check - Module Definition File = blank
GetTime.cpp
Add a source file.
#include <windows.h>
#include <time.h>
#define SECS_PER_DAY (24 * 60 * 60)
long current_system_time(void)
{
time_t time_t_T;
time(&time_t_T);
struct tm tm_T;
time_t time_t_T2 = time(0);
localtime_s(&tm_T, &time_t_T2);
return tm_T.tm_sec + 60 * (tm_T.tm_min + 60 * tm_T.tm_hour);
}
double __stdcall get_system_time_C(long trigger)
{
return current_system_time() / (double)SECS_PER_DAY;
}
Build the project
SS
GetTime.def
Add a definition file.
Project > Add > New Item
under Visual C++, select "Code"
SS
Select "Module Definition File"
LIBRARY
EXPORTS
get_system_time_C
The module definition file must be explicitly added to the project settings
Configuration Properties > Linker > Input
Module Definition File = GetTime.def
Build the project
SS
check and change the "Solution Platform" drop-down to x64
SS
Create the Workbook
Save the file as GetTime.xlsm
Open the VBA Editor and add a code module
Declare PtrSafe Function get_system_time_C Lib _
"C:\Users\*username"\source\repos\exceldll\x64\Debug\exceldll.dll" _
(ByVal trigger As Long) As Double
Public Function Get_C_System_Time(trigger As Double) As Double
Get_C_System_Time = get_system_time_C(0)
End Function
Public Function Get_VBA_Time(trigger As Double) As Double
Get_VBA_Time = Now
End Function
Enter the following information into cells "B1, B2, B3"
Start Debugging
SS
Open the excel file
press F9
© 2025 Better Solutions Limited. All Rights Reserved. © 2025 Better Solutions Limited TopPrevNext