How can I return a PChar from a DLL function to a VB6 application without risking crashes or memory leaks?

前端 未结 7 1509
花落未央
花落未央 2020-12-19 05:37

I have to create a DLL which is used by a VB6 application. This DLL has to provide several functions, some of them must return strings.

This is the VB6 declaration:<

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 05:58

    You need to craft a BSTR instead. VB6 strings are actually BSTRs. Call SysAllocString() on the Delphi side and return the BSTR to the VB6 side. The VB6 side will have to call SysFreeString() to free the string - it will do it automatically.

    If PChar corresponds to an ANSI string (your case) you have to manually convert it to Unicode - use MultiByteToWideChar() for that. See this answer for how to better use SysAllocStringLen() and MultiByteToWideChar() together.

提交回复
热议问题