Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran字符串引用C++和C++设置值 我有一个简单的FORTRAN应用程序,我想通过一个字符引用传递给C++ DLL方法,然后用C++方法设置引用字符串,但是我不能让它工作。这只是完整代码的一个子集,因为我甚至不能让它工作_C++_Fortran - Fatal编程技术网

Fortran字符串引用C++和C++设置值 我有一个简单的FORTRAN应用程序,我想通过一个字符引用传递给C++ DLL方法,然后用C++方法设置引用字符串,但是我不能让它工作。这只是完整代码的一个子集,因为我甚至不能让它工作

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_

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_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不安全,但这是正确的。非常感谢。