![](/img/trans.png)
[英]The third-party DLL works when called from Excel and crashes in the case of call from VisualBasic/C#
[英]Call VisualBasic DLL in C# works but how to replace a Pchar?
我有一个用VisualBasic编写的旧DLL,需要调用C#中的函数。 15个功能中的14个已经在运行(已通过控制台项目测试)。 剩余的函数有一个参数PChar,我不知道如何在C#中调用它。
工作功能如下所示:
[DllImport("C:\\DLL\\visualbasic.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
public static extern int command(out UInt32 pNumber);
public void Executecommand(out UInt32 pNumber, out int Res)
{
Res = command(out pNumber);
}
不起作用的函数有两个参数:1. out pData:PChar 2. const pDataLen(这是返回的数据长度)
VB DLL手册指出,重要的是在调用函数之前保留内存,并在函数完成后释放内存。 20个字符就足够了。
有人可以提示我如何调用该函数吗? 当我简单地使用“字符串”时,我得到一个理解:发生了System.AccesViolationException类型的未处理的异常。 其他信息:尝试读取或写入受保护的内存。 这通常表明其他内存已损坏。
感谢帮助。
您尝试使用指针了吗?
public static extern void NotWorkFunction(out IntPtr pOut, int someConst)
void Main(string[] args)
{
IntPtr pOut;
int dataLength;
string foo;
NotWorkFunction(pOut, dataLength);
foo = Marshal.PtrToStringAuto(pOut);
BlockFree(pOut);
}
解决方案是使用“引用字符串”而不是“输出字符串”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.