简体   繁体   English

在C#中调用VisualBasic DLL可行,但是如何替换Pchar?

[英]Call VisualBasic DLL in C# works but how to replace a Pchar?

i have an old DLL written in VisualBasic and need to call the functions in C#. 我有一个用VisualBasic编写的旧DLL,需要调用C#中的函数。 14 of the 15 functions a already running (Tested with console project). 15个功能中的14个已经在运行(已通过控制台项目测试)。 The remaining function has a paramater PChar where i dont know how to call it in C#. 剩余的函数有一个参数PChar,我不知道如何在C#中调用它。

A working function looks like this: 工作功能如下所示:

[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);
}

The not working function has two parameters: 1. out pData: PChar 2. const pDataLen (This is the length of data that is returned) 不起作用的函数有两个参数:1. out pData:PChar 2. const pDataLen(这是返回的数据长度)

The manual for the VB DLL says that it is important to reserve memory before calling the function and also to free up memory after function is finished. VB DLL手册指出,重要的是在调用函数之前保留内存,并在函数完成后释放内存。 20 characters should be enough. 20个字符就足够了。

Can someone give me a hint how to call the function? 有人可以提示我如何调用该函数吗? When i simply use "string" then i get an exeception: An unhandled exception of Type System.AccesViolationException occured. 当我简单地使用“字符串”时,我得到一个理解:发生了System.AccesViolationException类型的未处理的异常。 Addidtional information: Attempted to read or write protected memory. 其他信息:尝试读取或写入受保护的内存。 This is often an indication that other memory is corrupt. 这通常表明其他内存已损坏。

Thanks for help. 感谢帮助。

Did you try with pointer?? 您尝试使用指针了吗?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM