如何使用 DLLImport 将字符串从 C# 传递到 C++(以及从 C++ 到 C#)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10799685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass strings from C# to C++ (and from C++ to C#) using DLLImport?
提问by Jsncrdnl
I've been trying to send a string to/from C# to/from C++ for a long time but didn't manage to get it working yet ...
很长一段时间以来,我一直试图将一个字符串发送到/从 C# 发送到/从 C++ 发送到/从 C++,但还没有设法让它工作......
So my question is simple :
Does anyone know some way to send a string from C# to C++ and from C++ to C# ?
(Some sample code would be helpful)
所以我的问题很简单:
有谁知道将字符串从 C# 发送到 C++ 以及从 C++ 发送到 C# 的某种方法?
(一些示例代码会有所帮助)
采纳答案by Samy Arous
Passing string from C# to C++ should be straight forward. PInvoke will manage the conversion for you.
将字符串从 C# 传递到 C++ 应该是直截了当的。PInvoke 将为您管理转换。
Geting string from C++ to C# can be done using a StringBuilder. You need to get the length of the string in order to create a buffer of the correct size.
可以使用 StringBuilder 将字符串从 C++ 获取到 C#。您需要获取字符串的长度才能创建正确大小的缓冲区。
Here are two examples of a well known Win32 API:
以下是著名的 Win32 API 的两个示例:
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
public static string GetText(IntPtr hWnd)
{
// Allocate correct string length first
int length = GetWindowTextLength(hWnd);
StringBuilder sb = new StringBuilder(length + 1);
GetWindowText(hWnd, sb, sb.Capacity);
return sb.ToString();
}
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool SetWindowText(IntPtr hwnd, String lpString);
SetWindowText(Process.GetCurrentProcess().MainWindowHandle, "Amazing!");
回答by noonand
A lot of functions that are encountered in the Windows API take string or string-type parameters. The problem with using the string data type for these parameters is that the string datatype in .NET is immutable once created so the StringBuilder datatype is the right choice here. For an example examine the API function GetTempPath()
Windows API 中遇到的许多函数都采用字符串或字符串类型的参数。对这些参数使用字符串数据类型的问题在于 .NET 中的字符串数据类型一旦创建就无法改变,因此 StringBuilder 数据类型是这里的正确选择。例如,检查 API 函数 GetTempPath()
Windows API definition
Windows API 定义
DWORD WINAPI GetTempPath(
__in DWORD nBufferLength,
__out LPTSTR lpBuffer
);
.NET prototype
.NET 原型
[DllImport("kernel32.dll")]
public static extern uint GetTempPath
(
uint nBufferLength,
StringBuilder lpBuffer
);
Usage
用法
const int maxPathLength = 255;
StringBuilder tempPath = new StringBuilder(maxPathLength);
GetTempPath(maxPathLength, tempPath);
回答by sithereal
in your c code:
在你的 C 代码中:
extern "C" __declspec(dllexport)
int GetString(char* str)
{
}
extern "C" __declspec(dllexport)
int SetString(const char* str)
{
}
at .net side:
在.net方面:
using System.Runtime.InteropServices;
[DllImport("YourLib.dll")]
static extern int SetString(string someStr);
[DllImport("YourLib.dll")]
static extern int GetString(StringBuilder rntStr);
usage:
用法:
SetString("hello");
StringBuilder rntStr = new StringBuilder();
GetString(rntStr);

