Fortran字符串引用C++和C++设置值 我有一个简单的FORTRAN应用程序,我想通过一个字符引用传递给C++ DLL方法,然后用C++方法设置引用字符串,但是我不能让它工作。这只是完整代码的一个子集,因为我甚至不能让它工作
Fortran代码Fortran字符串引用C++和C++设置值 我有一个简单的FORTRAN应用程序,我想通过一个字符引用传递给C++ DLL方法,然后用C++方法设置引用字符串,但是我不能让它工作。这只是完整代码的一个子集,因为我甚至不能让它工作,c++,fortran,C++,Fortran,Fortran代码 program FortranConsoleExample implicit none interface !!! subroutine reverseString(str_out, str_in, str_in_len) bind(C, name="ReverseString3") USE, INTRINSIC :: ISO_C_BINDING integer(C_INT), VALUE, INTENT(IN) :: str_
program FortranConsoleExample
implicit none
interface
!!!
subroutine reverseString(str_out, str_in, str_in_len) bind(C, name="ReverseString3")
USE, INTRINSIC :: ISO_C_BINDING
integer(C_INT), VALUE, INTENT(IN) :: str_in_len
character, dimension(*), intent(IN) :: str_in
character(kind=C_CHAR), dimension(512), intent(out) :: str_out
end subroutine reverseString
end interface
! Variables
character*512 :: strResult
call reverseString(strResult, "tacobell", len(strResult))
print *, strResult
end program FortranConsoleExample
C++代码
extern "C"
{
__declspec(dllexport) void __cdecl ReverseString3(char * buff, const char *text, int buffLen)
{
buff = "yellow";
}
}
如果你写:
void ReverseString3(char * buff, const char *text, int buffLen)
{
strncpy(buff,"yellow", 6);
}
它将起作用,您必须注意在Fortran部分中初始化字符串,如下所示:
strResult = " "
看看
使用指令buff=yellow,您已将字符串黄色的地址分配给变量buff,保留原始缓冲区不变。如果您写入:
void ReverseString3(char * buff, const char *text, int buffLen)
{
strncpy(buff,"yellow", 6);
}
它将起作用,您必须注意在Fortran部分中初始化字符串,如下所示:
strResult = " "
看看
使用指令buff=yellow,您已将字符串黄色的地址分配给变量buff,而保留原始缓冲区不变。我最终使用了strncpy,因为Visual Studio说strncpy不安全,但这是正确的路径。谢谢。我最终使用了strncpy\s,因为VisualStudio说strncpy不安全,但这是正确的。非常感谢。