Solved

String Handling in VB .Net

Posted on 2004-09-30
26
368 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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi ,
what kind of argument Read_Photo takes. just string ?
What Read_Photo(strPhoto) is giving you ?

-Baan
0
 
LVL 28

Expert Comment

by:iboutchkine
Comment Utility
DId you change your API for using in .NET

Long will become Integer
Any will become Object
0
 
LVL 5

Author Comment

by:ianouii
Comment Utility
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
Comment Utility
So, when you do this...  nResult = Read_Photo(strPhoto)
What's it giving you ?
-Baan
0
 
LVL 5

Author Comment

by:ianouii
Comment Utility
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
Comment Utility
So you are concerned about "strPhoto = Space(4000)" , right ?
-Baan
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
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
Comment Utility
I've made the space(500000)
still, it doesn't work.
0
 
LVL 96

Expert Comment

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

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 5

Author Comment

by:ianouii
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
any resolution?
0
 
LVL 96

Expert Comment

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

Author Comment

by:ianouii
Comment Utility
just a normal string....
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Comment Utility
What kind of class it gsdk.dll?

Bob
0
 
LVL 5

Author Comment

by:ianouii
Comment Utility
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
Comment Utility
Is gsdk.dll a C++ program, VB6 dll, ...?

Bob
0
 
LVL 5

Author Comment

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

Accepted Solution

by:
Bob Learned earned 500 total points
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

10 Experts available now in Live!

Get 1:1 Help Now