C++はC#に文字列を渡します

2446 ワード

方法1:
  • headfile:
  • #pragma once
    extern "C" __declspec(dllexport) char* SayHelloWorld();
    
  • cpp file
  • char* SayHelloWorld(){
    	return "hello world";
    }
    
  • C# file
  • [DllImport("persimmon_dll")]
    public static extern IntPtr SayHelloWorld();
    
    //  
    IntPtr ret = SayHelloWorld();
    string str = Marshal.PtrToStringAnsi(ret);
    

    方法2
    使用中にエラーが発生したので、さらに検査してから貼り付けます
    リファレンス
    C#C++dll関数を呼び出すときに文字列を渡すときに注意すべき問題