Powerbasic Museum 2020-B

IT-Consultant: Frederick J. Harris => PowerBASIC Programmer's Survival Guide To C And C++ => Topic started by: Chris Chancellor on August 21, 2018, 07:32:58 PM

Title: How to write c++ to call a PB dll
Post by: Chris Chancellor on August 21, 2018, 07:32:58 PM
Hello all

i'm looking for a c++ code that can call a PB dll and retrive a string from that PB dll.

how to write this c++ code ?

all help appreciated and thanxx in advance
Title: Re: How to write c++ to call a PB dll
Post by: Patrice Terrier on August 22, 2018, 03:28:03 PM
The easiest way is to export your strings as ASCIIZ.

But if you link with a PB DLL, then you are limiting yourself to 32-bit only.
Title: Re: How to write c++ to call a PB dll
Post by: Chris Chancellor on August 22, 2018, 03:54:48 PM
Thanxx Patrice

i'm doing the conversion to C++ stage by stage, something like testing the water first
before leaping into it.  The main thing is to shed the PB codes or modules whenever i find a better
replacement for it.

so do you have any codes in C++ and PB that i can test out ?
Title: Re: How to write c++ to call a PB dll
Post by: Patrice Terrier on August 22, 2018, 06:25:09 PM
Exported ASCIIZ return a pointer.

So you can use movememory to copy it into you own buffer.

See also
https://forum.powerbasic.com/forum/user-to-user-discussions/powerbasic-for-windows/56114-conversion-c-c-pb
Title: Re: How to write c++ to call a PB dll
Post by: Frederick J. Harris on October 03, 2018, 04:14:00 AM
Quote
i'm looking for a c++ code that can call a PB dll and retrive a string from that PB dll.

Everything depends on what you mean by retrieve 'a string'.  Most PB'ers use PowerBASIC's Dynamic String Type, i.e., String or WString.  The underlying machinery involved in that is the COM based BSTR data type, which is actually a very simple class.  I would assume if you have a PowerBASIC dll containing one or more functions which take as a parameter a String as described above, then you are going to get yourself into C/C++ 'deep water' very fast, as you are going to have to master some fairly advanced C++ topics including C++ low level COM and the nuances of the BSTR type. 

If you are just starting out with C++ your best bet is to do as Patrice says and rework your PowerBASIC dll to use null terminated strings (asciiz or wstringz).  Having done that, using a C/C++ host/client, the incomming string will simply be interpreted as a pointer to a string of characters - something a beginning C/C++ coder can easily deal with.  Hope this helps.  In my various tutorials I give a number of simple examples of this.