Author Topic: bc9Basic and PowerBasic  (Read 3076 times)

0 Members and 1 Guest are viewing this topic.

Offline Eros Olmi

  • Sr. Member
  • ****
  • Posts: 380
  • Gender: Male
    • thinBasic
bc9Basic and PowerBasic
« on: April 12, 2016, 09:06:28 PM »
Hi James,

I would like to try to use bc9Basic to develop DLL functions passing strings in/out to PowerBasic executables using PB Strings (BSTR)
Do you have any example I can use as a start in order to understand how to do it?

Thanks a lot.
Eros
« Last Edit: April 14, 2016, 02:08:59 PM by James C. Fuller »
thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 673
Re: bc9Basic and PowerBasic
« Reply #1 on: April 12, 2016, 10:21:13 PM »
Eros,
  BSTR's can be a bit of a pia :)
What c/c++ compiler will you be using?
What calling convention for the dll functions?

James
« Last Edit: April 14, 2016, 02:09:18 PM by James C. Fuller »

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 673
Re: bc9Basic and PowerBasic
« Reply #2 on: April 13, 2016, 12:25:52 AM »
Eros,
  Here is an example.
Note it is compiled as a c++ ($CPPHDR) dll so you have access to all the wonders of c++ and the STL.
Plus it seems it needs to be for the undecorated names to be created??
The dll was created with visual studio 2015 community.
James


bc9Basic Dll
Code: [Select]
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Eros BSTR test
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$DLL STDCALL
$ONEXIT "VSCPP.BAT $FILE$ -m32 dll Release"
Function MyDllFunction(strIn As char Ptr ) As BOOL EXPORT
Print strIn
Print "Hello from MyDllFunction"
Function = TRUE
End Function
'==============================================================================
Function GetTheString() As BSTR EXPORT
Dim S$
S$ = "Hello From the Dll"
Function = SysAllocStringByteLen(S$,Len(S$))
End Function
'==============================================================================

PowerBasic PBCC Test code.
Code: [Select]
#COMPILE EXE
#DIM ALL
DECLARE FUNCTION MyDllFunction LIB "Test2.Dll" ALIAS "MyDllFunction"(BYVAL S1 AS STRING) AS LONG
DECLARE FUNCTION GetTheString LIB "Test2.Dll" ALIAS "GetTheString"() AS STRING
FUNCTION PBMAIN () AS LONG
    DIM S1 AS STRING
    DIM RetVal AS LONG
    S1 = "James"
    RetVal = MyDllFunction(S1)
    PRINT "RetVal = ";RetVal
    S1 =  GetTheString()
    PRINT S1
END FUNCTION
« Last Edit: April 14, 2016, 02:09:35 PM by James C. Fuller »

Offline Eros Olmi

  • Sr. Member
  • ****
  • Posts: 380
  • Gender: Male
    • thinBasic
Re: bc9Basic and PowerBasic
« Reply #3 on: April 13, 2016, 06:48:53 AM »
Hi James,

thanks a lot. I just needed something to start.
Yes, calling convention is standard, VS2015 community is perfect.

My idea is to use bc9Basic to develop thinBasic modules  (special DLLs) using thinBasic SDK interface. thinBasic is developed using PowerBasic so I needed an example on how to manage PB Strings in bc9Basic.
I already have possibility to use FreeBasic and FreePascal to develop thinBasic modules, I just wanted to give my user base another option.

I will experiment in next days and see what I can do.

Thanks again.
Eros
« Last Edit: April 14, 2016, 02:09:57 PM by James C. Fuller »
thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 673
Re: bc9Basic and PowerBasic
« Reply #4 on: April 13, 2016, 04:34:17 PM »
Eros,
  Note, you must use my VSCPP.BAT file to get undecorated dll functions.

James
« Last Edit: April 14, 2016, 02:10:14 PM by James C. Fuller »

Offline Eros Olmi

  • Sr. Member
  • ****
  • Posts: 380
  • Gender: Male
    • thinBasic
Re: bc9Basic and PowerBasic
« Reply #5 on: April 14, 2016, 06:23:12 AM »
Thanks James,

I was able to compiler my first DLL.

I need to know if it is possible to have Pointer to a Function.
The equivalent in PowerBasic is CODEPTR

Thanks a lot
Eros
« Last Edit: April 14, 2016, 02:11:55 PM by James C. Fuller »
thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

Offline Eros Olmi

  • Sr. Member
  • ****
  • Posts: 380
  • Gender: Male
    • thinBasic
Re: bc9Basic and PowerBasic
« Reply #6 on: April 14, 2016, 06:57:35 AM »
OK, I should have found: & in front of the function name.
Something like: &MyFunctionName

Is it correct?

« Last Edit: April 14, 2016, 02:10:28 PM by James C. Fuller »
thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 673
Re: bc9Basic and PowerBasic
« Reply #7 on: April 14, 2016, 01:15:15 PM »
Eros,
  A bit different because we are not EXPORTing the calllback so we need to specify STDCALL for the callback.
The $DLL STDCALL defaults to EXPORTed procedures only.
James

