Fixed character length problem calling Fortran DLL

A

AlanL

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.
 
G

Guest

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;
}
 
A

AlanL

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;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top