Bridging PowerBasic and a PureBasic DLL: A Detailed Guide on Interoperability

Everything else that doesn't fall into one of the other PB categories.
TheoG
New User
New User
Posts: 8
Joined: Fri Jun 16, 2023 7:04 pm

Bridging PowerBasic and a PureBasic DLL: A Detailed Guide on Interoperability

Post by TheoG »

Greetings fellow developers,

In this post, I aim to shed light on a critical aspect of programming that often goes unnoticed - the handling of strings and binary data in different programming languages. Specifically, I will be focusing on two languages: PureBasic and PowerBasic.

Understanding String Engines: PureBasic vs PowerBasic
The first point of divergence between PureBasic and PowerBasic lies in their string engines. PowerBasic employs BSTRINGS, while PureBasic uses "zero terminated strings". This distinction becomes crucial when dealing with binary data.

In PowerBasic, you can directly insert binary data into a string. However, in PureBasic, you need to allocate memory for binary data and handle it in a manner akin to the C programming language.

It's worth noting that strings often contain "text" and do not include any zeroes. In such cases, you can use a PureBasic DLL with the strings from PowerBasic without any issues.

The Case for PureBasic DLLs in PowerBasic
There are compelling reasons to consider calling a PureBasic DLL from PowerBasic. Over time, PureBasic has surpassed PowerBasic in terms of library support, offering a wide range of functionalities with just a handful of commands.

Moreover, PureBasic supports x64, is user-friendly, and is a "Single-Pass" compiler, which results in impressive compiler speed.

The Power of Combining PowerBasic and PureBasic
Why not leverage the best of both worlds? By combining your existing PowerBasic code with the new PureBasic, transitioning becomes much smoother. Let's delve into this further.

Creating a DLL with PureBasic and Calling it from PowerBasic
The goal here is to create a DLL using PureBasic and call it from a PowerBasic main program. While working with numbers poses no problem, string handling can be a bit more complex.

A Simple Example: Sending a String from PowerBasic to a PureBasic DLL
Let's start with a simple example where we send a string from PowerBasic into a PureBasic DLL. We'll be working with both 32-bit versions, as PowerBasic does not support x64.

In the following sections, I will provide a step-by-step guide on how to accomplish this, complete with code snippets and explanations. Stay tuned!

I hope this post has been informative and sparked your interest in exploring the interoperability between PureBasic and PowerBasic. As always, I welcome any questions or comments you may have.

Code: Select all

';This is the Code in the Purebasic DLL

Global.s Mode

ProcedureDLL Set_Mode(U01.s)  
 Protected S02.s
 If Len(U01)>0  
   Mode=Trim(U01)
 Else
   Mode=#defmode
 EndIf
EndProcedure

Code: Select all

' In Powerbasic you would write
DECLARE SUB Set_Mode LIB "Pure.dll" ALIAS "Set_Mode" (BYVAL U01 AS WSTRING)   
You can then proceed to call the PureBasic DLL and set the variable. However, it's crucial to ensure that your string doesn't contain any "zeroes" before sending it to the DLL, as this could potentially lead to unexpected outcomes.

Retrieving a string from a PureBasic DLL into PowerBasic presents a more challenging scenario. The interoperability between the two isn't as seamless as one might hope. However, it's not an insurmountable task. Let's explore this through an example:

Code: Select all

; This is the code in the Purebasic DLL to send the String 
; Last_A to Powerbasic
; Its split into 2 Subprogrammes

Global.s Last_A
ProcedureDLL.l GetLast_A()    
ProcedureReturn @Last_A
EndProcedure

ProcedureDLL.l GetALen(U01.l)
  Static T01.l
   T01=Len(Last_A)
  ProcedureReturn T01
EndProcedure    
  
On the Powerbasic side we need to put this all together.

Code: Select all

DECLARE FUNCTION GetALenLIB "Pure.dll" ALIAS "GetALen" () AS LONG
DECLARE FUNCTION GetLast_ALIB "Pure.dll" ALIAS "GetLast_A" () AS LONG  

