要將Fortran字符串傳遞給C,隱藏參數也會與變量的大小一起傳遞。這裏有一個工作FORTRAN定義,以及C(實際上是C++/CLI)方法:多個Fortran字符串應該如何傳遞給C?
interface
subroutine AppendExtension(
+ Filename)
+ bind(C, name="AppendExtension")
character *1, intent(inout):: Filename
end subroutine AppendExtension
end interface
和這裏的C++/CLI,它被稱爲:
extern "C" {
void __declspec(dllexport) __cdecl AppendExtension(
char * name,
int buffersize)
{
String^ clistr = gcnew String(name);
clistr = System::IO::Path::ChangeExtension(clistr->Trim(), gcnew String("OUT"));
IntPtr p = Marshal::StringToHGlobalAnsi(clistr);
char *pNewCharStr = static_cast<char*>(p.ToPointer());
int cstrlen = strlen(pNewCharStr);
memcpy_s(name, buffersize, pNewCharStr, cstrlen);
if (cstrlen < buffersize)
{
// backfill with spaces, since a Fortran string is spaces on the right.
memset(&name[cstrlen], ' ', buffersize-cstrlen);
}
Marshal::FreeHGlobal(p);
}
以上正常工作(英特爾視覺Fortran語言2013 SP1)。
我現在想將兩個字符串傳遞給一個函數。下面是我所做的:
interface
subroutine ParseSpec(
+ Filename, Corename)
+ bind(C, name="ParseSpec")
character *1, intent(inout):: Filename
character *1, intent(inout):: Corename
end subroutine ParseSpec
end interface
下面是行動電話:
CHARACTER*80 FILE_SPEC
CHARACTER*200 CORE_NAME
CALL ParseSpec(FILE_SPEC, CORE_NAME)
和這裏的C++/CLI:
void __declspec(dllexport) __cdecl ParseSpec(char * name, char * corename, int namelen, int corelen)
{
// namelen and corelen both contain the length of "name".
...
有兩個隱藏的變量。我認爲他們應該爲我的兩個字符串中的每一個的緩衝區大小,一個用於「文件名」,另一個用於「核心名稱」。但都包含第一個緩衝區的緩衝區大小,即80.
我在哪裏出錯了?