Link to home
Start Free TrialLog in
Avatar of mpdillon
mpdillon

asked on

Passing a BYTE array from a VB6 DLL to a Visual Basic 2008 program

I need to convert a VB String Data type to a VB Byte array. This works great in VB6 but I am struggling in VB 2008. I have another post addressing this issue (https://www.experts-exchange.com/questions/26901478/StrConv-equivilent-in-Visual-Basic-2008.html?anchorAnswerId=35214746#a35214746)

if I am unsuccessful there my fall back plan is to create a VB6 DLL that does the conversion and passes the resultant BYTE array back to VB 2008.

I have created the DLL in VB6 (see attached code). I have referenced this DLL in my VB 2008 project. However, when I try to assign the VB6 Byte array to the VB 2008 Byte Array I receive the following error message:
"Value of type 'Byte' cannot be converted to a '1-dimensional array of Byte'

How do I pass an Byte array from VB 6 to VB 2008? Is my VB6 Class constructed properly to do this?
thanks,
pat
Option Explicit

Public Property Get s() As String
s = pstrS
End Property
Public Property Let s(ByVal strS As String)
pstrS = strS
End Property
Public Property Get ba() As Byte
ba = pba
End Property
Public Property Let ba(ByVal pba As Byte)
pba = ba
End Property
Public Sub ConvertStoBa()
pba = StrConv(pstrS, vbFromUnicode)

End Sub

Open in new window

Avatar of MedievalWarrior
MedievalWarrior
Flag of United States of America image

You may use the Array Class
Dim myBytes As Array = classInstance.ba

Open in new window

BTW: You don't need to create DLL just to use StrConv() in .NET ... Can you post all of the VB6 code that needs to be converted to VB.NET
Avatar of mpdillon
mpdillon

ASKER

MedievalWarrior:
I am thought Microsoft.VisualBasic.StrConv will work just fine. But vbFromUnicode is not an option.

The only code I am having trouble with is, ba = StrConv(s, vbFromUnicode). Actually there is a corollary which would be a second piece of code. I would need to convert a Byte array to a Unicode string for storage.
s = StrConv(ba(),vbUnicode).

Also, I really do not understand the basics. Could you please elaborate on using an array class. Is this in the VB6 Dll or in the Visual Basic 2008 project?
thanks,
pat
StrConv() just converts the String in VB6 to a byte array. The below example is the same so can you post example of your problem here?
Dim myString As String = "Hello World"
Dim myBytes() As Byte = System.Text.UnicodeEncoding.Default.GetBytes(myString)
Dim myStringFromBytes As String = System.Text.UnicodeEncoding.Default.GetString(myBytes)

Open in new window

I have attached the two code sets.

I tried the UnicodeEncoding section but that didn't alter the results.


txtctl-vb6-pgm.txt
txtctl-vb2008-pgm.txt
I would be glad to pass you the raw data also but I do not know what to best way to accomplish that would be.
ASKER CERTIFIED SOLUTION
Avatar of MedievalWarrior
MedievalWarrior
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
MedievalWarrior:
You GOT IT!!!!! The 1252 encoding works perfectly. I can read and write the string to a byte array. I will pass your solution along to their Tech dept so no one else has to search for this.

I have to be honest and tell you that I do not understand code pages. The data is stored in a TEXT field type in SQL server 2000. The data originates in a third party control for editing text. The output of that control is a Byte array. In 2004 when this project began, one of the requirements was to store the documents in SQL Server. I was told to convert the Byte array output to a string and store it in a Text field data type. Today, there are about 100 programs that use that paradigm.

I wanted to start moving away from VB 6 to a .Net product. This was the first project to do that. I would never had guessed that the Tech Dept would have been stumped by this question.

As to why I was using a file to compare, I didn't know of another way to visualize what was going on. I have tried several encoding methods. I would always get an error when passing to the control but I wanted to know why I was getting an error. I thought by creating text files I might spot some difference. Maybe there would be a misplaced NULL or something. I had no idea the data wouldn't even be close.

All is well that ends well. Thank you so much for staying with me on this.
pat

Perfect!