繁体   English   中英

将Fortran字符串引用传递给C ++,然后C ++设置值

[英]Pass Fortran string reference to C++ and C++ set the value

我有一个简单的Fortran应用程序,我试图将字符引用传递给C ++ dll方法,然后让C ++方法设置引用字符串,但我无法使其正常工作。 这只是完整代码的一部分,因为我什至无法正常工作。

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"指令,您已将变量“ yellow”的地址分配给了buff变量,而原始缓冲区保持不变。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM