Solved

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

Posted on 2011-03-25
9
719 Views
Last Modified: 2012-05-11
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 (http://www.experts-exchange.com/Programming/Languages/.NET/Q_26901478.html#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

0
Comment
Question by:mpdillon
  • 5
  • 4
9 Comments
 
LVL 5

Expert Comment

by:MedievalWarrior
ID: 35215122
You may use the Array Class
Dim myBytes As Array = classInstance.ba

Open in new window

0
 
LVL 5

Expert Comment

by:MedievalWarrior
ID: 35215145
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
0
 

Author Comment

by:mpdillon
ID: 35215379
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
0
 
LVL 5

Expert Comment

by:MedievalWarrior
ID: 35215609
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

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:mpdillon
ID: 35216041
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
0
 

Author Comment

by:mpdillon
ID: 35216049
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.
0
 
LVL 5

Accepted Solution

by:
MedievalWarrior earned 500 total points
ID: 35217725
I'm lost because the example should be working as StrConv just uses the default code page. What happens when you try this code instead. You are validating this against SQL right? I don't know the reason why you are using a file to compare results

' The same as using StrConv("string",vbFromUnicode)

dim myBytes() as Byte = System.Text.Encoding.GetEncoding(1252).GetBytes("Hello World")

Open in new window

0
 

Author Comment

by:mpdillon
ID: 35221829
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

0
 

Author Closing Comment

by:mpdillon
ID: 35221832
Perfect!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now