Indexing of IntPtr

  • Thread starter Richard Blewett [DevelopMentor]
  • Start date
R

Richard Blewett [DevelopMentor]

I think you misunderstand the nature of IntPtr. It is mean't to be used to represent an opaque handle retrieved via interop - its not a pointer to an integer or a pointer of integer size. The point is its a "thing" that you receive from and pass back to the interop layer (and so to the operating system or some other DLL). if you want to walk a specific array just index into the array or use foreach.

What exactly are you trying to achieve?

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#[email protected]>

I have a pointer to array and I want to apply indexing to this Array so I
have function (name it IntPrt Func[]) to go to certain member I can use (as
C++) Func[5], but in C# I recieve an error "Cannot apply indexing with [] to
an expression of type 'System.IntPtr'".

How to get rid of it?

TNX

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004



[microsoft.public.dotnet.languages.csharp]
 
N

Niki Estner

Did I get that right: You cast an array to IntPtr, and want to use that
IntPtr later to access data in the array? This will never work: The GC might
move the array, or even free it without changing your IntPtr. You can
temporarily fix an object on the mamanged heap, but this is only possible
inside a fixed-block, which ends as soon as you leave a function.

Niki

Tamir Khason said:
As I mentioned earlier I build a function with return from unmanaged the
pointer to array item on override of "this" method so when using Func[1]
it will return the pointer to 1st member of source array, but (see
message) an error on calling this function



Richard Blewett said:
I think you misunderstand the nature of IntPtr. It is mean't to be used to
represent an opaque handle retrieved via interop - its not a pointer to an
integer or a pointer of integer size. The point is its a "thing" that you
receive from and pass back to the interop layer (and so to the operating
system or some other DLL). if you want to walk a specific array just index
into the array or use foreach.

What exactly are you trying to achieve?

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog


nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#[email protected]>

I have a pointer to array and I want to apply indexing to this Array so I
have function (name it IntPrt Func[]) to go to certain member I can use
(as
C++) Func[5], but in C# I recieve an error "Cannot apply indexing with []
to
an expression of type 'System.IntPtr'".

How to get rid of it?

TNX

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004



[microsoft.public.dotnet.languages.csharp]
 
T

Tamir Khason

I have a pointer to array and I want to apply indexing to this Array so I
have function (name it IntPrt Func[]) to go to certain member I can use (as
C++) Func[5], but in C# I recieve an error "Cannot apply indexing with [] to
an expression of type 'System.IntPtr'".

How to get rid of it?

TNX
 
R

Richard Blewett [DevelopMentor]

OK, so an interop call returns a pointer to an array which you receive as an IntPtr, e.g.:

[DllImport("Foo.dll")]

IntPtr GetArray();

or something like that.

Now in the class you have you want to provide access to this array via an indexer. However

IntPtr i = GetArray();

public char this[int index]

{

get

{

return i[index];

}

}

doesn't compile?

IntPtr is not mea';t to be manipulated from managed code only received and returned - as I explained. You need to receive this array back in a way in which you can manipulate not as an IntPtr.

Have I got the scenario right? and if so can you show us the P/Invoke signature (DllImport, etc)

regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog



nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

As I mentioned earlier I build a function with return from unmanaged the
pointer to array item on override of "this" method so when using Func[1] it
will return the pointer to 1st member of source array, but (see message) an
error on calling this function



Richard Blewett said:
I think you misunderstand the nature of IntPtr. It is mean't to be used to
represent an opaque handle retrieved via interop - its not a pointer to an
integer or a pointer of integer size. The point is its a "thing" that you
receive from and pass back to the interop layer (and so to the operating
system or some other DLL). if you want to walk a specific array just index
into the array or use foreach.

What exactly are you trying to achieve?

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog


nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/#[email protected]
I have a pointer to array and I want to apply indexing to this Array so I
have function (name it IntPrt Func[]) to go to certain member I can use
(as
C++) Func[5], but in C# I recieve an error "Cannot apply indexing with []
to
an expression of type 'System.IntPtr'".

How to get rid of it?

TNX

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004



[microsoft.public.dotnet.languages.csharp]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004



[microsoft.public.dotnet.languages.csharp]
 
T

Tamir Khason

