Trying to return a string from PB Compiled DLL to Excel

Just starting out? Need help? Post your questions and find answers here.
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Trying to return a string from PB Compiled DLL to Excel

Post by naw »

This is so frustrating, returning a number is easy, but I seem to be banging my head against a wall. Is it my PB Code or my Excel VBA?

PB Code:

Code: Select all

  Global RETSTR$
  
  ProcedureDLL.s PBTest(MYSTR.s)
    RETSTR$ = "Hello "+ MYSTR
    MessageRequester("PB Test", RETSTR$)
    ProcedureReturn RETSTR$
  EndProcedure
Excel VBA - Module1 - just returns a long number - presumably because I defined as "Long":

Code: Select all

Public Declare Function PBTest Lib "pbtest.dll" (ByVal TXT As String) As Long
If I define as a "String" Excel Crashes...

Code: Select all

Public Declare Function PBTest Lib "pbtest.dll" (ByVal TXT As String) As String
... Pleeeeease help ...
Ta - N
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Use this:

Code: Select all

ProcedureDLL.l PBTest(MYSTR.s)
  RETSTR$ = "Hello "+ MYSTR
  MessageRequester("PB Test", RETSTR$)
  ProcedureReturn SysAllocString_(RETSTR$)
EndProcedure 
I think, UNICODE is recommend
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post by naw »

@ts-soft

thanks - XL doesnt crash anymore - but now it returns this:

效汬N敧慗敬

When I Call the PB Function as an Excel Formula - eg:

Code: Select all

=PBTest("NAW")
But if I call the function from a VBA Button, then the output is fine - eg:

Code: Select all

Private Sub CommandButton1_Click()
  RET = PBTest("NAW")
  MsgBox RET
  Range("A1").Value = RET
End Sub
Ta - N
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post by naw »

Ok - Now Solved - But I dont know why...


If I call PBTest from a formula direcectly, it produces Chinese.
If I call PBTest from myPBTest, it works fine.

eg:

This returns "效汬TW"

Code: Select all

=PBTest("This produces chinese")

But This returns "Hello World"

Code: Select all

=myPBTest("World")

New VBA:

Code: Select all

Public Declare Function PBTest Lib _
       "c:\pbtest.dll" _
       (ByVal TXT As String) As String
       
Public Function myPBTest(ByVal TXT As String)
  myPBTest = PBTest(TXT)
End Function
[code]
Ta - N
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

Search for BSTR over here.
HarryO
User
User
Posts: 42
Joined: Wed May 07, 2003 4:25 am
Location: Palatine,IL.,USA

Here is what I had to do....

Post by HarryO »

Maybe this will help, here is what I had to do:

PB DLL Code:

Code: Select all

ProcedureDLL Get_2_Stuff()
	
	Z_String_01 = "Some Fun Now."
	
	ProcedureReturn @Z_String_01

EndProcedure
For the VBA portion (in Excel):

Code: Select all


Public Declare Function Get_2_Stuff Lib "Quotes_3_Lib01.dll" () As Long

Public Declare Function LStrCpy Lib "kernel32.dll" Alias "lstrcpy" (ByVal MyString As String, ByVal MyLong As Long) As Long

Public Target_String                'defined as variant
Public Tmp_String  As String * 99

Target_String = Space(99)       'adjust as necessary
Z_String_ptr = Get_2_Stuff()
Tmp_out = LStrCpy(Tmp_String, Z_String_ptr)
Z_String_Len = Len(Trim(Tmp_String))
Target_String = Left(Trim(Tmp_String), Z_String_Len - 1)

What I have found out (through this forum) is that PB will only return a long from a procedure.

Or more correctly, I have a pointer to be returned.
So what is returned is a pointer to the string.

This was originally compiled under 3.94 of PB and Excel 2000.
Still compiles and works under Excel 2003.

Hope this helps!

Harry0
Post Reply