intptr

The new IntPtr.Add method - am I missing the point of the int?

狂风中的少年 提交于 2019-12-10 23:43:28
问题 Starting from FW 4.0, the IntPtr structure has the Add method: public static IntPtr Add( IntPtr pointer, int offset ) Which is great, as it's supposed to address all those questions on IntPtr math we have had (1, 2, probably more). But why is the offset int ? Must it not be IntPtr ? I can easily imagine offsetting a 64-bit pointer by a value which is beyond the int range. For instance, consider Marshal.OffsetOf: public static IntPtr OffsetOf( Type t, string fieldName ) It returns an IntPtr as

C# - Converting IntPtr pointing to sockaddr structure to IPAddress

岁酱吖の 提交于 2019-12-10 21:55:05
问题 From a P/Invoked native function, I get an IntPtr which points to a sockaddr structure. How can I convert it to an IPAddress? Thanks! 回答1: Since you can't directly Marshal a sockaddr type into an IPAddress type, you'd have to make a managed struct out of it first to marshal it into: [StructLayout(LayoutKind.Sequential)] internal struct sockaddr_in { internal EnumLib.ADDRESS_FAMILIES sin_family; internal ushort sin_port; internal in_addr sin_addr; [MarshalAs(UnmanagedType.ByValArray, SizeConst

A pointer type static field's value is displayed as zero 0x0 by the debugger while it actually has a valid value

99封情书 提交于 2019-12-10 18:12:49
问题 I came across this behaviour while trying to access the value of a struct's static field with type uint* While debugging, watch window shows the static field StaticBitMask 's value as zero, but actually (and as expected) it is a valid pointer, and Console.WriteLine() prints it, as can be seen in the console output below: Pointers: ----------------- Static bitmask pointer: 0x706790 Instance bitmask pointer: 0x708A20 Values: ----------------- Static bitmask pointer val: 0xFFFFFFFF Instance

Changing the string to which an IntPtr is pointing

≡放荡痞女 提交于 2019-12-10 12:15:57
问题 In my C# application I have a variable lpData of type IntPtr (received from a call to unmanaged code), and it points to a string. I have to replace this string with another value. I tried: int RegQueryValueExW_Hooked( IntPtr hKey, string lpValueName, int lpReserved, ref Microsoft.Win32.RegistryValueKind lpType, IntPtr lpData, ref int lpcbData) { lpData = Marshal.StringToHGlobalUni("new string"); ... } but this doesn't seem to replace the actual string. Can someone point me in the right

Is GCHandleType.Pinned similar to using “fixed” keyword?

半世苍凉 提交于 2019-12-07 10:53:56
问题 I'm experimenting with IntPtr in "safe" code, comparing it to how things are done in the "unsafe" mode. Is GCHandleType.Pinned similar to using "fixed" in unsafe mode? GCHandle pinnedArray = GCHandle.Alloc(byteArray, GCHandleType.Pinned); IntPtr pointer = pinnedArray.AddrOfPinnedObject(); //do your stuff pinnedArray.Free(); vs byte[] buffer = new byte[255]; fixed (byte* p = buffer) { IntPtr ptr = (IntPtr)p; // do you stuff here } 回答1: Yes, the result is the same. The difference is in the

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

旧城冷巷雨未停 提交于 2019-12-06 10:49:02
问题 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

How to convert IntPtr/Int to Socket?

空扰寡人 提交于 2019-12-06 03:35:51
问题 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()? 回答1: You can't do it because the Socket class creates and

IntPtr arithmetics

≡放荡痞女 提交于 2019-12-05 22:55:41
问题 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

Can't change DEVMODE of a printer

♀尐吖头ヾ 提交于 2019-12-05 18:06:40
I need to change DEVMODE of printer for current printing task to pass standard and device-specific settings. I do the following: PrintDocument d = new PrintDocument(); d.PrinterSettings.PrinterName = "Microsoft XPS Document Writer"; // example printer name byte[] devmode_data; // contains a valid value that is obtained from registry IntPtr devmode = IntPtr.Zero; GCHandle handle = GCHandle.Alloc(devmode_data, GCHandleType.Pinned); try { devmode = handle.AddrOfPinnedObject(); if (devmode != IntPtr.Zero) d.PrinterSettings.SetHdevmode(devmode); } finally { if (handle.IsAllocated) handle.Free(); }

Equivalent of (IntPtr)1 in VBNET?

天涯浪子 提交于 2019-12-05 11:22:19
I've taken a piece of code from a @Hans Passant code from here: Bold text in MessageBox this is the C# code: SendMessage(hText, WM_SETFONT, mFont.ToHfont(), (IntPtr)1) Which would be the translation into vb.net? This will not work (cant be compiled): SendMessage(hText, WM_SETFONT, mFont.ToHfont(), DirectCast(1, IntPtr)) Try this: SendMessage(hText, WM_SETFONT, mFont.ToHfont(), New IntPtr(1)) 来源: https://stackoverflow.com/questions/19283287/equivalent-of-intptr1-in-vbnet