Passing VB Arrays
some more information:
=> http://www.roblocher.com/whitepapers/oletypes.aspx
seems to be a good documentation about safearrays!
and more information from Microsoft:
=> http://support.microsoft.com/kb/106553/en-us
=> http://support.microsoft.com/kb/118643/en-us
cu, helpy
			
			
									
									=> http://www.roblocher.com/whitepapers/oletypes.aspx
seems to be a good documentation about safearrays!
and more information from Microsoft:
=> http://support.microsoft.com/kb/106553/en-us
=> http://support.microsoft.com/kb/118643/en-us
cu, helpy
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
						PB Last Final / Last Beta Testing
- Kwai chang caine
 - Always Here

 - Posts: 5502
 - Joined: Sun Nov 05, 2006 11:42 pm
 - Location: Lyon - France
 
A kind programmer of VBForum give to me this solution in VB only.
He can't give me more, because he don't have PB
 
VB code
Is it possible to convert the code of this procedure in PB ???  :roll: 
VB code to try to translate in PB
			
			
									
									He can't give me more, because he don't have PB
VB code
Code: Select all
Private Declare Sub  CopyMemory  Lib  "kernel32"  Alias  "RtlMoveMemory"  (pDst  As Any , pSrc  As Any ,  ByVal  ByteLen As Long ) 
Private Sub  Command1_Click() 
    Dim xs(2) As String 
    xs(0) = "Ligne 1" 
    xs(1) = "Ligne 2" 
    xs(2) = "Ligne 3" 
    
    'appel méthode magique simulant la dll PB 
    CloneStringArray VarPtr(xs(0)), UBound(xs) 
End Sub VB code to try to translate in PB
Code: Select all
Sub CloneStringArray(ByVal lhPointer As Long, ByVal lSize As Long) 
    Dim saNew() As String 
    ReDim saNew(lSize) 
    
    CopyMemory ByVal VarPtr(saNew(0)), ByVal lhPointer, 12 
    
    'test 
    Dim i As Integer 
    For i = LBound(saNew) To UBound(saNew) 
        Debug.Print i & ". '" & saNew(i) & "'" 
    Next i 
End Sub 
 
The happiness is a road...Not a destination
This won't help you. Your problem is you'd like to pass a Unicode string from VB (BSTR) to DLL, compiled in ANSI mode. So all you have to do is to convert Unicode to ANSI no matter where:Kwaï chang caïne wrote:A kind programmer of VBForum give to me this solution in VB only.
in VB:
or in PB for example like this (not tested):thearr wrote:PostPosted: Thu Jun 04, 2009 12:03 am Post subject:
![]()
Code: Select all
MyArrayVB(0) = StrConv("one", vbFromUnicode) MyArrayVB(1) = StrConv("two", vbFromUnicode)
Code: Select all
Procedure.s PeekBSTR(*BSTR)
  lenBSTR=PeekL(*BSTR-4)/2
  Dim Char.c(lenBSTR)
  For i=0 To lenBSTR
    Char(i)=PeekC(*BSTR+i*2)
  Next i
  ProcedureReturn PeekS(@Char(0))
EndProcedure
;Change MessageRequester(PeekS(MyArray(0)), PeekS(MyArray(1))) to
;MessageRequester(PeekBSTR(MyArray(0)), PeekBSTR(MyArray(1))) 
Ceterum censeo Carthaginem esse delendam!
						- Kwai chang caine
 - Always Here

 - Posts: 5502
 - Joined: Sun Nov 05, 2006 11:42 pm
 - Location: Lyon - France
 
Thanks thanks my savior THEARR  
I have test quickly your solution, and i believe that's works
 
But it's too late in france for build all the code and tomorrow i go to work in five hour, and i have not again eat
So, tomorrow i build all the code and say you the result
I wish you a good night
But already i thanks you very much for your gold help
			
			
									
									I have test quickly your solution, and i believe that's works
