473,388 Members | 1,207 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How to include Chr(0) in String variable

Hello,

I'm converting a VB6 app to VB2005 which uses a third party API dll.

The API function accepts structure as its parameter (byRef).
Below is the struct (shortened for clarity):-

<StructLayout(LayoutKind.Sequential)_
Public Structure MCPBusType
Public OutBuffer As String <-- the API function will
fill result here: Initialised to String of 4096 characters
Public OutBufferSize As Integer <-- the length/size of outBuffer
(=4096 of course)
Public ResponseLen As Integer
End Structure

And I declare the Api function as follows:-

<DllImport("MCPAPI.DLL", EntryPoint:="McpEnum")_
Public Shared Function McpEnum(ByRef MCPBus As MCPBusType) As Integer
End Function

My problem is the API function returns result in OutBuffer and it's not
necessarily at the start of it. What I meant is the result in OutBuffer
could be Chr(0) & Chr(0) & "RESULT" & Chr(0) & .... but the expected result
is that "RESULT". If I take a look in OutBuffer, I couldn't get the "RESULT"
but instead it will return zero-length string ("") since there is a null
termination char at the begining of OutBuffer. I couldn't use Mid(OutBuffer,
2, 5) cos that will throw ArgOutOfRangeException.

In VB6, taking the OutBuffer gives me everything including null chars, up to
the length of the string (4096).

So, please, is there a way for me to read the whole 4096 chars in OutBuffer,
including the null chars?

Or is there any way I can change the struct and change OutBuffer to array of
bytes, and pass them to the API function and it will take it as if it was
string variable?

Please help....
Sep 19 '08 #1
2 2345
"Ajak" <aj***********@yahoo.comschrieb:
I'm converting a VB6 app to VB2005 which uses a third party API dll.

The API function accepts structure as its parameter (byRef).
Below is the struct (shortened for clarity):-

<StructLayout(LayoutKind.Sequential)_
Public Structure MCPBusType
Public OutBuffer As String <-- the API function will
fill result here: Initialised to String of 4096 characters
Public OutBufferSize As Integer <-- the length/size of
outBuffer (=4096 of course)
Public ResponseLen As Integer
End Structure
Post the C version of the structure. Maybe the string should be stored
inside the structure.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Sep 20 '08 #2
You could try to access the string as an array of char using .ToCharArray,
and then process it into another string char by char (eg, discarding invalid
characters up the the result length).

"Ajak" <aj***********@yahoo.comwrote in message
news:Ou**************@TK2MSFTNGP05.phx.gbl...
Hello,

I'm converting a VB6 app to VB2005 which uses a third party API dll.

The API function accepts structure as its parameter (byRef).
Below is the struct (shortened for clarity):-

<StructLayout(LayoutKind.Sequential)_
Public Structure MCPBusType
Public OutBuffer As String <-- the API function will
fill result here: Initialised to String of 4096 characters
Public OutBufferSize As Integer <-- the length/size of
outBuffer (=4096 of course)
Public ResponseLen As Integer
End Structure

And I declare the Api function as follows:-

<DllImport("MCPAPI.DLL", EntryPoint:="McpEnum")_
Public Shared Function McpEnum(ByRef MCPBus As MCPBusType) As
Integer
End Function

My problem is the API function returns result in OutBuffer and it's not
necessarily at the start of it. What I meant is the result in OutBuffer
could be Chr(0) & Chr(0) & "RESULT" & Chr(0) & .... but the expected
result is that "RESULT". If I take a look in OutBuffer, I couldn't get the
"RESULT" but instead it will return zero-length string ("") since there is
a null termination char at the begining of OutBuffer. I couldn't use
Mid(OutBuffer, 2, 5) cos that will throw ArgOutOfRangeException.

In VB6, taking the OutBuffer gives me everything including null chars, up
to the length of the string (4096).

So, please, is there a way for me to read the whole 4096 chars in
OutBuffer, including the null chars?

Or is there any way I can change the struct and change OutBuffer to array
of bytes, and pass them to the API function and it will take it as if it
was string variable?

Please help....

Sep 20 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Venki | last post by:
Hai Guys, I'm facing a problem in reading the file values. When the record contains the Chr(0) i can get the value till Chr(0). After Chr(0), I'm unable to get the value. It's getting truncated....
2
by: alederer | last post by:
hallo! given the following table create table s2 (a_key integer, a_varchar varchar(200)) for testing i have some scripts which makes inserts of chr(0) values like insert into s2 (a_key,...
2
by: crjunk | last post by:
I have the following code in my web page. Dim tmpReplace As String 'Giving tmpReplace the value from the textbox on the webform. tmpReplace = txtComments.Text.Trim 'Prevents report from...
5
by: Colmag | last post by:
Can somebody please help me and tell me how to include the " symbol in a string?
4
by: Elephant | last post by:
Hello, The function Microsoft.VisualBasic.Chr( ), is there a similar function in System.Text ? How does this work? Thank you, me.
2
by: yawnmoth | last post by:
Obviously, "\9" equals chr(9), but what would equal chr(90)? "\90" doesn't. Rather, "\90" seems to be equal to chr(9).chr(0). So what could I do to get chr(90) using escaped numbers?
7
by: Zytan | last post by:
I am trying to set a const string made up of Chr(), but if the value passed into Chr() is too large, the compiler complains that it is not constant!! Example: Public Const m_Data As String =...
4
by: R.A.M. | last post by:
Hello, I have little experience in PHP so I would like to ask a question. I use PHP5 in Windows XP Professional, Apache 2.0.59. I have created a website with index.php file. This file contains:...
11
!NoItAll
by: !NoItAll | last post by:
I need to search a buffer for a string of characters. The buffer is essentially a binary file (it's an image) and contains lots of chr(0) characters. The string I need to locate also contains...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.