c# - 将字符串从 C# 传递到 C DLL

标签 c# c string dll char

我正在尝试将字符串从 C# 传递到 C DLL。根据我的阅读,.NET 应该为我完成从字符串到 char* 的转换,但是我得到“错误 CS1503:参数‘1’:无法从‘string’转换为‘char*’”有人能告诉我我在哪里有错吗?谢谢。

C#代码

[DllImport("Source.dll", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
public static unsafe extern bool StreamReceiveInitialise(char* filepath);

const string test = "test";
// This method that will be called when the thread is started
public void Stream()
{
    if (StreamReceiveInitialise(test))
    {


    }
}

动态链接库

extern "C"
{
    __declspec(dllexport) bool __cdecl StreamReceiveInitialise(char* filepath);
}

最佳答案

将您的外部方法声明为:

public static extern bool StreamReceiveInitialise(string filepath);

关于c# - 将字符串从 C# 传递到 C DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8896030/

相关文章:

c# - 如何允许用户从列表框中复制项目并粘贴到窗体之外

c - Bison 在表达式类型之间进行转换

C - 在位图中绘制

java - 字符串分词器和管道符号

c++ - 在 C++ 中使用 wmemset

python - .join() 方法到底是做什么的?

c# - 为什么 C# 在控制台 writeline 中显示一个四舍五入的值,但作为实际值插入到数据库中?

c# - Dispatcher.Invoke 的竞争条件

c# - C# 中 Java 的 .length 相当于什么?

c - 使用三维动态矩阵时的分割错误