Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.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
如何在C++;还有C#? 我在C++项目中导入了C++库。我可以得到一个没有参数的C++方法的答案。我想做的是把一些字符串参数发送给C++并得到它的结果。_C#_C++_String_Interop_Pinvoke - Fatal编程技术网

如何在C++;还有C#? 我在C++项目中导入了C++库。我可以得到一个没有参数的C++方法的答案。我想做的是把一些字符串参数发送给C++并得到它的结果。

如何在C++;还有C#? 我在C++项目中导入了C++库。我可以得到一个没有参数的C++方法的答案。我想做的是把一些字符串参数发送给C++并得到它的结果。,c#,c++,string,interop,pinvoke,C#,C++,String,Interop,Pinvoke,在C++中: public String SomeMethod(String param1, String param2) { //Some code } 在C#中: 我如何做到这一点?< < /P> < P>对于从C++ C++传递到强>输入参数< /强>的字符串,可以在C++接口上使用 LPCWSTR> (即 const WCHARYT*),并在C++的边上使用 >代码> unMaundType。 但要注意 >将字符串从C++返回到C<> 事实上,字符串必须以C++方式分配,然

在C++中:

public String SomeMethod(String param1, String param2)
{
     //Some code
}

在C#中:


<>我如何做到这一点?< < /P> < P>对于从C++ C++传递到<>强>输入参数< /强>的字符串,可以在C++接口上使用 LPCWSTR> <代码>(即<代码> const WCHARYT*<代码>),并在C++的边上使用<强> >代码> unMaundType。

但要注意<强> >将字符串从C++返回到C<> 事实上,字符串必须以C++方式分配,然后字符串指针必须传递给C。C#和CLR必须能够在不再需要该字符串时正确释放该字符串,这一点可能会出现问题。例如:C·y/CLR应该能够使用C++代码所使用的相同分配器来释放字符串内存,以分配字符串。 < >使用C++创建的字符串内存相同的内存分配程序,并使用<强> <代码> BSTR < /代码> S<强>来解决。

BSTR
是一个COM字符串,使用COM分配器分配,可以使用相同的COM分配器释放。所以,C++和C++都可以共享字符串的相同内存分配器。 要返回
BSTR
,可以使用
marshallas.UnmanagedTypeBStr
选项

有关C#中字符串封送的更多详细信息,请参见

C++中:

//
// Note: Export using a pure C interface from C++ DLL.
// (You can use C++ *inside* the DLL boundaries.)
// __stdcall is a widely used calling convention for 
// C DLL functions (e.g. lots of Win32 APIs use it).
//
extern "C" BSTR __stdcall GetSomeString(void)
{
    //
    // Note: BSTRs are allocated using SysAllocString()
    // (...and freed using SysFreeString()).
    //
    // You could also use a convenient ATL RAII wrapper
    // (instead of using raw BSTRs inside C++ code), 
    // like CComBSTR.
    //
    return ::SysAllocString(L"Hello from C++!");
}
[DllImport(@"YourDll.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.BStr)]
private static extern string GetSomeString();
C#中:

//
// Note: Export using a pure C interface from C++ DLL.
// (You can use C++ *inside* the DLL boundaries.)
// __stdcall is a widely used calling convention for 
// C DLL functions (e.g. lots of Win32 APIs use it).
//
extern "C" BSTR __stdcall GetSomeString(void)
{
    //
    // Note: BSTRs are allocated using SysAllocString()
    // (...and freed using SysFreeString()).
    //
    // You could also use a convenient ATL RAII wrapper
    // (instead of using raw BSTRs inside C++ code), 
    // like CComBSTR.
    //
    return ::SysAllocString(L"Hello from C++!");
}
[DllImport(@"YourDll.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.BStr)]
private static extern string GetSomeString();
如果选择<代码> bSTR < /Cult> s作为返回字符串,对于C++函数接口代码的一致性,您也可以使用<代码> BSTR < /Cord>s作为输入字符串(即使不是严格必要的)。

另一个选项是请求<强>调用方提供适当大小的缓冲区,该C++函数将填充返回的字符串。这在某种程度上类似于一些Win32 API(例如)的功能。
例如,您可以找到

GetWindwoText()
对应的P/Invoke:

但是,我认为从C#调用的角度来看,返回一个
BSTR
更好(事实上,在C#端,您不需要对输出字符串使用
StringBuilder



<> C++ C++ C++语言,其中,最后一个但不是最不重要的一个选项,你可能感兴趣的是建立一个极小的<强> c+/CLI桥接层,将你的本机C++代码暴露到C++中,并用C++和CLI在本地和托管之间编组字符串(例如 > Stry::String 类)。?使用C++/CLI构建包装器类