.NET/Link Callback from a DLL
- To: mathgroup at smc.vnet.net
- Subject: [mg74851] .NET/Link Callback from a DLL
- From: "Asim" <maa48 at columbia.edu>
- Date: Sun, 8 Apr 2007 05:05:48 -0400 (EDT)
Hi
I am trying to use .NET/Link to call a C function (DLL) that takes a
callback function pointer as an argument. I tried to follow the
EnumWindows.nb example that is part of the .NET/Link documentation,
but am getting an erroneous result for my function.
The DLL that I am trying to call is for the C function
double fpt( double (* callfunc)(double , double), double a, double b)
{
double c= (* callfunc)(a, b);
return c;
}
I created a DLL for the above function using DevC++ on Windows XP, and
the associated dll file is named newtry.dll.
Here is what I do in Mathematica 5.2
In[2]:=
Needs["NETLink`"];
In[3]:=
delegateType=DefineNETDelegate["x", "double", {"double", "double"}]
Out[3]=
NETType[Wolfram.NETLink.DynamicDelegateNamespace.x,1]
In[4]:=
fpt=DefineDLLFunction["fpt", "newtry.dll", "
double", {delegateType, "double", "double"}]
Out[4]=
Function[Null,If[NETLink`DLL`Private`checkArgCount[fpt,{##1},
3],Wolfram`\
NETLink`DynamicDLLNamespace`DLLWrapper1`fpt[##1],$Failed],{HoldAll}]
In[5]:=
fptex[a_Real, b_Real]:=Module[{c}, c=a+b];
In[6]:=
callbackDelegate=NETNewDelegate[delegateType, fptex]
Out[6]=
=ABNETObject[Wolfram.NETLink.DynamicDelegateNamespace.x]=BB
In[7]:=
d=fpt[callbackDelegate, 1.0, 2.0]
Out[7]=
0=2E
In[8]:=
fptex[1.0, 2.0]
Out[8]=
3=2E
I
As you can see, the correct answer should be 3, as in Out[8]. But the
function fpt that uses the callback mechanism returns 0 as in Out 7,
instead of 3.
I would appreciate if someone can point out what I am doing wrong?
Asim Ansari