As I mentioned earlier I build a function with return from unmanaged the
pointer to array item on override of "this" method so when using Func[1] it
will return the pointer to 1st member of source array, but (see message) an
error on calling this function
 
T

Tamir Khason

Yes, you get it, but how to implement it this way? maybe using
"implicit/explicit" methods in constructor to order the array ??

Niki Estner said:
Did I get that right: You cast an array to IntPtr, and want to use that
IntPtr later to access data in the array? This will never work: The GC
might move the array, or even free it without changing your IntPtr. You
can temporarily fix an object on the mamanged heap, but this is only
possible inside a fixed-block, which ends as soon as you leave a function.

Niki

Tamir Khason said:
As I mentioned earlier I build a function with return from unmanaged the
pointer to array item on override of "this" method so when using Func[1]
it will return the pointer to 1st member of source array, but (see
message) an error on calling this function



Richard Blewett said:
I think you misunderstand the nature of IntPtr. It is mean't to be used
to represent an opaque handle retrieved via interop - its not a pointer
to an integer or a pointer of integer size. The point is its a "thing"
that you receive from and pass back to the interop layer (and so to the
operating system or some other DLL). if you want to walk a specific array
just index into the array or use foreach.

What exactly are you trying to achieve?

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog


nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#[email protected]>

I have a pointer to array and I want to apply indexing to this Array so
I
have function (name it IntPrt Func[]) to go to certain member I can use
(as
C++) Func[5], but in C# I recieve an error "Cannot apply indexing with
[] to
an expression of type 'System.IntPtr'".

How to get rid of it?

TNX

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004



[microsoft.public.dotnet.languages.csharp]
 
N

Niki Estner

You can't.
IntPtr wasn't made for that.
Rethink your design; What's wrong with passing an ICollection reference or
similar?

Niki

Tamir Khason said:
Yes, you get it, but how to implement it this way? maybe using
"implicit/explicit" methods in constructor to order the array ??

Niki Estner said:
Did I get that right: You cast an array to IntPtr, and want to use that
IntPtr later to access data in the array? This will never work: The GC
might move the array, or even free it without changing your IntPtr. You
can temporarily fix an object on the mamanged heap, but this is only
possible inside a fixed-block, which ends as soon as you leave a
function.

Niki

Tamir Khason said:
As I mentioned earlier I build a function with return from unmanaged the
pointer to array item on override of "this" method so when using Func[1]
it will return the pointer to 1st member of source array, but (see
message) an error on calling this function



message I think you misunderstand the nature of IntPtr. It is mean't to be used
to represent an opaque handle retrieved via interop - its not a pointer
to an integer or a pointer of integer size. The point is its a "thing"
that you receive from and pass back to the interop layer (and so to the
operating system or some other DLL). if you want to walk a specific
array just index into the array or use foreach.

What exactly are you trying to achieve?

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog


nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#[email protected]>

I have a pointer to array and I want to apply indexing to this Array so
I
have function (name it IntPrt Func[]) to go to certain member I can use
(as
C++) Func[5], but in C# I recieve an error "Cannot apply indexing with
[] to
an expression of type 'System.IntPtr'".

How to get rid of it?

TNX

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004



[microsoft.public.dotnet.languages.csharp]
 
S

Steven Cheng[MSFT]

Hi Tamir,

Thank you for posting. Regarding on the issue, I am
finding proper resource to assist you and we will update as soon as posible.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security(This posting is provided "AS IS",
with no warranties, and confers no rights.)
 
R

Richard Blewett [DevelopMentor]

OK, two issues:

1) what exactly *is* the returned IntPtr - its an array you say so what type are the members of the array?

2) How is the memory being allocated for the returned array? Do you have another call to free_unmg_items? If not then how is the memory going to get freed up? .NET won't know whether you allocated with VirtualAlloc, HeapAllow, new, malloc, CoTaskMemAlloc, etc.

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Almost, it's going like this:

[DllImport("Foo.dll")]
static extern IntPtr int_unmg_get_items(IntPtr raw);

public IntPtr Items {
get {
IntPtr ret = int_unmg_get_items (Handle);
return ret;
}
}

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Richard Blewett said:
OK, so an interop call returns a pointer to an array which you receive as
an IntPtr, e.g.:

[DllImport("Foo.dll")]

IntPtr GetArray();

or something like that.

Now in the class you have you want to provide access to this array via an
indexer. However

IntPtr i = GetArray();

public char this[int index]

{

get

{

return i[index];

}

}

doesn't compile?

IntPtr is not mea';t to be manipulated from managed code only received and
returned - as I explained. You need to receive this array back in a way in
which you can manipulate not as an IntPtr.

Have I got the scenario right? and if so can you show us the P/Invoke
signature (DllImport, etc)

regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog




nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

As I mentioned earlier I build a function with return from unmanaged the
pointer to array item on override of "this" method so when using Func[1]
it
will return the pointer to 1st member of source array, but (see message)
an
error on calling this function



Richard Blewett said:
I think you misunderstand the nature of IntPtr. It is mean't to be used
to
represent an opaque handle retrieved via interop - its not a pointer to
an
integer or a pointer of integer size. The point is its a "thing" that you
receive from and pass back to the interop layer (and so to the operating
system or some other DLL). if you want to walk a specific array just
index
into the array or use foreach.

What exactly are you trying to achieve?

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog


nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/#[email protected]
I have a pointer to array and I want to apply indexing to this Array so
I
have function (name it IntPrt Func[]) to go to certain member I can use
(as
C++) Func[5], but in C# I recieve an error "Cannot apply indexing with
[]
to
an expression of type 'System.IntPtr'".

How to get rid of it?

TNX

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004



[microsoft.public.dotnet.languages.csharp]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004



[microsoft.public.dotnet.languages.csharp]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004



[microsoft.public.dotnet.languages.csharp]
 
T

Tamir Khason

Almost, it's going like this:

[DllImport("Foo.dll")]
static extern IntPtr int_unmg_get_items(IntPtr raw);

public IntPtr Items {
get {
IntPtr ret = int_unmg_get_items (Handle);
return ret;
}
}

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Richard Blewett said:
OK, so an interop call returns a pointer to an array which you receive as
an IntPtr, e.g.:

[DllImport("Foo.dll")]

IntPtr GetArray();

or something like that.

Now in the class you have you want to provide access to this array via an
indexer. However

IntPtr i = GetArray();

public char this[int index]

{

get

{

return i[index];

}

}

doesn't compile?

IntPtr is not mea';t to be manipulated from managed code only received and
returned - as I explained. You need to receive this array back in a way in
which you can manipulate not as an IntPtr.

Have I got the scenario right? and if so can you show us the P/Invoke
signature (DllImport, etc)

regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog




nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

As I mentioned earlier I build a function with return from unmanaged the
pointer to array item on override of "this" method so when using Func[1]
it
will return the pointer to 1st member of source array, but (see message)
an
error on calling this function



Richard Blewett said:
I think you misunderstand the nature of IntPtr. It is mean't to be used
to
represent an opaque handle retrieved via interop - its not a pointer to
an
integer or a pointer of integer size. The point is its a "thing" that you
receive from and pass back to the interop layer (and so to the operating
system or some other DLL). if you want to walk a specific array just
index
into the array or use foreach.

What exactly are you trying to achieve?

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog


nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/#[email protected]
I have a pointer to array and I want to apply indexing to this Array so
I
have function (name it IntPrt Func[]) to go to certain member I can use
(as
C++) Func[5], but in C# I recieve an error "Cannot apply indexing with
[]
to
an expression of type 'System.IntPtr'".

How to get rid of it?

TNX

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004



[microsoft.public.dotnet.languages.csharp]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004



[microsoft.public.dotnet.languages.csharp]
 
J

James Curran

Tamir said:
I have a pointer to array and I want to apply indexing to this Array
so I have function (name it IntPrt Func[]) to go to certain member I
can use (as C++) Func[5], but in C# I recieve an error "Cannot apply
indexing with [] to an expression of type 'System.IntPtr'".

The block must be moved from unmanaged memory to managed memory:

IntPtr ptr = int_unmg_get_items (Handle);
int len = int_unmg_get_items_count(Handle);
int[] ret = new int[len];
Marshal.Copy(ptr, ret, 0, len);
return ret;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top