Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

DllImport, NullReferenceException

jacobya
Beginner
1,413 Views
I apologize if this is not the proper place to post this question, but I'm getting pretty desperate after 3 days of trying to get this to work. I'm trying to call a function in a Fortran dll (for which I don't have the source) from a C# application. I have a working copy of a VB6 application that calls this dll, but I can't get my C# version to work.
Here is the code I'm using:

[DllImport(@"TrendFit.dll", EntryPoint="TrendFit"

)]

public static extern int trendfit(ref int lNPt,ref double dblX,ref double dblY,ref double dblSdY,ref int lNCoeff,ref double dblCoeff,ref double dblV,ref double dblVF);

private void button1_Click(object sender, System.EventArgs e)

{

int lNPt = 30;

double dblX = 38222.5664467593;

double dblY = 6.99999999778811;

double dblSdY = 1;

int lNCoeff = 2;

double dblCoeff = 0;

double dblV = 0;

double dblVF = 0;

int result = 0;

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

}

The 'trendfit' function is called, but the dblCoeff, dblV and dblVF values get set to NaN and I get a NullReferenceException when the calling method completes. The code above is similar to how the VB6 app calls the trendfit function, except the VB6 app was using LONG where I'm using INT because VB6 long is 4 bytes and long in .NET is 8.

Anyone have any idea what I'm doing wrong? Many thanks for your help

0 Kudos
9 Replies
sabalan
New Contributor I
1,413 Views
Remove "ref"-s from your DLL declaration and call statement and try to run the code.
Sabalan.

Message Edited by Sabalan on 01-13-2005 03:43 AM

0 Kudos
jacobya
Beginner
1,413 Views
Hi Sabalan,
Thanks for your reply. I tried removing the "ref"s and it throws the NullReferenceException on the call to the the TrendFit function. The reason I put the "ref"s in there is because the VB6 app that works, passes all values ByRef. I even tried to get this to work from VB.NET instead of C# using the exact same syntax as the VB6 app but I still get the NRException. If I change the datatypes of the parameters around the parameters get altered differently, but I still get Nan in some results, that makes me think it has something to do with the size of the parameters that are being passed.
0 Kudos
Steven_L_Intel1
Employee
1,413 Views
In the DLL project, try setting the Libraries..Use Run-Time Libraries property to "Multi-threaded DLL" (not any of the Debug options.)
0 Kudos
jacobya
Beginner
1,413 Views
Unfortuneately, I don't have the source code for the DLL, so I can't change any of the properties. What I don't understand is why it would work in VB6 and not in .NET, would it be related to the datatypes? If I specify CallingConvention=CallingConvention.ThisCall, it doesn't throw a NullReferenceException (until I close the application), but doesn't modify any of the parameters.
0 Kudos
Steven_L_Intel1
Employee
1,413 Views
VB.NET seems to have some incompatibilities with the debug multithread DLLs - we have seen this in a handful of customer problems. Same is true for any of the other managed code environments.
0 Kudos
jacobya
Beginner
1,413 Views
I'm running out of time and getting pretty desperate for a solution, so I'm considering taking the working VB6 code and rewriting it as a COM object and then tapping the COM object from .NET, I was saving this as a last resort, but it seems I'm at that point :)
0 Kudos
jacobya
Beginner
1,413 Views
What's really weird is, if I change the order of the params around, it stops giving me an exception, but unfortuneately doesn't return any results either.
0 Kudos
sabalan
New Contributor I
1,413 Views
You have a C# problem and I know almost nothing about C#. Butwhat yousay makes me think that there must be some problem with thevariable lengths. I have seen in som C# samples that people use a "F" after single precision constants, e.g.
MyVar=1.15F
I don't know if this is the same as to write:
float MyVar=1.15
Maybe it would be worth to try a "D" after your double precision constants.
Sabalan.

Message Edited by Sabalan on 01-14-2005 01:09 AM

Message Edited by Sabalan on 01-17-2005 12:17 AM

0 Kudos
jacobya
Beginner
1,413 Views
I figured it out, my problems were partly due to my lack of understaning of VB and partly because of differences between VB.NET and C#. In the VB6 example, I thought it was passing the first parameter in an array to the function, but I guess that's how VB passes an array, so it turns out I was passing doubles where I was supposed to be passing arrays of doubles.
After I finally clued into this, I got it working in VB.NET, but not C#. To get it to work in C#, I had to pass all arrays by value and not by ref.
Thanks for the help and hopefully my lack of VB knowledge didn't waste too much of anyone's time.
Thanks again.
0 Kudos
Reply