Buffer problem

  • Thread starter Thread starter Denis C
  • Start date Start date
D

Denis C

Hi,

I posted this earlier today with no help. I'm trying
agin as I am desparate(sp?).

Hi there,

I'm trying to convert part of a byte array into a series
of fixed length strings but half way through the for loop
I get an error that I'm accessing outside the buffer
bounds.

The error:

An unhandled exception of
type 'System.ArgumentOutOfRangeException' occurred in
mscorlib.dll

Additional information: Index and count must refer to a
location within the buffer.

The loop:

Private Sub initAttribText(ByVal bData() As Byte)
Dim pos As Integer = charSize * 100

For i As Integer = 0 To 39
theAttribText(i) = _
System.Text.ASCIIEncoding.ASCII.GetString
(bData, arrayStart + (i * pos),
(arrayStart + (i * pos)) + 99)
Next i

arrayStart += (40 * pos)
End Sub

The byte array bData is of length 4481.
The value of i at the point of the error is 21.
The value of arrayStart is 160.
The value of pos is 100.
So the indexes at the point of the error are 2260, 2359.

IF anyone can tell me the cause of the error I would be
very greatful!! Clearly the indexes are within the bounds
of the byte array so there must be some other cause but I
can't see it.

Thanks,

Denis
..

The byte array comes is marshalled from an IntPtr. The
IntPtr is returned by a C dll function that I have access
using the DllImportAttribute class.

Any help would be really appreciated. Unfortunately I
cannot post much more code than this. But I can say this
is the only case that htis approach is not working. For
Accessing integer arrays and boolean arrays I am having
no problems.

Denis
 
Hi Denis,

As far as we can see is arraystart global,

And therefore it maybe will work the first time,
but the second time the start point can be more than 4000
4000 + 5*100 = out of range

That is what I can see with the code you did suply, so you first have to
check what is the startpositon of arrayStart before the routine starts.

I hope this did help?

Cor
 
Hi,

The arrayStart is global and only changes after the
subroutine is done so the arrayStart of the next
subroutine is correct. i and pos supply the indexwhen
added to arrayStart.

Denis
 
Denis C said:
I posted this earlier today with no help. I'm trying
agin as I am desparate(sp?).

Sorry that I did not answer in the other thread about the same problem. I
pasted your code and tried to understand it, but I failed. Sorry.
 
Hi Dennis,


This is at the end of your sub
arrayStart += (40 * pos)

But it is probably not the error,
The function you uses is as far as I can see
(bytearray,index,count)
and not as you uses it
(bytearray,index,end)

theAttribText(i) = _
System.Text.ASCIIEncoding.ASCII.GetString
(bData, arrayStart + (i * pos),
(arrayStart + (i * pos)) + 99)

So the error I saw will probably only be in the next visit to the function.

Cor
 
-----Original Message-----
Hi Dennis,


This is at the end of your sub
arrayStart += (40 * pos)

But it is probably not the error,
The function you uses is as far as I can see
(bytearray,index,count)

That's it I think!! Count not end, I was using it
wrong!! Thanks so much!!

Denis
 
Back
Top