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

前端 未结 7 1491
花落未央
花落未央 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条回答
  •  無奈伤痛
    2020-12-19 05:55

    You cannot return a PChar as a function result, but you can pass an additional PChar parameter and copy the string you want to return to this PChar. Note, that VB must allocate that string to the required size before passing it to the dll. Also in VB that parameter must be declared as byval param as string AND it must be passed with byval:

      param = "aaaaaaaaaaaaaaaaaaaa" ' reserve 20 characters
      call myproc(byval param)
    

    The additional byval in the call will do the compiler magic of converting a VB string to a PChar and back.

    (I hope I remember this is correctly, it has been quite a while since I was forced to use VB.)

提交回复
热议问题