Questions tagged [intptr]

IntPtr is a .NET Framework platform-specific type that is used to represent a pointer or a handle.

IntPtr is a .NET Framework platform-specific type that is used to represent a pointer or a handle.

296 questions
195
votes
8 answers

Just what is an IntPtr exactly?

Through using IntelliSense and looking at other people's code, I have come across this IntPtr type; every time it has needed to be used I have simply put null or IntPtr.Zero and found most functions to work. What exactly is it and when/why is it…
Callum Rogers
  • 15,630
  • 17
  • 67
  • 90
60
votes
5 answers

Why / when to use `intptr_t` for type-casting in C?

I have a question regarding using intptr_t vs. long int. I've observed that incrementing memory addresses (e.g. via manual pointer arithmetic) differs by data type. For instance incrementing a char pointer adds 1 to the memory address, whereas…
william_grisaitis
  • 5,170
  • 3
  • 33
  • 40
57
votes
6 answers

C# how to get Byte[] from IntPtr

I have a .dll(not my own) that has a delegate. This delegate Callback function is: "CallBackFN(ushort opCOde, IntPtr payload, uint size, uint localIP)" How can i convert IntPtr to Byte[]? I think that payload is actually Byte[]. If it's not Byte[]…
Gabriel
  • 1,435
  • 3
  • 17
  • 24
57
votes
2 answers

C# - How To Convert Object To IntPtr And Back?

I want to pass an object from managed code to a WinApi function as IntPtr. It will pass this object back to my callback function in managed code as IntPtr. It's not a structure, it's an instance of a class. How do I convert object to IntPtr and back…
Bitterblue
  • 13,162
  • 17
  • 86
  • 124
38
votes
4 answers

Copy data from from IntPtr to IntPtr

I have two IntPtr values pointing to some data areas of length bytes. length may have an order of magnitude of 200k to 400k. int length = /* ..*/ IntPtr ptrSrc = /*.. */; IntPtr ptrDst = /* .. */; Now I want to copy the data from ptrSrc to ptrDst.…
Doc Brown
  • 19,739
  • 7
  • 52
  • 88
32
votes
4 answers

Can IntPtr be cast into a byte array without doing a Marshal.Copy?

I want to get data from an IntPtr pointer into a byte array. I can use the following code to do it: IntPtr intPtr = GetBuff(); byte[] b = new byte[length]; Marshal.Copy(intPtr, b, 0, length); But the above code forces a copy operation from IntPtr…
TTGroup
  • 3,575
  • 10
  • 47
  • 79
31
votes
2 answers

Using intptr_t instead of void*?

Is it a good idea to use intptr_t as a general-purpose storage (to hold pointers and integer values) instead of void*? (As seen here: http://www.crystalspace3d.org/docs/online/manual/Api1_005f0-64_002dBit-Portability-Changes.html) For what I've…
Gepard
  • 499
  • 1
  • 4
  • 7
29
votes
4 answers

What do LRESULT, WPARAM and LPARAM mean?

I'm importing WinApi functions, writing callbacks etc. (example) in C# and always wonder: what do they mean ? LRESULT as last result ? W-PARAM ? L-PARAM ? how to safely "wrap" them WPARAM and LPARAM contain structs sometimes. So I need to use them…
Bitterblue
  • 13,162
  • 17
  • 86
  • 124
20
votes
2 answers

Correct way to marshal SIZE_T*?

I have the following C++ function definition, which I am trying to call through PInvoke from managed code: bool FooBar(SIZE_T* arg1); My managed declaration looked as follows: [DllImport("mydll", SetLastError=true, CharSet=CharSet.Unicode)] private…
sooniln
  • 14,607
  • 4
  • 29
  • 35
19
votes
3 answers

Getting Array of struct from IntPtr

I have some struct like this struct MyStruct { public int field1; public int field2; public int field3; } and I have pointer to array of this struct. So, I need to get array from this pointer. I'm tried to using Marshal.PtrToStructure,…
Moroz
  • 217
  • 1
  • 3
  • 6
19
votes
3 answers

C# Can I check if an IntPtr is null?

I have an IntPtr field in my C# class. It holds a reference to an object in a C++ library. protected IntPtr ThingPtr; At some stage I may or may not initialise it. ThingPtr = FunctionInMyCplusplusLibrary(); I'm wondering if checking whether it is…
Guye Incognito
  • 2,726
  • 6
  • 38
  • 72
18
votes
4 answers

Proper IntPtr use in C#

I think I understand the use of IntPtr, though I'm really not sure. I copied the IDisposable pattern from MSDN just to see what I could get from it, and while I understand it for the most part, I have no idea how to implement an IntPtr properly, or…
zeboidlund
  • 9,731
  • 31
  • 118
  • 180
18
votes
2 answers

C#, default parameter value for an IntPtr

I'd like to use a default parameter value of IntPtr.Zero in a function that takes an IntPtr as an argument. This is not possible as IntPtr.Zero is not a compile time constant. Is there any way I can do what I want?
Tom Davies
  • 2,386
  • 3
  • 27
  • 44
17
votes
3 answers

Win api in C#. Get Hi and low word from IntPtr

I am trying to process a WM_MOUSEMOVE message in C#. What is the proper way to get an X and Y coordinate from lParam which is a type of IntPtr?
HelloWorld
  • 1,061
  • 2
  • 15
  • 25
17
votes
2 answers

IntPtr into hex string in string.Format

Note, I am not quite sure this question belongs to this site but I try to be constructive. Why does the code IntPtr ptr = new IntPtr(1234); Console.WriteLine(string.Format("{0:X8}",…
Roland Pihlakas
  • 4,246
  • 2
  • 43
  • 64
1
2 3
19 20