读书人

使用c dllfunction pointer的,该

发布时间: 2012-02-12 17:16:33 作者: rapoo

使用c dllfunction pointer的
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 vba

Declare 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


[解决办法]
帮顶了。
[解决办法]

VB code
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 调用约定,你就能成功。否则,就算返回了正确的计算结果程序也会崩溃一下,那时没法解决的。 

读书人网 >VB

热点推荐