1、 vba 调用 C++ Dll
1.1 code blocks 建立DLL
1.1.1 将 def 文件拷贝到 项目根目录下,改为:
EXPORTS
SomeFunction
1.1.2 ".h": DLL_EXPORT 改为 __stdcall, 同时:
//#ifdef BUILD_DLL
// #define DLL_EXPORT __declspec(dllexport)
//#else
// #define DLL_EXPORT __declspec(dllimport)
//#endif
void __stdcall SomeFunction(double *outArr,double *inArr, int outLen=12, int inLen=24);
1.1.3 “.cpp”: DLL_EXPORT 改为 __stdcall
1.2 VBA
1.2.1 将 DLL 拷贝至 excel 同目录
1.2.2 ChDrive (ThisWorkbook.Path)
ChDir (ThisWorkbook.Path)
1.2.3 Public Declare Sub SomeFunction Lib "SomeFunction.dll" (ByRef outArr As Double, _
ByRef inArr As Double, _
Optional ByVal outLen As Long = 12, _
Optional ByVal inLen As Long = 24)
2. R 调用 DLL
2.1 code block, ".cpp"(全部以指针传送数据)
void DLL_EXPORT SomeFunction(double* outArr, int* outLen)
{
int len=*outLen;
for(int j=0; j<len; j++)
{
outArr[j] = outArr[j]*2;
}
}
2.2 R:
dyn.load("C:\\...\\Project\\test111R\\bin\\Debug\\test111R.dll")
.C("SomeFunction",as.double(c(1:12)),as.integer(c(12)))