Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

C# string array to fotran?

pellea
Beginner
1,642 Views
Hello,

We're trying to push string array (String[]) of C# to a Fotran COM server but it doesn't work.

Do you have any samples to do that ? Which is the type to use in the Fortran .HIE file (Type(VARIANT), CHARACTER(256) (why 256?), PTR_KIND) ?


Thanks,
Pellea.
0 Kudos
8 Replies
Wendy_Doerner__Intel
Valued Contributor I
1,642 Views
Pellea,

No sample I know of though we do have some for Quickwin and calling a DLL in the directory (by default):

C:\Program Files (x86)\Intel\ComposerXE-2011\Samples\en_US\Fortran

In addition I think you might find this article of interest:

http://software.intel.com/en-us/articles/calling-fortran-function-or-subroutine-in-dll-from-c-code/

Hope these help you.

------

Wendy

Attaching or including files in a post

0 Kudos
onkelhotte
New Contributor II
1,642 Views
I dont know whether it differs from a COM Server, but when using a Fortran DLL for calculation in a C# GUI you have to use a StringBuilder variable instead of an string:

C# Code:

[bash]	[DllImport("kernel.dll")]
	private extern static void PUT_TEMPDIR(StringBuilder value, int lenOfString);

	[DllImport("kernel.dll")]
	private extern static void GET_TEMPDIR(StringBuilder value, int lenOfString);

	public static void puttempDir(string value)
	{
		StringBuilder stringBuilderValue = new StringBuilder(value.Substring(0, System.Math.Min(value.Length, 255)));
		PUT_TEMPDIR(stringBuilderValue, 255);
	}

	public static string gettempDir()
	{
		StringBuilder stringBuilderValue = new StringBuilder(255);
		GET_TEMPDIR(stringBuilderValue, 255);
		return stringBuilderValue.ToString();
	}[/bash]
Fortran Code:

[bash]subroutine PUT_TEMPDIR(value)
!DEC$ ATTRIBUTES DLLEXPORT::PUT_TEMPDIR
	use variables !tempDir is declared here
	implicit none
	character*(*) value
	tempDir = value
end subroutine PUT_TEMPDIR
!
subroutine GET_TEMPDIR(value)
!DEC$ ATTRIBUTES DLLEXPORT::GET_TEMPDIR
	use variables !tempDir is declared here
	character*(*) value
	value = tempDir
end subroutine GET_TEMPDIR[/bash]

Maybe this helps.

Markus
0 Kudos
pellea
Beginner
1,642 Views

I do not use a DllExport and I have to pass to fortran an array of string and not only one.

I succeeded to pass to fortran only one string.

0 Kudos
anthonyrichards
New Contributor III
1,642 Views
This link could have some useful information for you:

http://www.nag.co.uk/numeric/csharpinfo.asp
0 Kudos
pellea
Beginner
1,642 Views
In this example they convert the string array to a byte array and pass this last one to the fortran. Is it possible to pass directly a string array?
0 Kudos
anthonyrichards
New Contributor III
1,642 Views
I would think yes, probably, as a SafeArray. You will have to use Marshalling (don't ask!). The same problem (passing string arrays) but for VB .NET to Fortran is dealt with in one of the IVF examples, so I would guess the treatment for C# should be very similar.

You will still finish up with a SafeArray on the Fortran side, which is not so easily dealt with as a standard Fortran array. You will have to use special routines such as SafeArrayGetDim, SafeArrayGetLBound,SafeArrayGetUBound,SafeArrayGetElement, etc. in order to get at the strings. The strings will also be 'wide' strings, using 2-bytes per character and so they also will require conversion before you finish up with a standard Fortran character string which uses 1 byte per character..
0 Kudos
pellea
Beginner
1,642 Views
I saw that solution too but it was too complicated for what I want to do.

Thanks for answer,I think I'll stay with the array of byte. It's easy to set up and it requires only a little conversion.
0 Kudos
mariaosawa
Beginner
1,642 Views
you see the code above, is the place where I do not understand
0 Kudos
Reply