Passing VB Arrays

Just starting out? Need help? Post your questions and find answers here.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post by helpy »

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
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

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 :cry:

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 
Is it possible to convert the code of this procedure in PB ??? :roll:

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 
 
ImageThe happiness is a road...
Not a destination
thearr
User
User
Posts: 21
Joined: Sun Apr 26, 2009 3:18 am
Location: RU

Post by thearr »

Kwaï chang caïne wrote:A kind programmer of VBForum give to me this solution in VB only.
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:
in VB:
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) 
or in PB for example like this (not tested):

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!
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Thanks thanks my savior THEARR 8)

I have test quickly your solution, and i believe that's works :D
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 8)

I wish you a good night
But already i thanks you very much for your gold help 8)
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Yes i have tested the code now, and obviously you are right :D
Now, i can import all my array thanks to you 8)

8 day of search and try all the method, for just one function :oops:

It's very difficult to put VB and PB in the same room :lol:
They don't eat the same food, don't watch the same program TV, no sleep at the same time :lol:

You never know, how your help is precious for me 8)
So i say to my son......who say later at his son......who is THEARR ......one of my heroes and savior of KCC :D

I wish you...a very very very good day.
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

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

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
Pb Code

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
ImageThe happiness is a road...
Not a destination
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

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:
Sounds like Unicode.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Hello DEMIVEC :D

You mean that the UNICODE is like that???
I don't understand anything :cry:

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 ????
ImageThe happiness is a road...
Not a destination
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

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 ????
I thought you had meant that there was a zero "byte" between each character.

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. :wink:
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Yes it's exactly that.

In the code above

Code: Select all

b = PeekB(MyArray(i) + o)
B, sometimes, in fact one time on two, is equal at 0 :shock:
And i don't know why ???

I search a method in VB for not send this 0 :roll:
But apparently it's not easy :cry:

I go to create a new thread....perhaps someone have an idea ??? :roll:
ImageThe happiness is a road...
Not a destination
thearr
User
User
Posts: 21
Joined: Sun Apr 26, 2009 3:18 am
Location: RU

Post by thearr »

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.
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
How did you declare the function and use it?

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 
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.
Ceterum censeo Carthaginem esse delendam!
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Thanks THEARR for your explanation 8)

I'm tired of this history of UNICODE/ANSI, 2 byte, 4 byte, STR, BSTR.... :cry:

Finally, i believe, i use the first method with the convert in the DLL.
It's more simple, and that's works.....:D
This is two reasons for use it :wink:

One thousand of thanks for your kindness, you have help me so much 8)
ImageThe happiness is a road...
Not a destination
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Post by doctorized »

I translate a code from VB 6.0 to PB 4.31.

Is there a way to translate:

"StrConv(ImgColor, vbUnicode)" with ImgColor byte array?
Post Reply