Thanks people, for the code suggestions so far. Two approaches:
1. With the p-unicode pseudotype
PureBasic-code
Code: Select all
; Name: IS2.pb
; dll name: IS2.dll
; Test with p-inicode
; Compiler options: Create Unicode executeable
Prototype.l InitString2(name.p-unicode)
ProcedureDLL.l InitString2(name.s)
MessageRequester("Message from IS2.dll",name)
ProcedureReturn 1
EndProcedure
VBA-Code in Word
Code: Select all
Declare Function InitString2 Lib "IS2.dll" (name As String) As Long
Public Sub inits1()
a = InitString2("Name")
End Sub
Result: Same gibberish as before.
2. With the Binary string approach.
Code: Select all
; Name: IS.pb
; dll name: IS.dll
; Test with binary string approach
; Compiler options: Create Unicode executeable: off
Procedure.s ReadBSTR(bstr) ; by Fr34k
length = WideCharToMultiByte_(#CP_ACP, 0, bstr, -1, 0, 0, 0, 0)
Text$ = Space(length)
WideCharToMultiByte_(#CP_ACP, 0, bstr, -1, @Text$, length, 0, 0)
ProcedureReturn Text$
EndProcedure
ProcedureDLL.l InitString(*pbstr)
MessageRequester("",ReadBSTR(*pbstr) )
ProcedureReturn 1
EndProcedure
VBA-Code in Word I use to call this suggested code.
Code: Select all
Declare Function InitString Lib "IS.dll" (name As String) As Long
Public Sub inits2()
a = InitString("Name")
End Sub
Result: Gibberish
Tested the last also with:
Code: Select all
Declare Function InitString Lib "IS.dll" (name As Any) As Long
Another approach 3
The next is a (incomplete) code snippet I once adapted for named pipe communication between Word and Excel.
Here I translated the string first from Unicode into ASCII with the StrConv() function in VBA and then passed it to the Write() function.
I dimmed SendBuffer:
This is VBA-code:
Code: Select all
res(0) = ConnectNamedPipe(hPipe, ByVal 0)
res(1) = ReadFile(hPipe, RecieveBuffer(0), _
UBound(RecieveBuffer), _
lpNumberOfBytesRead, ByVal 0)
Debug.Print Err.LastDllError
'Get the string out of the byte array.
For i = 0 To lpNumberOfBytesRead - 1
temp = temp & Chr(RecieveBuffer(i))
Next i
'And put it on the textbox.
Me.TbRecieve.Text = temp
temp = UserForm2.TbSend
SendBuffer = StrConv(temp, vbFromUnicode)
'Redim the SendBuffer and fill the rest of the buffer
'with nullchars.
ReDim Preserve SendBuffer(BUFFSIZE)
nNumberOfBytesToWrite = UBound(SendBuffer)
lpNumberOfBytesWritten = nNumberOfBytesToWrite
'Write the number of bytes requested
res(2) = WriteFile(hPipe, SendBuffer(0), UBound(SendBuffer), _
lpNumberOfBytesWritten, ByVal 0)
End of VBA-code.
If you look at the WriteFile API function, the SendBuffer(0) is dimmed as "lpBuffer as Any".
Code: Select all
Declare Function ReadFile Lib "kernel32" ( _
ByVal hFile As Long, _
lpBuffer As Any, _
ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, _
lpOverlapped As Any) As Long
If I use this approach I have to pass a pointer of the ASCII array to PureBasic. How do I pick this up in PureBasic?
Give me books, fruit, french wine, fine weather and a little music.
John Keats