Page 1 of 1
Trying to return a string from PB Compiled DLL to Excel
Posted: Thu Jul 19, 2007 4:08 pm
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 ...
Posted: Thu Jul 19, 2007 4:26 pm
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
Posted: Thu Jul 19, 2007 4:47 pm
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:
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
Posted: Thu Jul 19, 2007 5:51 pm
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"
But This returns "Hello 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]
Posted: Thu Jul 19, 2007 8:48 pm
by Edwin Knoppert
Search for BSTR over here.
Here is what I had to do....
Posted: Sat Jul 21, 2007 12:34 pm
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