从c#字符串传递char *到C DLL
pass char * to C DLL from C# string
我需要在c#应用程序的DLL中使用C函数库。我在使用char *参数调用DLL函数时遇到麻烦:
C DLL:
extern "C" __declspec(dllexport) int CopyFunc(char *, char *);
int CopyFunc(char *dest, char *src)
{
strcpy(dest, src);
return(strlen(src));
}
c#应用程序需要看起来像这样:
[DllImport("dork.dll")]
public static extern int CopyFunc(string dst, string src);
int GetFuncVal(string source, string dest)
{
return(CopyFunc(dest,source));
}
我见过使用字符串或StringBuilder或IntPtr作为DLL函数原型所需的char *的替代品的例子,但我没能让它们中的任何一个工作。我得到的最常见的异常是PInvoke使堆栈不平衡,因为函数调用与原型不匹配。
有一个简单的解决方案吗?
更新外部函数的p/Invoke声明:
[DllImport("dork.dll")]
public static extern int CopyFunc([MarshalAs( UnmanagedType.LPStr )]string a, [MarshalAs( UnmanagedType.LPStr )] string b);
int GetFuncVal(string src, string dest)
{
return(CopyFunc(dest,src));
}
相关文章:
- 使用c#访问c++dll中带有char*参数的函数时发生AccessViolationException
- 从 C# GUI 将 char * 参数传递给C++ dll 函数
- 使用安全模式从DLL(通过(INTPTR)参数传递到该函数)获取char **
- Java:加载带有char **的C DLL,将回调作为参数
- 当我释放由CFFI生成的DLL分配的char*时,为什么我的应用程序会崩溃
- 将字节数组作为char*从C#传递到C++DLL
- 使用 char* 将结构传递给 C# 中的 C++ dll 函数
- 使用dll将C#StringBuilder / String传递给C char*
- (c#)从c dll获取char **时的accessViolationException
- 从C DLL接收Char **到C#String []
- 我在 VC32 中有 Win6 DLL,并使用参数 char* 调用 C#
- 用 C# 编组我的C++ .dll与 "const char*" 和 "const int"
- 将 char*[] 从 C++ DLL 结构体传递到 C#
- 如何在 C# 应用程序中使用 extern "C" dll 函数以 char** 作为参数?
- 使用Node.js中包含char数组的结构调用C++dll
- char*在从C DLL返回到js ctypes时丢失
- 将StringBuilder传递给DLL函数时需要char指针
- 如何在.NET Visual Basic应用程序中读取从C++DLL返回的const char*
- 从VB向C++DLL传递const wchar_t*和cont char*
- 外部dll、静态函数和C++中的char*关系