简体   繁体   中英

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#. 14 of the 15 functions a already running (Tested with console project). The remaining function has a paramater PChar where i dont know how to call it in 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)

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. 20 characters should be enough.

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

解决方案是使用“引用字符串”而不是“输出字符串”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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