Solved

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

Posted on 2011-03-25
9
737 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

728 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