By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,507 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,507 IT Pros & Developers. It's quick & easy.

Fixed character length problem calling Fortran DLL

P: n/a
I am calling a Fortran DLL that has a declaration like: character * 260
variablename. I do not have the Fortran code. A path to a file is
passed to the DLL.

C# does not have a fixed character length. I can use vba or vb6 with
Dim variablename as String * 260 and it works. However, I can not get
this to work in C#. The DLL returns a value denoting that the file is
not found. I even concatenated spaces to the string to make sure it
had 260 characters. No luck.

I have no idea how to pass this required parameter to the Fortran DLL
with C#.

Any ideas? Thank you.

Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

This example is slightly different but it might help. In RAS I can dial a
modem by passing this C struct to a raw WIN32 API. The C struct contains
several char arrays of fixed length:

typedef struct _RASDIALPARAMS {
DWORD dwSize;
TCHAR szEntryName[RAS_MaxEntryName + 1];
TCHAR szPhoneNumber[RAS_MaxPhoneNumber + 1];
TCHAR szCallbackNumber[RAS_MaxCallbackNumber + 1];
TCHAR szUserName[UNLEN + 1];
TCHAR szPassword[PWLEN + 1];
TCHAR szDomain[DNLEN + 1] ;
DWORD dwSubEntry;
ULONG_PTR dwCallbackId;
} RASDIALPARAMS;
The equivalent C# code looks like this:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
class RasDialParams
{
public int dwSize=Marshal.SizeOf(typeof(RasDialParams));
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxEntryName + 1)]
public string szEntryName = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxPhoneNumber + 1)]
public string szPhoneNumber = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxCallbackNumber + 1)]
public string szCallbackNumber = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxUserName + 1)]
public string szUserName = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxPassword + 1)]
public string szPassword = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxDomain + 1)]
public string szDomain = null;
public int dwSubEntry = 0;
public int dwCallbackId = 0;
}

"AlanL" wrote:
I am calling a Fortran DLL that has a declaration like: character * 260
variablename. I do not have the Fortran code. A path to a file is
passed to the DLL.

C# does not have a fixed character length. I can use vba or vb6 with
Dim variablename as String * 260 and it works. However, I can not get
this to work in C#. The DLL returns a value denoting that the file is
not found. I even concatenated spaces to the string to make sure it
had 260 characters. No luck.

I have no idea how to pass this required parameter to the Fortran DLL
with C#.

Any ideas? Thank you.

Nov 17 '05 #2

P: n/a
Thank you. This got me pointed in the right direction. The Fortran
DLL liked the following. Thanks again.

[StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
public struct DataFiles
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)] public string
Fan;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)] public string
Fin;
public DataFiles(string Fan, string Fin)
{
this.Fan = Fan;
this.Fin = Fin;
}
}

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.