【发布时间】:2012-02-12 08:09:21
【问题描述】:
我正在尝试将字符串从 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))
{
}
}
C DLL
extern "C"
{
__declspec(dllexport) bool __cdecl StreamReceiveInitialise(char* filepath);
}
【问题讨论】:
-
为什么将 StreamReceiveInitialise 声明为不安全?现在你仍然需要传递一个 char* 而不是一个字符串。
-
我现在已经删除了 unsafe 并且正在使用,如答案 1 所示。