in test.dll (比方說梯形法求積分 a,b上下極限. n 為切的塊數).h 檔
extern "C" __declspec(  dllexport ) float __stdcall trapzd(float (*func)(float), float a, float b, int n);.cpp 檔
float __stdcall trapzd(float (*func)(float), float a, float b, int n)
{
float x,tnm,sum,del;
static float s;
int it,j; if (n == 1) {
return (s=0.5*(b-a)*((*func)(a)+(*func)(b)));
} else {
for (it=1,j=1;j<n-1;j++) it <<= 1;
tnm=it;
del=(b-a)/tnm;
x=a+0.5*del;
for (sum=0.0,j=1;j<=it;j++,x+=del) sum += (*func)(x);
s=0.5*(s+(b-a)*sum/tnm);
return s;
}
}
==============================================
in vbaDeclare Function trapzd Lib "test.dll" Alias "trapzd@16" (ByVal fn As Long, ByVal a As Double, ByVal b As Double, ByVal n As Integer) As Double如何呼叫 trapzd, 假設想積 f(x)=x^2, 從0 到1, n=5
Function vbsquare(ByVal x As Double) As Double
vbsquare = x * x
End Function

解决方案 »

  1.   

    Declare Function trapzd Lib "test.dll" Alias "trapzd@16" (ByVal fn As Long, ByVal a As  Single , ByVal b As  Single , ByVal n As Integer) As  Single 'double 的不行Function vbsquare(ByVal x As signle) As  Single 'double 的不行
     
    vbsquare = x * x 
    End Function 
    sub test
     dim a as single
     a=trapzd(addressof vbsquare,0,1,5)
    end sub
    ''但这样做是有条件的,如果 test.dll 中的 float (*func)(float) 用的是 stdcall 调用约定,你就能成功。否则,就算返回了正确的计算结果程序也会崩溃一下,那时没法解决的。