bc9Basic  test3.dll
Code: [Select]
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Eros BSTR test
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$DLL STDCALL
$ONEXIT "VSCPP.BAT $FILE$ -m32 dll Release"
'==============================================================================
Sub CallbackSub() STDCALL
    Print "Hello From CallbackSub"
End Sub
'==============================================================================
Function MyDllFunction(strIn As char Ptr ) As BOOL EXPORT
    Print strIn
    Print "Hello from MyDllFunction"
    Function = TRUE
End Function
'==============================================================================
Function GetTheString() As BSTR EXPORT
    Dim S$
    S$ = "Hello From the Dll"
    Function = SysAllocStringByteLen(S$,Len(S$))
End Function
'==============================================================================
Function GetTheCallback() As DWORD EXPORT
    Function = (DWORD)&CallbackSub
End Function
'==============================================================================
PowerBASIC
Code: [Select]
#COMPILE EXE
#DIM ALL
DECLARE FUNCTION MyDllFunction LIB "Test3.Dll" ALIAS "MyDllFunction"(BYVAL S1 AS STRING) AS LONG
DECLARE FUNCTION GetTheString LIB "Test3.Dll" ALIAS "GetTheString"() AS STRING
DECLARE FUNCTION GetTheCallback LIB "Test3.DLL" ALIAS "GetTheCallback"() AS DWORD
FUNCTION PBMAIN () AS LONG
    DIM S1 AS STRING
    DIM RetVal AS LONG
    DIM dwCallBack As DWORD
    S1 = "James"
    RetVal = MyDllFunction(S1)
    PRINT "RetVal = ";RetVal
    S1 =  GetTheString()
    PRINT S1
    dwCallBack = GetTheCallback
    CALL DWORD dwCallBack
END FUNCTION

« Last Edit: April 14, 2016, 02:10:40 PM by James C. Fuller »

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 673
Re: bc9Basic and PowerBasic
« Reply #8 on: April 14, 2016, 02:08:27 PM »
Eros,
  You also can call pb subs/functions from the dll.
James

bc9Basic  test4.dll
Code: [Select]
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Eros BSTR test
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$DLL STDCALL
$ONEXIT "VSCPP.BAT $FILE$ -m32 dll Release"
'==============================================================================
'you need to define your pb sub/function like this
'this one is a Sub(void) with no paramaters
$TYPEDEF void (__stdcall *foo)()
' another example:  we call a function that returns an int( pb long) passing a
' long(int) and a fixed length string
'$TYPEDEF int (__stdcall *foo1)(int,char*)
'==============================================================================
Sub CallbackSub() STDCALL
    Print "Hello From CallbackSub"
End Sub
'==============================================================================
Function MyDllFunction(strIn As char Ptr ) As BOOL EXPORT
    Print strIn
    Print "Hello from MyDllFunction"
    Function = TRUE
End Function
'==============================================================================
Function GetTheString() As BSTR EXPORT
    Dim S$
    S$ = "Hello From the Dll"
    Function = SysAllocStringByteLen(S$,Len(S$))
End Function
'==============================================================================
Function GetTheCallback() As DWORD EXPORT
    Function = (DWORD)&CallbackSub
End Function
'==============================================================================
Sub CallPbFunc(fooptr As foo) EXPORT
    fooptr()
End Sub
'==============================================================================
PowerBASIC
Code: [Select]
#COMPILE EXE
#DIM ALL
DECLARE FUNCTION MyDllFunction LIB "Test4.Dll" ALIAS "MyDllFunction"(BYVAL S1 AS STRING) AS LONG
DECLARE FUNCTION GetTheString LIB "Test4.Dll" ALIAS "GetTheString"() AS STRING
DECLARE FUNCTION GetTheCallback LIB "Test4.DLL" ALIAS "GetTheCallback"() AS DWORD
DECLARE Sub CallPbFunc LIB "Test4.DLL" ALIAS "CallPbFunc"(BYVAL DWORD)
'==============================================================================
Sub PbCallBack()
    Print "Here I am in the PbCallBack Sub"
End Sub
'==============================================================================
FUNCTION PBMAIN () AS LONG
    DIM S1 AS STRING
    DIM RetVal AS LONG
    DIM dwCallBack As DWORD
    S1 = "James"
    RetVal = MyDllFunction(S1)
    PRINT "RetVal = ";RetVal
    S1 =  GetTheString()
    PRINT S1
    dwCallBack = GetTheCallback
    CALL DWORD dwCallBack
    CallPbFunc(CODEPTR(PbCallBack))
END FUNCTION

« Last Edit: April 14, 2016, 02:10:55 PM by James C. Fuller »

Offline Eros Olmi

  • Sr. Member
  • ****
  • Posts: 380
  • Gender: Male
    • thinBasic
Re: bc9Basic and PowerBasic
« Reply #9 on: April 14, 2016, 08:37:47 PM »
Thanks a lot for those other explanations.

I've already done some tests on creating thinBasic modules with BC9 and it seems I'm able to do what I want.
During the weekend I will experiment more deeply but I'm quite happy about possibilities.

Thanks a lot
Eros
thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB