首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

使用c dll裡function pointer的問題,该如何解决

2012-02-12 
使用c dll裡function pointer的問題in test.dll (比方說梯形法求積分 a,b上下極限. n 為切的塊數).h 檔ext

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

热点排行