473,414 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Error calling Fortran function from C# using

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.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
6 1605
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..

"jacob ya" <ja*****@netzero.net> schrieb im Newsbeitrag
news:uB****************@TK2MSFTNGP11.phx.gbl...
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.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Please respond if that is solving your problem, because Iam interested if my
ideas were correct.

"jacob ya" <ja*****@netzero.net> schrieb im Newsbeitrag
news:uB****************@TK2MSFTNGP11.phx.gbl...
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.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
IIRC, in VB an int was only 16bit so you could try to pass short instead of
int.

"jacobya" <jacobya@NOSPAMnetzeroDOTnet> schrieb im Newsbeitrag
news:u1****************@TK2MSFTNGP14.phx.gbl...
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #6
Actually, the VB version was using long, so I already accounted for
that, but thanks.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Bigdakine | last post by:
I don't know if this is the right forum for this, and if not please suggest one which fits. I have to call a fortran sub routine from a C main program. The fortran subroutine statement is ...
9
by: travisperkins03 | last post by:
Hi, I have read somewhere that C code sometimes cannot be compiled to be as efficient as FORTRAN, eg for matrix multiplication, because a C compiler cannot make the assumptions about arrays that...
2
by: Ray J. | last post by:
I have a C++ program written and compiled on Solaris 8 with gcc. With gcc lets me compile fortran code along with the C++ program to be able to call the fortran code as a subroutine. The...
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
3
by: Alexander Eisenhuth | last post by:
Hello, Ctrl+C is not passed to the interpreter (i guess it) while I'm executing a script. Instead i get: forrtl: error (200): program aborting due to control-C event If I start python in...
2
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p *...
1
by: urkel | last post by:
Hi everyone, I critically need help to solve this problem related to pointer in C++ Basically, I have a C/C++ program "retardselfenerg" calling a Fortran 90 subroutine "surfGF4.f90". i am so...
3
by: =?Utf-8?B?TWFyZWs=?= | last post by:
Hi As well as trying to call a fortran dll with a structure parameter from a C# front end, we are trying to do this using AssemblyBuilder and MethodInfo to do it dynamically. Ultimately this will...
0
by: Gabriel Genellina | last post by:
En Sun, 21 Sep 2008 19:42:10 -0300, Blubaugh, David A. <dblubaugh@belcan.comescribió: Below there is a transcript of a compile session. I've used a somewhat old version of mingw and g77, scipy...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.