+ Reply to Thread
Results 1 to 7 of 7

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

  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:

    Please Login or Register  to view this content.
    Main.cpp:

    Please Login or Register  to view this content.

    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:

    Please Login or Register  to view this content.

    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:

    Please Login or Register  to view this content.
    and adding a DllMain function as follows:

    In main.h:

    Please Login or Register  to view this content.
    and in main.cpp:

    Please Login or Register  to view this content.

  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.

  7. #7
    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 have been following "Financial applications using Excel add-in development in C/C++", 2nd edn, by Steve Dalton. However it generally assumes that MS Visual Studio is being used for the C++ DLL development, whereas I am currently using the Code::Blocks IDE with GNU GCC/MinGW.

    The main difficulty was exposing the functions with names unmangled for access by VB, and it seems that the solution is compiler-dependent. I found the necessary compiler/linker options for GCC and MinGW here:

    http://www.mingw.org/wiki/Visual_Basic_DLL

    I need to call the code from VB for my particular application, though I do appreciate the XLL tip.

    Best wishes,

    Sabbir.

+ 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