How do I create a dll using gcc compiler / Mingw for visual basic?

How do I create a dll using gcc compiler / Mingw for visual basic?

+2


a source to share


4 answers


Below is the information on how I managed to get MinGW to create a DLL that will be used in Excel 2003 VBA.

fooBar.cpp

int __stdcall Foo(int x)   
{   
    return x * x;   
}

double __stdcall Bar(double x)   
{   
    return x * x;   
}

      

1) Start MinGW shell and create a directory named fooBar

. Close the Excel workbook (if open).

mkdir -p fooBar
cd fooBar
rm *.a *.dll *.def 

      

2) Compile and generate a .def file - Note: this DLL will not work because it has garbled symbols.

gcc -shared -o fooBar.dll fooBar.cpp -Wl,--output-def,fooBar.def,--out-implib,libfooBardll.a

      

The generated fooBar.def will look something like this:

EXPORTS
    _Z3Bard@8 @1
    _Z3Fooi@4 @2

      



3) Modify the generated fooBar.def file to add pure symbol aliases for the generated symbols. fooBar.def should now look something like this:

EXPORTS
    _Z3Bard@8 @1
    _Z3Fooi@4 @2
    Bar = _Z3Bard@8
    Foo = _Z3Fooi@4

      

4) Cleaup again (except the modified fooBar.def)

rm *.a *.dll 

      

5) Compile the .def file with pure symbolic aliases for the generated symbols.

gcc -shared -o fooBar.dll fooBar.cpp fooBar.def -Wl,--out-implib,libfooBar_dll.a

      

6) Open Excel and add the following VBA code (be sure to use the correct path, doubt it will include mmorris) :

Private Declare Function Foo Lib _
    "C:\MinGW\msys\1.0\home\mmorris\fooBar\fooBar.dll" _
    (ByVal x As Long) As Long

Private Declare Function Bar Lib _
    "C:\MinGW\msys\1.0\home\mmorris\fooBar\fooBar.dll" _
    (ByVal x As Double) As Double

      

7) If you want to call functions from Excel workbook, in cell type =Foo(5)

or=Bar(5)

+7


a source


First, a few DLL-iquette:

All exported functions must have a C link. All C ++ exceptions thrown inside a DLL must be found inside the dll.

Why? Since there is no standard C ++ ABI on Windows.

Also use __declspec (dllexport) for functions to be exported

Now how to make a DLL without the need for a DEF file



fooBar.cpp

#ifdef __cplusplus
extern "C"
{
#endif
    __declspec(dllexport) int __stdcall square_int(int x) //I prefer manual mangling in this case to support static polymorphism
    {   
        return x * x;   
    }

    __declspec(dllexport) double __stdcall square_double(double x)   
    {   
        return x * x;   
    }
#ifdef __cplusplus
}
#endif

      

compile with

gcc fooBar.cpp -shared -Wl,--kill-at -o fooBar.dll

      

You should now be able to call square_xxx as in mmorris. His solution probably works.

+2


a source


This: http://www.mingw.org/wiki/DLL may be of use to you.

+1


a source


VB (all versions) prefers the pascal calling convention.

Declare your external functions with WINAPI and export them to your .def file.

0


a source







All Articles