Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

.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



  • Prev by Date: optimizing
  • Next by Date: Re: listdensityplot, black box?
  • Previous by thread: Re: optimizing
  • Next by thread: Re: Fractals and Time Series Analysis by R.J. Korsan appearing in