But it's too late in france for build all the code and tomorrow i go to work in five hour, and i have not again eat
So, tomorrow i build all the code and say you the result
I wish you a good night
But already i thanks you very much for your gold help
The happiness is a road...Not a destination
- Kwai chang caine
 - Always Here

 - Posts: 5502
 - Joined: Sun Nov 05, 2006 11:42 pm
 - Location: Lyon - France
 
Yes i have tested the code now, and obviously you are right  
Now, i can import all my array thanks to you
8 day of search and try all the method, for just one function
It's very difficult to put VB and PB in the same room
They don't eat the same food, don't watch the same program TV, no sleep at the same time
You never know, how your help is precious for me
So i say to my son......who say later at his son......who is THEARR ......one of my heroes and savior of KCC
 
I wish you...a very very very good day.
			
			
									
									Now, i can import all my array thanks to you
8 day of search and try all the method, for just one function
It's very difficult to put VB and PB in the same room
They don't eat the same food, don't watch the same program TV, no sleep at the same time
You never know, how your help is precious for me
So i say to my son......who say later at his son......who is THEARR ......one of my heroes and savior of KCC
I wish you...a very very very good day.
The happiness is a road...Not a destination
- Kwai chang caine
 - Always Here

 - Posts: 5502
 - Joined: Sun Nov 05, 2006 11:42 pm
 - Location: Lyon - France
 
I know i know....you say KCC is very a mule  
 
But for good understand, i try to convert the BSTR in the VB and not in the PB.
That's works nearly fine.
I don't know why there are a chr(0) under each bit in VB ????
Then i have delete it, and find, that the record of each array, finish by two chr(0)
What is your mind about this ??? :roll:
VB code
Pb Code
			
			
									
									But for good understand, i try to convert the BSTR in the VB and not in the PB.
That's works nearly fine.
I don't know why there are a chr(0) under each bit in VB ????
Then i have delete it, and find, that the record of each array, finish by two chr(0)
What is your mind about this ??? :roll:
VB code
Code: Select all
Private Declare Function FenetreTexte Lib "MyDll.dll" (Titre As String, Texte As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As _
    Any, source As Any, ByVal bytes As Long)
Function PointerFromBSTR(ByVal pointer As Long) As Long
     Dim temp As String, Longueur As String
    ' copy the pointer into the temporary string's BSTR
    CopyMemory ByVal VarPtr(temp), pointer, 4
    ' now Temp points to the original string, so we can copy it
    PointerFromBSTR = StrPtr(temp)
    ' manually clear then temporary string to avoid GPFs
    CopyMemory ByVal VarPtr(temp), 0&, 4
End Function
Private Sub Form_Load()
 Dim TabloVB(15) As String
 TabloVB(1) = "Hello...i'm KCC"
 TabloVB(2) = "i'm locked in VB :-("
 TabloVB(3) = "and i want to jump in PB :-)"
 TabloVB(4) = " "
 
 For i = 5 To 14
  TabloVB(i) = "Line " + Str(i)
 Next
Code: Select all
ProcedureDLL FenetreTexte(Titre.s, *AdresseTablo)
  
 Dim MyArray.l(1)
 SizeOfArray.l = PeekL(*AdresseTablo)
 ReDim MyArray.l(SizeOfArray)
 CopyMemory(*AdresseTablo, @MyArray(), SizeOfArray * 4) 
 
 For i = 1 To SizeOfArray - 1
  
  Mot$ = ""
  Zero = 0
  
  For o = 0 To 1000
   
   b = PeekB(MyArray(i) + o)
  
   If b <> 0
    Mot$ + Chr(b)
    Zero = 0
   ElseIf b = 13 Or zero = 2
    Break
   Else 
    Zero + 1 
   EndIf 
   
  Next 
  
  MessageRequester("C'est magique, m'vl'a dans la DLL", Mot$)
 Next 
  
