c# - 将 C# 中的 stringbuilder 或 byte[] 传递到非托管 API 兑现异常访问冲突
问题描述
我有一个非托管 API,其签名是:
int ImportFile( char *sFileName )
其中 Filename 是要导入的文件的名称、完整路径和文件名。
所以我在我的 C# 代码中使用这个 API:
string StepFilePath = @"C:\Users\pandey\AppData\Local\Temp\W-00004_CAD.stp";
StringBuilder Sb = new StringBuilder(256);
if (!String.IsNullOrEmpty(StepFilePath))
{
Sb.Append(StepFilePath);
}
int a = API.ImportFile(Sb);
此时它崩溃并给出错误访问冲突读取位置错误。我什至尝试使用 byte[] 代替 stringbuilder 但它没有用。
我已经在一个单独的 API.cs 文件中声明了这个 API,如下所示:
[DllImport("exe name", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
unsafe extern static public int ImportFile(StringBuilder aStringBuilder);
任何想法可能是什么问题?我应该使用其他东西来代替 StringBuilder 还是 byte[]。
解决方案
推荐阅读
- java - 添加到不重复的链表(util)
- python - 复制文件时在控制台中显示进度条。在 python 中使用 tqdm
- javascript - 在AngularJS控制器中编译html中的表达式
- r - 我可以将 NetLogo 与另一个程序结合起来吗?
- javascript - 在 WebRTC 中取消静音麦克风后,出现严重的本地环回(自己的声音)问题
- windows - 无法重置
- excel - 使用ods excel语句时停止Excel转换保存为字符的数字
- mysql - 如何从另一台计算机向树莓派上的 phpmyadmin 发送数据?
- python - 为什么在我的 Windows 机器上我必须将命令写成“py -m pip install”module-name”,而不是只写“pip install”module-name”?
- html - 如何重置仅使用jquery允许数字的html文本框