473,396 Members | 1,864 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,396 software developers and data experts.

Indexing of IntPtr

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 "
Nov 16 '05 #1
10 4594
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/<#G**************@TK2MSFTNGP11.phx.gbl>

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]
Nov 16 '05 #2
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 [DevelopMentor]" <ri******@develop.com> wrote in message
news:OX**************@tk2msftngp13.phx.gbl...
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/<#G**************@TK2MSFTNGP11.phx.gbl>

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]

Nov 16 '05 #3
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" <ta**********@tcon-NOSPAM.co.il> wrote in
news:ep*************@tk2msftngp13.phx.gbl...
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 [DevelopMentor]" <ri******@develop.com> wrote in message
news:OX**************@tk2msftngp13.phx.gbl...
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/<#G**************@TK2MSFTNGP11.phx.gbl>

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]


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

"Niki Estner" <ni*********@cube.net> wrote in message
news:uo**************@TK2MSFTNGP11.phx.gbl...
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" <ta**********@tcon-NOSPAM.co.il> wrote in
news:ep*************@tk2msftngp13.phx.gbl...
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 [DevelopMentor]" <ri******@develop.com> wrote in message
news:OX**************@tk2msftngp13.phx.gbl...
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/<#G**************@TK2MSFTNGP11.phx.gbl>

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]



Nov 16 '05 #5
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/<ep*************@tk2msftngp13.phx.gbl>

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 [DevelopMentor]" <ri******@develop.com> wrote in message
news:OX**************@tk2msftngp13.phx.gbl...
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/#G**************@TK2MSFTNGP11.phx.gbl 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]
Nov 16 '05 #6
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" <ta**********@tcon-NOSPAM.co.il> wrote in
news:uM**************@tk2msftngp13.phx.gbl...
Yes, you get it, but how to implement it this way? maybe using
"implicit/explicit" methods in constructor to order the array ??

"Niki Estner" <ni*********@cube.net> wrote in message
news:uo**************@TK2MSFTNGP11.phx.gbl...
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" <ta**********@tcon-NOSPAM.co.il> wrote in
news:ep*************@tk2msftngp13.phx.gbl...
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 [DevelopMentor]" <ri******@develop.com> wrote in
message news:OX**************@tk2msftngp13.phx.gbl...
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/<#G**************@TK2MSFTNGP11.phx.gbl>

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]



Nov 16 '05 #7
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 [DevelopMentor]" <ri******@develop.com> wrote in message
news:uv**************@TK2MSFTNGP11.phx.gbl...
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/<ep*************@tk2msftngp13.phx.gbl>

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 [DevelopMentor]" <ri******@develop.com> wrote in message
news:OX**************@tk2msftngp13.phx.gbl...
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/#G**************@TK2MSFTNGP11.phx.gbl

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]

Nov 16 '05 #8
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.)

Nov 16 '05 #9
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/<uz**************@tk2msftngp13.phx.gbl>

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 [DevelopMentor]" <ri******@develop.com> wrote in message
news:uv**************@TK2MSFTNGP11.phx.gbl...
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/<ep*************@tk2msftngp13.phx.gbl>

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 [DevelopMentor]" <ri******@develop.com> wrote in message
news:OX**************@tk2msftngp13.phx.gbl...
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/#G**************@TK2MSFTNGP11.phx.gbl

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]
Nov 16 '05 #10
Tamir Khason wrote:
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;

--
Truth,
James Curran [MVP]
www.NJTheater.com (Professional)
www.NovelTheory.com (Personal)


Nov 16 '05 #11

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

Similar topics

21
by: Hilde Roth | last post by:
This may have been asked before but I can't find it. If I have a rectangular list of lists, say, l = ,,], is there a handy syntax for retrieving the ith item of every sublist? I know about for i...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
2
by: Nuno Esculcas | last post by:
Hello, I come from C++ and i now have to work with C#, and someone tell me that bye bye pointers but i think this is not true, i must convert a DIB image in something that i can use in C# (like...
13
by: Christian Westerlund | last post by:
Hi! I'm trying to use P/Invoke and a Method which takes an IntPtr where I am supposed to put an address to a method which the native method will use to communicate back to me. How do I convert a...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
7
by: Ryan | last post by:
I have a bit of a problem with regards an indexing strategy. Well, basically there is no indexing strategy on a set of data I have at work. Now, I didn't create the design as I would have allowed...
3
by: Chung Leong | last post by:
Here's the rest of the tutorial I started earlier: Aside from text within a document, Indexing Service let you search on meta information stored in the files. For example, MusicArtist and...
4
by: Emin | last post by:
Dear Experts, How much slower is dict indexing vs. list indexing (or indexing into a numpy array)? I realize that looking up a value in a dict should be constant time, but does anyone have a...
2
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I have a server 2008 IIS 7.0 with indexing service installed. I have created the catalog and have a test page using these posts:...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.