C++函数原型:

BOOL _stdcall PSStartup(HANDLE hPrintps, LPCSTR lpszDatabasePath, LPCSTR lpszSpoolFile);

lpszSpoolFile为返回参数,由PSStartup中赋值并带回来。

一开始我在C#中的声明是:

[DllImport("PrintScu.dll", CallingConvention = CallingConvention.StdCall)]
public extern static int PSStartup(IntPtr hPrintps, string lpszDatabasePath, ref string lpszSpoolFile);

但是在实际调用的时候出现错误:

System.AccessViolationException was unhandled
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=mscorlib
  StackTrace:
       at System.String..ctor(SByte* value)
       at System.StubHelpers.CSTRMarshaler.ConvertToManaged(IntPtr cstr)

 

网上找了一圈之后发现这样声明不行,不能使用ref string,而需要使用StringBuilder,于是改成以下声明,测试通过:

[DllImport("PrintScu.dll", CallingConvention = CallingConvention.StdCall)]
public extern static int PSStartup(IntPtr hPrintps, string lpszDatabasePath, StringBuilder lpszSpoolFile);

stackoverflow帖子地址

相关文章:

  • 2021-10-30
  • 2022-02-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-18
  • 2022-12-23
  • 2021-07-21
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-09
  • 2022-12-23
相关资源
相似解决方案