Error calling Fortran function from C# using

  • Thread starter Thread starter jacob ya
  • Start date Start date
J

jacob ya

Hi,

I'm working on a project that requires us to call some mathematical
functions that are in a Fortran dll. I'm trying to call the function
using P/Invoke, but I keep getting a null reference exception, anyone
have any ideas? Here's my code:

[DllImport(@"TrendFit.dll", EntryPoint="TrendFit")]
public static extern int trendfit(
int lNPt,
double dblX,
double dblY,
double dblSdY,
int lNCoeff,
double dblCoeff,
double dblV,
double dblVF);

private void button1_Click(object sender, System.EventArgs e)
{
int lNPt = 30;
double dblX = 38222.5664467593;
double dblY = 19.8469260358252;
double dblSdY = 1;
int lNCoeff = 2;
double dblCoeff = 0;
double dblV = 0;
double dblVF = 0;

int result = trendfit(lNPt, dblX, dblY, dblSdY, lNCoeff, dblCoeff,
dblV, dblVF);

// result should be 0
textBox1.Text = result.ToString();
}


Many thanks.
 
Maybe wrong calling convention? You can use a special Attribute to specify
it.
Or maybe you have to call a special Init routine of this library to set some
internal variables.
A null reference exception from native code is a hint that a page fault
occured in the native code like a wuninitialized pointer or similar..
 
Please respond if that is solving your problem, because Iam interested if my
ideas were correct.
 
I tried all of the different calling conventions available and none of
them worked. I've also tried passing all of the parameters by reference
which is what I think I need to do because I have the source code from a
VB6 app that calls the same DLL and it passes all values ByRef. The VB6
app doesn't call any Init functions or anything like that, so I don't
believe that is the issue. I think that datatype mismatches between VB6
and VB.NET/C# could be an issue as well. I'll let you know when I get
it working.
 
I changed all my parameters to pass by reference and the call to the
function actually works, but the NullReferenceException is now being
thrown once the calling function terminates. This seems like a cleanup
issue to me. I'm going to keep investigating, but if anyone has
experienced this same problem or has a solution, your advice would be
much appreciated. Thanks.
 
Back
Top