C# string problems with .net 4.0

C# string problems with .net 4.0

I have project where a c# GUI calls a fortran subroutine.  The program passes integrers, floats, arrays, and strings.  I built the c# program originally with .NET 2.0 without problems, then .NET 3.0, and even .NET 3.5 without any issue.  When I try to upgrade my c# program to .NET 4.0 I suddenlt start to get string errors.  What has worked in the past for the string definition (much thanks to help from this forum) is as follows:

C# -  [MarshalAs(UnmanagedType.LPStr] string logfile

Fortran - character*255 logfile.

Now I am getting the error: forrtl:severe (408): fort: (18): Dummy character variable 'LOGFILE' has length 255 which is greater than actual variable length 0.

I know in VB there were issues with having to sneak in the string length into the argument pass, but I have had no problems with this project as it is currently written until I moved over to .NET 4.0 (which I need to do).  Anyone have any advice?

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

C# -  [MarshalAs(UnmanagedType.LPStr] defines "A pointer to a null-terminated array of ANSI characters." according to http://msdn.microsoft.com/en-us/library/s9ts558h.aspx.

Therefore, on your Fortran side, I guess you need a compiler directive to mark the variable 'Logfile' as by REFERENCE. Do you do this?


Thanks for that!  I meant to try that and somehow thought I had.  That fixed the problem.  The big question is, why did ommitting the REFERENCE work for the earlier versions of .NET?  Either way, problem solved,  Thanks!


I spoke too soon.  The error goes away, but the strings (there are three of them) come through garbled, and it looks like their memory locations are linked similar to an EQUIVALENT statement ...very strange.  A work around is to accept the strings into one set of variable names, and them write to another.  This seems to protect the strings within the program. but it makes me nervous to have memory locations getting overwritten for no obvious reason.

Please explain what you mean by 'garbled'. Give examples.

Plus, please confirm that your is C# program is calling Fortran and that you are null-terminating the strings.

Please give the full C# calling sequence and its function prototype.

Also, please list all your compiler directives in the Fortran routine that is receiving the strings and also the function/routine argument list and specifications for them.

Leave a Comment

Please sign in to add a comment. Not a member? Join today