2009年12月25日 星期五

VB Techniques

VBA call 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)
{ /*some codes here */}

in vba

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

Function vbsquare(ByVal x As single) As Single
vbsquare = x * x
End Function

sub test
dim a as single
a=trapzd(addressof vbsquare,0,1,5)
end sub







=======================================================================
How to do pointers in Visual Basic
http://www.codeproject.com/KB/vbscript/how_to_do_pointers_in_visual_basic.aspx?df=100&forumid=1232&exp=0&fr=26&select=5094
Pointers in Visual Basic using Undocumented Functions
http://www.codeproject.com/KB/vbscript/UB_Pointers_In_VB.aspx

沒有留言:

張貼留言