+ Reply to Thread
Results 1 to 7 of 7

Need help writing a C++ DLL for VB / Excel 2003 using Code::Blocks

Hybrid View

  1. #1
    Registered User
    Join Date
    06-19-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    3

    Need help writing a C++ DLL for VB / Excel 2003 using Code::Blocks

    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.
    Last edited by sarahman; 06-19-2009 at 06:33 PM.

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Need help writing a C++ DLL for VB / Excel 2003 using Code::Blocks

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Registered User
    Join Date
    06-19-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Need help writing a C++ DLL for VB / Excel 2003 using Code::Blocks

    Hi,

    I managed to solve the problem by adding the following options in Project build options-->Linker settings-->Other linker options:

    -Wl,--add-stdcall-alias
    and adding a DllMain function as follows:

    In main.h:

    extern "C"
    {
        DLL_EXPORT double WINAPI get_system_time_C(long trigger);
        DLL_EXPORT BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
    }
    and in main.cpp:

    DLL_EXPORT BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
    {
        switch (fdwReason)
        {
            case DLL_PROCESS_ATTACH:
                // attach to process
                // return FALSE to fail DLL load
                break;
    
            case DLL_PROCESS_DETACH:
                // detach from process
                break;
    
            case DLL_THREAD_ATTACH:
                // attach to thread
                break;
    
            case DLL_THREAD_DETACH:
                // detach from thread
                break;
        }
        return TRUE; // succesful
    }

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Need help writing a C++ DLL for VB / Excel 2003 using Code::Blocks

    Your post does not comply with Rule 9 of our Forum RULES. If you solve a problem yourself before anyone else has responded, please take a moment to describe your solution, chances are some other member will benefit. And please never edit a thread in which someone else has responded.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Need help writing a C++ DLL for VB / Excel 2003 using Code::Blocks

    sarahman thanks for posting resolution - can I ask if you found any good resources on this topic ? Compiling add-ins in C is something I'd love to learn to do but I suspect it's beyond me...

  6. #6
    Registered User
    Join Date
    06-20-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: Need help writing a C++ DLL for VB / Excel 2003 using Code::Blocks

    It is possible to write xlls for Excel directly in C/C++ using Code::Blocks.
    If your goal is to expose your C/C++ functions to Excel and you're using VBA for boiler plate code then generally using xlls is a more preferable approach.

    You can see a demo of creating an xll with Code::Blocks here:

    Creating a C++ XLL with Code::Blocks & MinGW

    Moreover if you're using Visual Studio :

    Creating a C++ XLL with Visual Studio

    Creating a C# XLL with Visual Studio

    Creating a hybrid C++/C# XLL with
    Visual Studio (Professional)


    Debugging a C# XLL with Visual C# Express

    The software demonstrated in the clips can be downloaded here xlw-4.0.0b0 and its completely free.

    XLW also supports the creation of xlls in VB.NET

    A's Dad
    Last edited by AlexesDad; 06-20-2009 at 07:29 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1