如何使用gcc编译器/Mingw for visualbasic创建dll?
如何使用gcc编译器/Mingw for visual basic创建dll?VB(所有版本)更喜欢pascal调用约定如何使用gcc编译器/Mingw for visualbasic创建dll?,dll,gcc,mingw,Dll,Gcc,Mingw,如何使用gcc编译器/Mingw for visual basic创建dll?VB(所有版本)更喜欢pascal调用约定 使用WINAPI声明外部函数并将它们导出到.def文件中。这对您可能有用。以下是我如何让MinGW构建一个DLL以在Excel 2003 VBA中使用的 fooBar.cpp int __stdcall Foo(int x) { return x * x; } double __stdcall Bar(double x) { r
使用WINAPI声明外部函数并将它们导出到.def文件中。这对您可能有用。以下是我如何让MinGW构建一个DLL以在Excel 2003 VBA中使用的 fooBar.cpp
int __stdcall Foo(int x)
{
return x * x;
}
double __stdcall Bar(double x)
{
return x * x;
}
#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
1) 启动MinGW shell并创建一个名为fooBar
的目录。
关闭Excel工作簿(如果打开)
2) 编译并生成.def文件-注意:此dll将无法工作,因为它的符号已损坏。
gcc -shared -o fooBar.dll fooBar.cpp -Wl,--output-def,fooBar.def,--out-implib,libfooBardll.a
生成的fooBar.def将类似于:
EXPORTS
_Z3Bard@8 @1
_Z3Fooi@4 @2
EXPORTS
_Z3Bard@8 @1
_Z3Fooi@4 @2
Bar = _Z3Bard@8
Foo = _Z3Fooi@4
3) 通过为生成的符号添加干净的符号别名,修改生成的fooBar.def文件。fooBar.def现在应该类似于:
EXPORTS
_Z3Bard@8 @1
_Z3Fooi@4 @2
EXPORTS
_Z3Bard@8 @1
_Z3Fooi@4 @2
Bar = _Z3Bard@8
Foo = _Z3Fooi@4
4) 重新启动(修改后的fooBar.def除外)
5) 使用.def文件编译生成的符号,并使用干净的符号别名
gcc -shared -o fooBar.dll fooBar.cpp fooBar.def -Wl,--out-implib,libfooBar_dll.a
6) 打开Excel并添加以下VBA代码(确保使用正确的路径,怀疑其中是否有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) 如果要从Excel工作簿调用函数,请在单元格类型
=Foo(5)
或=Bar(5)
中首先调用以下函数:
所有导出的函数都应该有C链接。
DLL中抛出的所有C++异常都应该在DLL中捕获
为什么??因为Windows上没有标准的C++ ABI。
在应该导出的函数上也使用_declspec(dllexport)
现在,如何在不需要任何DEF文件的情况下创建DLL
fooBar.cpp
int __stdcall Foo(int x)
{
return x * x;
}
double __stdcall Bar(double x)
{
return x * x;
}
#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
使用编译
gcc fooBar.cpp -shared -Wl,--kill-at -o fooBar.dll
现在,您应该能够调用square_xxx作为mmorris应答。但是他的解决方案可能有效。什么Visual Basic版本?您希望在VB中使用什么技术来调用dll:COM或API调用/PInvoke?这已编译,但在Excel中不起作用。mmorris的建议是有效的,但这看起来是一个更好的实现。可能是mingw gcc版本的问题?这种方法我应该仔细考虑一下,因为它似乎是一种更优雅的解决方案。谢谢。@RJB我还没有试过自己,但excel可能不识别“\ux”