Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 435
  • Last Modified:

String Handling in VB .Net

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
ianouii
Asked:
ianouii
  • 10
  • 8
  • 4
  • +2
1 Solution
 
webtransCommented:
how to read binary file in .net is as below
http://www.dotnet4all.com/Dot-Net-Books/2004/08/read-binary-file.html
0
 
ianouiiAuthor Commented:
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
 
DotNetLover_BaanCommented:
Hi ,
what kind of argument Read_Photo takes. just string ?
What Read_Photo(strPhoto) is giving you ?

-Baan
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
iboutchkineCommented:
DId you change your API for using in .NET

Long will become Integer
Any will become Object
0
 
ianouiiAuthor Commented:
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
 
DotNetLover_BaanCommented:
So, when you do this...  nResult = Read_Photo(strPhoto)
What's it giving you ?
-Baan
0
 
ianouiiAuthor Commented:
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
 
DotNetLover_BaanCommented:
So you are concerned about "strPhoto = Space(4000)" , right ?
-Baan
0
 
DotNetLover_BaanCommented:
Do you think the problem is with the size ? try making the size bigger, lets see what happens.
0
 
ianouiiAuthor Commented:
I've made the space(500000)
still, it doesn't work.
0
 
Bob LearnedCommented:
I think what Iouri was alluding to, what is your API defined as:

Bob
0
 
Bob LearnedCommented:
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
 
ianouiiAuthor Commented:
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
 
Bob LearnedCommented:
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
 
ianouiiAuthor Commented:
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
 
iboutchkineCommented:
Yes, in this case there is nothing to change. But if it were declared as Long, you shoud've declared it as integer
0
 
ianouiiAuthor Commented:
any resolution?
0
 
Bob LearnedCommented:
Actually, what kind of string does ReadPhoto take?
0
 
ianouiiAuthor Commented:
just a normal string....
0
 
Bob LearnedCommented:
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
 
Bob LearnedCommented:
What kind of class it gsdk.dll?

Bob
0
 
ianouiiAuthor Commented:
pls explain further over what're the info u wanna obtain?

the gsdk.dll exposed the API calls for Read_Photo...
0
 
Bob LearnedCommented:
Is gsdk.dll a C++ program, VB6 dll, ...?

Bob
0
 
ianouiiAuthor Commented:
I think it's a C++ program.
not very sure about this.
0
 
Bob LearnedCommented:
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 10
  • 8
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now