Solved

String Handling in VB .Net

Posted on 2004-09-30
26
381 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get list of word ducuments in a folder 10 35
Visual Studio editor? 1 37
vb.net checkbox 7 41
Variable Event ? 3 19
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

15 Experts available now in Live!

Get 1:1 Help Now