' This will do the trick
FUNCTION Get_Last_A() AS STRING
REGISTER R01 AS LONG,R02 AS LONG
LOCAL S01 AS STRING
R01=GetLast_A()
R02=GetALen()
S01=PEEK$$(R01,R02)
FUNCTION=S01
END FUNCTION   
In Powerbasic you will call: "A$=Get_Last_A()" to get the String from Purebasic.

While the steps outlined so far have their complexities, they are not the most challenging aspect of this process. The real test of your skills comes when you want to send a string array from PowerBasic to PureBasic.

Code: Select all

; This is in the Purebasic DLL 
; This Array will be filled with the Content from thwe Powerbasic String-Array
Global.s GArray()

ProcedureDLL TransferStringArray(arrayPointer.i, arraySize.l,U03.l)
  Protected i = 0, start = arrayPointer, current = arrayPointer, tempString.s
  ReDim_GArray(U03)
  GDim=U03
  While current < start + arraySize-1
    If PeekB(current) = 0 ; Null terminator found
      GArray(i) = tempString
      tempString = ""
      i + 1
    Else
      tempString + Chr(PeekB(current))
    EndIf
    current + 1
  Wend
; Zum Anzeigen ob das Array angekommen ist  
CompilerIf 0
  For i = 0 To GDim ;ArraySize(GArray()) - 1
    Debug(GArray(i))
  Next
CompilerEndIf
EndProcedure

Code: Select all

; This is the Code on the Powerbasic Side
' U01 - STRPTR()
' U02 - Len()
' U03 - UBOUND()
'
DECLARE SUB TransferStringArray LIB "Pure.dll" ALIAS "TransferStringArray" (BYVAL arrayPointer AS LONG, BYVAL arraySize AS LONG,BYVAL U03 AS LONG)
' U01() - Array 0 ... U02-1
'
SUB Send_Array(BYREF U01() AS STRING)
    'DIM MyArray(0 TO 4) AS STRING
    REGISTER i AS LONG
    REGISTER j AS LONG
    LOCAL T01,T02,T03 AS LONG
    LOCAL S01 AS STRING
    ' Upper Array-Bound bestimmen
    T01=LBOUND(U01(1)):T02=UBOUND(U01(1))
    ' Concatenate all strings into a single string, with a null character as separator
    FOR i = T01 TO T02
        S01 += U01(i)+CHR$(0)
    NEXT
    ' Am Ende noch eine Extra-0
    S01 += CHR$(0)

    ' Call the DLL function
    j=LEN(S01)
    T03=T02-T01
    TransferStringArray(STRPTR(S01),j,T03)
END SUB              
In this manner, you can transmit a string array to PureBasic. As always, it's crucial to ensure that the string data does not contain any "zeroes". If it does, you might need to employ a different approach due to the "zero terminated strings" characteristic of PureBasic.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Bridging PowerBasic and a PureBasic DLL: A Detailed Guide on Interoperability

Post by Little John »

PowerBasc's string handling is more powerful (no pun intended) than PureBasic's string handling. So I don't see the point in doing string handling with a PureBasic DLL in a PowerBasic program. The other way around would make more sense.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Bridging PowerBasic and a PureBasic DLL: A Detailed Guide on Interoperability

Post by skywalk »

And, I did not know PowerBasic had an x64 version? Only x86(32-bit) last time I checked.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Bridging PowerBasic and a PureBasic DLL: A Detailed Guide on Interoperability

Post by Little John »

skywalk wrote: Thu Jul 13, 2023 4:39 pm And, I did not know PowerBasic had an x64 version? Only x86(32-bit) last time I checked.
Yes. Also PowerBasic is only for Windows, and the most ... erm ... “recent” version is from 2011.
Robert Zale, the creator of PowerBasic, died on November 6, 2012. And obviously since then there is no further developement of PowerBasic.
TheoG
New User
New User
Posts: 8
Joined: Fri Jun 16, 2023 7:04 pm