EndProcedure
The happiness is a road...Not a destination
Sounds like Unicode.Kwaï chang caïne wrote:I know i know....you say KCC is very a mule![]()
But for good understand, i try to convert the BSTR in the VB and not in the PB.
That's works nearly fine.
I don't know why there are a chr(0) under each bit in VB ????
Then i have delete it, and find, that the record of each array, finish by two chr(0)
What is your mind about this ??? :roll:
- Kwai chang caine
 - Always Here

 - Posts: 5502
 - Joined: Sun Nov 05, 2006 11:42 pm
 - Location: Lyon - France
 
I thought you had meant that there was a zero "byte" between each character.Kwaï chang caïne wrote:I have tested one of a element of the array, in VB, with the API IsTextUnicode and the return is 0.
So why y have in the DLL a chr(0) between each bit ????
If you have tested it, and it was reported as "not unicode," then I guess it must be something else. Alas, it was my best quess.
- Kwai chang caine
 - Always Here

 - Posts: 5502
 - Joined: Sun Nov 05, 2006 11:42 pm
 - Location: Lyon - France
 
Yes it's exactly that.
In the code above
B, sometimes, in fact one time on two, is equal at 0  
And i don't know why ???
I search a method in VB for not send this 0 :roll:
But apparently it's not easy
 
I go to create a new thread....perhaps someone have an idea ??? :roll:
			
			
									
									In the code above
Code: Select all
b = PeekB(MyArray(i) + o)And i don't know why ???
I search a method in VB for not send this 0 :roll:
But apparently it's not easy
I go to create a new thread....perhaps someone have an idea ??? :roll:
The happiness is a road...Not a destination
1. BSTR that is used in VB is always a Unicode string (each symbol is coded with 2 bytes). It also has 2 bytes of zeros at the end of the string for compatibility with WinAPI functions, that usually use null-terminated strings (PB uses them as well).
2.
3. If you'd like to convert strings to ANSI in VB, did you try internal VB functions the way I've posted:
4. By the way your wish to convert initially Unicode strings to ANSI makes your program limited, as users may have problems with certain symbols, for ex. with Russian alphabet.
			
			
									
									2.
How did you declare the function and use it?Kwaï chang caïne wrote:I have tested one of a element of the array, in VB, with the API IsTextUnicode and the return is 0
3. If you'd like to convert strings to ANSI in VB, did you try internal VB functions the way I've posted:
Code: Select all
 Dim TabloVB(15) As String
 TabloVB(1) = "Hello...i'm KCC"
 TabloVB(2) = "i'm locked in VB :-("
 TabloVB(3) = "and i want to jump in PB :-)"
 TabloVB(4) = " "
 
 For i = 5 To 14
  TabloVB(i) = "Line " + Str(i)
 Next 
 '**********
 For i = 1 to 14
  TabloVB(i) = StrConv(TabloVB(i), vbFromUnicode) 
 Next Ceterum censeo Carthaginem esse delendam!
						- Kwai chang caine
 - Always Here

 - Posts: 5502
 - Joined: Sun Nov 05, 2006 11:42 pm
 - Location: Lyon - France
 
Thanks THEARR for your explanation  
 
I'm tired of this history of UNICODE/ANSI, 2 byte, 4 byte, STR, BSTR....
 
Finally, i believe, i use the first method with the convert in the DLL.
It's more simple, and that's works.....
 
This is two reasons for use it
One thousand of thanks for your kindness, you have help me so much
			
			
									
									I'm tired of this history of UNICODE/ANSI, 2 byte, 4 byte, STR, BSTR....
Finally, i believe, i use the first method with the convert in the DLL.
It's more simple, and that's works.....
This is two reasons for use it
One thousand of thanks for your kindness, you have help me so much
The happiness is a road...Not a destination
- doctorized
 - Addict

 - Posts: 882
 - Joined: Fri Mar 27, 2009 9:41 am
 - Location: Athens, Greece
 

