Solved

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

Posted on 2011-03-25
9
725 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

831 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