Re: Bridging PowerBasic and a PureBasic DLL: A Detailed Guide on Interoperability

Post by TheoG »

skywalk wrote: Thu Jul 13, 2023 4:39 pm And, I did not know PowerBasic had an x64 version? Only x86(32-bit) last time I checked.
May i quote from my text:
as PowerBasic does not support x64.
We should read articles before we comment.
Let me add that that we have two successors in development from 3rd parties, one is "Oxygen",
but its also a bit different from PB, while it is 32 and 64 bit.
But again, Purebasic with its complete set of libraries is for complex projects the easier alternative.

What i have expected instead were additions and suggestions how to improve the interoperability.
Not signs that people are too lazy to read :-). Please Retry.

About using Purebasic DLL's: It makes sense.
While you are right that the PB-Strings are more versatile usable, Purebasic has overtaken PB in terms of Libraries.
So depending on what you want to do, a Purebasic DLL will save you tons of typing.

Let me add that you can also use my article "how you can transfer strings from Purebasic.exe into a PB-DLL.".
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Bridging PowerBasic and a PureBasic DLL: A Detailed Guide on Interoperability

Post by skywalk »

Slow down TheoG,
I did read your longwinded post and you throw stones because I misread your mention of PB x64 DLL and interop with PowerBasic?
TheoG wrote:The Case for PureBasic DLLs in PowerBasic
There are compelling reasons to consider calling a PureBasic DLL from PowerBasic. Over time, PureBasic has surpassed PowerBasic in terms of library support, offering a wide range of functionalities with just a handful of commands.

Moreover, PureBasic supports x64,
...
In my opinion, you would be better served creating a PowerBasic --> PureBasic code converter.
Within a few months you could leave behind a decade old compiler for the latest. :wink:
I wrote one for Visual Basic 6 when PB v4.5 met my needs. It was very helpful even converting 80% of my code.

I agree PureBasic needs a better string lib and the C transpiler opens up several existing Fast String C libs.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
TheoG
New User
New User
Posts: 8
Joined: Fri Jun 16, 2023 7:04 pm

Re: Bridging PowerBasic and a PureBasic DLL: A Detailed Guide on Interoperability

Post by TheoG »

Hello all,

As someone who's been using PureBasic for a long time (back when the editor didn't even have an UNDO function!), I've found it to be an excellent choice for specific tasks. While I've never regretted my decision to invest in it, I must admit that I'm not thoroughly familiar with its syntax.

That's where GPT-4, the AI model developed by OpenAI, came into the picture for me. Over last weeks, I've used GPT-4 as a co-creator in developing most of my code in PureBasic. And i must say that it became better from week to week.
This AI-powered collaboration has opened my eyes to the potential of integrating AI in everyday coding practices.

One of the reasons I wanted to post about this experience was to engage with all of you to see if there are better solutions or approaches. I'm aware that there may be PureBasic commands I haven't considered or coding tricks that can make my work easier, faster, or more efficient.

So, I'm hoping to open up a dialogue here about AI-assisted coding and PureBasic best practices. Perhaps you've discovered some nifty PureBasic tips and tricks to interface with Powerbasic, and you also want to share?
Whatever your insights, I'm eager to learn and explore this exciting frontier of coding with you.

So, let's dive into the discussion and see how we can make the most of these technological advancements together. Looking forward to hearing your thoughts and insights!

PS: About the Strings, this would be very difficult possibly. Because how will you switch from generally "Null-terminated" to BStrings with all the same commands? And then the question is, someone who "comes from C" - as i think is Fred. He may not even see the need for these "Basic-like" Strings.
X-People have another way to handle binary data, they use ALLOC-Commands and such. They are familiar with this, they may not see the need for handling binary data in Strings. As someone may want to do who comes solely from BASIC and has never learned C-like languages.
So i would not hope on that unless he is really bored :-).
Post Reply