?
Solved

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

Posted on 2011-03-25
9
Medium Priority
?
785 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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
 

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 2000 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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

864 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