Solved

String Handling in VB .Net

Posted on 2004-09-30
26
400 Views
Last Modified: 2008-01-09
I have a fine working code in VB6 which goes like this:


        Dim strPhoto As String
        strPhoto = Space(4000)
        nResult = Read_Photo(strPhoto)
       
which the strPhoto reads out an image file contents in binary format.
Note: Read_Photo is an API call.

as when I convert this to VB .Net, the strPhoto reading can't take in the correct binary format.
any idea? Pls advise.
0
Comment
Question by:ianouii
  • 10
  • 8
  • 4
  • +2
26 Comments
 
LVL 5

Expert Comment

by:webtrans
ID: 12188528
how to read binary file in .net is as below
http://www.dotnet4all.com/Dot-Net-Books/2004/08/read-binary-file.html
0
 
LVL 5

Author Comment

by:ianouii
ID: 12188547
I think u misunderstood. The API calls to return the binary string is a fixed function where I can't change that....

Rite now, I need the solution to manipulate the string in VB .Net just like how it react in VB6.

tqvm.
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12188957
Hi ,
what kind of argument Read_Photo takes. just string ?
What Read_Photo(strPhoto) is giving you ?

-Baan
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 28

Expert Comment

by:iboutchkine
ID: 12189101
DId you change your API for using in .NET

Long will become Integer
Any will become Object
0
 
LVL 5

Author Comment

by:ianouii
ID: 12191547
Read_Photo(string) is a fixed API which is directly given by the hardware provider where we can't change anything to it. It's only expecting string input.

the string will be filled up with all the binary info once the Read_Photo function successfully executed.

tqvm.
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12191591
So, when you do this...  nResult = Read_Photo(strPhoto)
What's it giving you ?
-Baan
0
 
LVL 5

Author Comment

by:ianouii
ID: 12191622
the nResult will just return successful code.

but the strPhoto will return big junk of binary info of a jpg format photos.

I'll then need to put that into a file by using the fileopen command in VB .net
let's concentrate in getting the string done to get the proper info.

tqvm.
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12191676
So you are concerned about "strPhoto = Space(4000)" , right ?
-Baan
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12191765
Do you think the problem is with the size ? try making the size bigger, lets see what happens.
0
 
LVL 5

Author Comment

by:ianouii
ID: 12197625
I've made the space(500000)
still, it doesn't work.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12200303
I think what Iouri was alluding to, what is your API defined as:

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12200310
Long in VB.NET is a 64-bit number, while Integer is a 32-bit number.  In defining the API declaration, you need to keep that in mind.

Bob
0
 
LVL 5

Author Comment

by:ianouii
ID: 12201342
It has nothing with the nResult.
The API declared can't be changed as it's provided by the hardware manufacturer.
The driver provider is set to take string input only.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12201571
Okay, let's try it one more time.  Can you post your API declaration for us to see (both the VB6 and VB.NET declarations), please?

Thanks,
Bob
0
 
LVL 5

Author Comment

by:ianouii
ID: 12201625
VB6.
Declare Function Read_Photo Lib "gsdk.dll" Alias "_Read_Photo@4" (ByVal pbPhoto As String) As Integer

VB .Net
Declare Function Read_Photo Lib "gsdk.dll" Alias "_Read_Photo@4" (ByVal pbPhoto As String) As Integer
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12201662
Yes, in this case there is nothing to change. But if it were declared as Long, you shoud've declared it as integer
0
 
LVL 5

Author Comment

by:ianouii
ID: 12201739
any resolution?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12201753
Actually, what kind of string does ReadPhoto take?
0
 
LVL 5

Author Comment

by:ianouii
ID: 12201762
just a normal string....
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12201779
You might need to use the MarshalAsAttribute:

<MarshalAs(UnmanagedType.LPWStr)>

Declare Auto Function Read_Photo Lib "gsdk.dll" Alias "_Read_Photo@4" (ByVal pbPhoto As <MarshalAs(UnmanagedType.LPWStr)>String) As Integer

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12201785
What kind of class it gsdk.dll?

Bob
0
 
LVL 5

Author Comment

by:ianouii
ID: 12201804
pls explain further over what're the info u wanna obtain?

the gsdk.dll exposed the API calls for Read_Photo...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12201827
Is gsdk.dll a C++ program, VB6 dll, ...?

Bob
0
 
LVL 5

Author Comment

by:ianouii
ID: 12201837
I think it's a C++ program.
not very sure about this.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 12201877
Saying it is a "normal" string is not exactly correct, especially with C.  If you looked at the UnmanagedType enumerations:

BStr:
A Unicode character string that is a length-prefixed double byte. You can use this member, which is the default string in COM, on the String data type.

ByValTStr:
Used for in-line, fixed-length character arrays that appear within a structure. The character type used with ByValTStr is determined by the System.Runtime.InteropServices.CharSet argument of the System.Runtime.InteropServices.StructLayoutAttribute applied to the containing structure. Always use the MarshalAsAttribute.SizeConst field to indicate the size of the array.
.NET Framework ByValTStr types behave like C-style, fixed-size strings inside a structure (for example, char s[5]). The behavior in managed code differs from the Microsoft Visual Basic 6.0 behavior, which is not null terminated (for example, MyString As String * 5).

ByValArray (char[]):
When MarshalAsAttribute.Value is set to ByValArray, the SizeConst must be set to indicate the number of elements in the array. The ArraySubType field can optionally contain the UnmanagedType of the array elements when it is necessary to differentiate among string types. You can only use this UnmanagedType on an array that appear as fields in a structure.

LPStr:
A single byte, null-terminated ANSI character string. You can use this member on the System.String or System.Text.StringBuilder data types

LPTStr:
A platform-dependent character string: ANSI on Windows 98 and Unicode on Windows NT and Windows XP. This value is only supported for platform invoke, and not COM interop, because exporting a string of type LPTStr is not supported.

LPWStr:
A 2-byte, null-terminated Unicode character string.

VBByRefStr:
Allows Visual Basic .NET to change a string in unmanaged code, and have the results reflected in managed code. This value is only supported for platform invoke.

Bob
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

820 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