Author Topic: Excel and dll compiled in Code:blocks  (Read 11659 times)

Koryf21

  • Guest
Excel and dll compiled in Code:blocks
« on: October 04, 2007, 03:14:01 pm »
Hello,

I am new user to Code:Blocks and I woud like to know how it is possible to compile a dll in Code:Blocks so it can be imported in Excel (2003) via VB 6.0 ? I would like to avoid in future to use Visual Studio C++...

Thank you in advance !

Cheers,

Patrick

I tried this but Excel doesn't seem to find it.

#include <windows.h>

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT
#endif

// a sample exported function
int DLL_EXPORT SomeFunction(const double somedouble)
{

    return 1;
}

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

Offline alchemist

  • Multiple posting newcomer
  • *
  • Posts: 38
    • My Blog
Re: Excel and dll compiled in Code:blocks
« Reply #1 on: October 04, 2007, 03:45:20 pm »
Hello,

You have then to declare the exported functions within VBA (using the Declare statement). See the VB help to have the right syntax.

EDIT:
Code
Declare Function SomeFunction lib "MyDLL.dll" Alias "SomeFunction" (ByVal SomeDouble As Double) As Integer
« Last Edit: October 04, 2007, 03:47:25 pm by alchemist »
Kind Regards,
Xavier Miller.
http://xaviermiller.be

Koryf21

  • Guest
Re: Excel and dll compiled in Code:blocks
« Reply #2 on: October 04, 2007, 04:00:21 pm »
Hello again,

Thank you for the answer  :D but still I get the sad "#VALUE" from excel  :(.

I did the following wrapping of the Dll call in vba:

Declare Function SomeFunction Lib "Sample.dll" (ByVal SomeDouble As Double) As Integer

Function testsample()

    Dim ld_int As Integer
    Dim ld_double As Double
   
    ld_double = 0
   
    ld_int = SomeFunction(ld_double)

    testsample = ld_int

End Function

I believe I miss something when I compile the dll ???

Thanks again,

Patrick

Mahobi

  • Guest
Re: Excel and dll compiled in Code:blocks
« Reply #3 on: October 04, 2007, 04:56:54 pm »
Hello,

maybe this could help you:
http://www.mingw.org/MinGWiki/index.php/VB-MinGW-DLL

VB and VBA have some restrictions usung standard dlls.

Koryf21

  • Guest
Re: Excel and dll compiled in Code:blocks
« Reply #4 on: October 04, 2007, 05:44:41 pm »
Thanks for the comment  :D.

 I still have the same "#VALUE!" in Excel.

Did I miss the linker flags ? I put "--add-stdcall-alias" in the "other linker options" field of "Code::Blocks" ? Is that correct ? Is there a way to check at least that the dll is loaded ?

I modified the sample code according to the previous reply to:

#include <windows.h>

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT
#endif

// a sample exported function
DLL_EXPORT int __stdcall SomeFunction(const double somedouble)
{
    //MessageBoxA(0, somedouble, "DLL Message", MB_OK | MB_ICONINFORMATION);

    return 1;
}

DLL_EXPORT BOOL __stdcall 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
}

Mahobi

  • Guest
Re: Excel and dll compiled in Code:blocks
« Reply #5 on: October 04, 2007, 05:54:57 pm »
You could try to add "-Wl,--add-stdcall-alias" to your linker flags.

Furthermore you could check the exported functions of the DLL with Dependency Walker.

Koryf21

  • Guest
Re: Excel and dll compiled in Code:blocks
« Reply #6 on: October 04, 2007, 06:11:18 pm »
I changed the linker options but still...  :(

I didn't find the dependency walker (my first day in Code::Blocks, sorry) but i asked Code::Blocks to create the .def file and I looked at it and saw that the "SomeFunction" is referenced inside.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7590
    • My Best Post
Re: Excel and dll compiled in Code:blocks
« Reply #7 on: October 04, 2007, 07:08:57 pm »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline rjmyst3

  • Multiple posting newcomer
  • *
  • Posts: 117
    • wxFormBuilder
Re: Excel and dll compiled in Code:blocks
« Reply #8 on: October 04, 2007, 09:15:57 pm »
With Dependency Walker, you can do Profile->Start Profiling on Excel.exe to see what problems Excel encounters when it loads your dll, or when it calls functions from your dll.

Koryf21

  • Guest
Re: Excel and dll compiled in Code:blocks
« Reply #9 on: October 09, 2007, 03:29:22 pm »
Hello,

Sorry to be late to be back to this post; I had to switch on another task. I came back to my question only today. I tried dependencywalker and saw Excel failed to load other DLLs, I suppressed these links and then Excel could load my dll and the above code worked.  :D :D

I am not expert on MS products and it is the only explanation that I can find now. Anyway thanks to those who answered me.

Now I have a much more basic question about code::blocks. When I choose to build a project that is a DLL, code::blocks only offers to compile in C; what should I do to make it compile in C++ ( I used before other C++ IDE on different platforms and here I made the standard installation of Code::Blocks).

It sounds good, I might be able to use something else than MS products...

Thanks again  :D

Patrick