Hi,
I have been having some problems trying to write a simple C++ DLL for use with VB in Microsoft Excel 2003. For some reason VB is not calling the DLL function correctly - can anyone help me with this?
For reference, this is my C++ code, which compiles just fine (I am using Code::Blocks, and the example is taken from "Financial applications using Excel add-in development in C/C++", 2nd edn, by Steve Dalton):
Main.h:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
#define DLL_EXPORT __declspec(dllexport)
extern "C"
{
double DLL_EXPORT WINAPI get_system_time_C(long trigger);
}
#endif // __MAIN_H__
Main.cpp:
#include "main.h"
#include <windows.h>
#include <time.h>
#define SECS_PER_DAY (24 * 60 * 60)
//==============================================================
// Returns the time of day rounded down to the nearest second as
// number of seconds since the start of day.
//==============================================================
long current_system_time(void)
{
time_t time_t_T;
struct tm tm_T;
time(&time_t_T);
tm_T = *localtime(&time_t_T);
return tm_T.tm_sec + 60 * (tm_T.tm_min + 60 * tm_T.tm_hour);
}
//==============================================================
// Returns the time of day rounded down to the nearest second as a
// fraction of 1 day, i.e. compatible with Excel time formatting.
//
// Wraps the function long current_system_time(void) providing a
// trigger for Excel using the standard calling convention for
// Win32 DLLs.
//==============================================================
double DLL_EXPORT WINAPI get_system_time_C(long trigger)
{
return current_system_time() / (double)SECS_PER_DAY;
}
The DLL is saved as MDA.dll, and this is the associated VB script, which is supposed to load the DLL and call the function:
Option Explicit
Public Declare Function get_system_time_C Lib "C:\Documents and Settings\srahman\My Documents\Projects\C++ Projects\MDA\bin\Debug\MDA.dll" (ByVal trigger As Long) As Double
Function Get_C_System_Time(trigger As Double) As Double
Get_C_System_Time = get_system_time_C(0)
End Function
When I try to use the formula =Get_C_Sytem_Time(0) on a spreadsheet, it enters the VB function, but does not call the C++ function in the DLL - instead it returns a #VALUE! error.
I am not sure (i) whether VB is unable to find the DLL, (ii) if the functions have not been exported correctly and are therefore not visible (iii) if I have made some other coding/compiler settings error.
Can anyone possibly help with this? If I can get this working, then I will have confidence to go ahead and build something more complex.
Thanks very much in advance,
Sabbir.
Bookmarks