intptr

Why can't we do IntPtr and UIntPtr arithmetic in C#?

被刻印的时光 ゝ 提交于 2019-12-05 06:07:11
It's a simple-looking question: Given that native-sized integers are the best for arithmetic, why doesn't C# (or any other .NET language) support arithmetic with the native-sized IntPtr and UIntPtr ? Ideally, you'd be able to write code like: for (IntPtr i = 1; i < arr.Length; i += 2) //arr.Length should also return IntPtr { arr[i - 1] += arr[i]; //something random like this } so that it would work on both 32-bit and 64-bit platforms. (Currently, you have to use long .) Edit: I'm not using these as pointers (the word "pointer" wasn't even mentioned)! They can be just treated as the C#

Fastest way to copy a blittable struct to an unmanaged memory location (IntPtr)

心不动则不痛 提交于 2019-12-04 15:38:06
I have a function similar to the following: [MethodImpl(MethodImplOptions.AggressiveInlining)] public void SetVariable<T>(T newValue) where T : struct { // I know by this point that T is blittable (i.e. only unmanaged value types) // varPtr is a void*, and is where I want to copy newValue to *varPtr = newValue; // This won't work, but is basically what I want to do } I saw Marshal.StructureToIntPtr(), but it seems quite slow, and this is performance-sensitive code. If I knew the type T I could just declare varPtr as a T* , but... Well, I don't. Either way, I'm after the fastest possible way to

How to convert IntPtr/Int to Socket?

江枫思渺然 提交于 2019-12-04 07:43:27
I want to convert message.WParam to Socket. protected override void WndProc(ref Message m) { if (m.Msg == Values.MESSAGE_ASYNC) { switch (m.LParam.ToInt32()) { case Values.FD_READ: WS2.Receive(m.WParam); case Values.FD_WRITE: break; default: break; } } else { base.WndProc(ref m); } } public class WS2 { public static void Receive(IntPtr sock) { Socket socket = sock; } } How to convert the IntrPtr(sock) to Socket so I can call Receive()? You can't do it because the Socket class creates and manages its own private socket handle. In theory you could use some evil reflection to jamb your socket

IntPtr arithmetics

▼魔方 西西 提交于 2019-12-04 05:01:51
I tried to allocate an array of structs in this way: struct T { int a; int b; } data = Marshal.AllocHGlobal(count*Marshal.SizeOf(typeof(T)); ... I'd like to access to allocated data "binding" a struct to each element in array allocated with AllocHGlobal... something like this T v; v = (T)Marshal.PtrToStructure(data+1, typeof(T)); but i don't find any convenient way... why IntPtr lack of arithmetics ? How can I workaround this in a "safe" way? Someone could confirm that PtrToStructure function copy data into the struct variable? In other words, modifing the struct reflect modifications in the

Can a Window Handle in .NET change it's value?

允我心安 提交于 2019-12-04 00:52:04
问题 During the lifetime of a .NET process, does the handle of a System.Windows.Forms.Form , lets say the main form used in Application.Run(form) actually change it's value, i.e. if using the value of the handle in a different process, e.g. IntPtr handle = User32.FindWindow(null, "Name") , is there a case where that handle might be invalidated by the .NET runtime? EDIT I need to know the handles because I want to use SendMessage and WM_COPYDATA and the like for IPC. 回答1: A window handle is

Getting the size of the array pointed to by IntPtr

旧时模样 提交于 2019-12-02 14:35:55
问题 I have a native C++ function that I call from a C# project using pinvoke. extern "C" _declspec(dllexport) void GetCmdKeyword( wchar_t** cmdKeyword, uint pCmdNum ) { int status = 1; int count = 0; int i = 0; if( cmdKeyword == NULL ) return ERR_NULL_POINTER; //search command in command list by letter from 'A' to 'Z' count = sizeof( stCommandList ) / sizeof( COMMANDLIST ) ; for ( i = 0 ; i < count && status != 0; i++ ) { if ( pCmdNum != stCommandList[i].ulCommand ) continue; *cmdKeyword =

How to get byte[] from IntPtr in C#

被刻印的时光 ゝ 提交于 2019-12-02 10:31:27
问题 I want to pass a IntPtr to a method takes a byte[] Parameter in c#. Is that possible and if it is possible how can I do that? thx 回答1: Check out the Marshal.Copy method. byte[] managedArray = {1,2,3,4,5}; int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length; IntPtr pnt = Marshal.AllocHGlobal(size); Marshal.Copy(pnt, managedArray, 0 , managedArray.Length); 来源: https://stackoverflow.com/questions/5298930/how-to-get-byte-from-intptr-in-c-sharp

How to get byte[] from IntPtr in C#

不问归期 提交于 2019-12-02 05:27:32
I want to pass a IntPtr to a method takes a byte[] Parameter in c#. Is that possible and if it is possible how can I do that? thx cordellcp3 Check out the Marshal.Copy method. byte[] managedArray = {1,2,3,4,5}; int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length; IntPtr pnt = Marshal.AllocHGlobal(size); Marshal.Copy(pnt, managedArray, 0 , managedArray.Length); 来源: https://stackoverflow.com/questions/5298930/how-to-get-byte-from-intptr-in-c-sharp

It is possible to get an IntPtr from an int[] array?

China☆狼群 提交于 2019-12-01 17:07:27
Greetings. In C#: If I have an int[] array declared like this int[] array = new array[size]; there is an way to get the IntPtr from this array? The thing is that I'm using the EmguCV framework, and there is an constructor to create an image which takes an IntPtr to the pixel data, in order to build an image from an array (int[]). Image<Gray,Int32> result = new Image<Gray,int>(bitmap.Width,bitmap.Height,stride,"**array.toPointer??**"); By the way if someone could told me how to calculate the stride, that would be great. You should be able to do this without unsafe code using GCHandle . Here is

How to get all memory address space used by a process?

℡╲_俬逩灬. 提交于 2019-12-01 05:25:33
I need to know all memory address space used by a process. The memory space will later be scanned to locate values within the process and identify their locations / addresses. My current process for this is to take each module's base address through its (base address + memory size). I'm testing this on a process with a known value at a known address. When I look up that specific address, I get the value I expect. However, when I scan (what I believe to be) all address space used by the process, I can't find the value anywhere. I know that a numeric value "4143000" exists at 0x0CF8DC38 and