intptr

How to convert a Bitmap Image to IntPtr in C#?

限于喜欢 提交于 2019-12-23 18:38:07
问题 I made this from an example i saw, it never threw any error, but the image is displayed as grey. Is there a better way to do this? private unsafe void menuItem7_Click(object sender, EventArgs e) { var settings = Utility.GatherLocalSettings(); openFileDialog1.InitialDirectory = settings.SavePath; openFileDialog1.Filter = "Scan Files (*.jpg)|*.jpg"; openFileDialog1.FilterIndex = 1; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { byte[] openFile =

Equivalent of (IntPtr)1 in VBNET?

流过昼夜 提交于 2019-12-22 06:59:39
问题 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)) 回答1: Try this: SendMessage(hText, WM_SETFONT, mFont.ToHfont(), New IntPtr(1)) 来源: https://stackoverflow.com/questions/19283287/equivalent-of-intptr1-in-vbnet

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

纵然是瞬间 提交于 2019-12-22 05:21:52
问题 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

what is intptr?

一世执手 提交于 2019-12-22 04:19:06
问题 I didn't understand what is IntPtr, could someone explain this? thanks 回答1: It is the managed counterpart of void* . You can cast to and from void* for usage in managed code without having to resort to unsafe code in managed layers, eg C#. 回答2: It is an integer that is the same size as a pointer. 32 bits wide in 32 bit images, 64 wide in 64 bit images. 回答3: It's a .NET platform-specific type that is used to represent a pointer or a handle. The IntPtr type is designed to be an integer whose

.NET Interop IntPtr vs. ref

不问归期 提交于 2019-12-18 11:11:56
问题 Probably a noob question but interop isn't one of my strong points yet. Aside from limiting the number of overloads is there any reason I should declare my DllImports like: [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam); And use them like this: IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(formatrange)); Marshal.StructureToPtr(formatrange, lParam, false); int returnValue = User32.SendMessage(_RichTextBox.Handle,

IntPtr to Byte Array and Back

一世执手 提交于 2019-12-17 19:24:08
问题 Referencing How to get IntPtr from byte[] in C# I am attempting to read the data that an IntPtr is referencing into a byte [] and then back into another IntPtr. The pointer is referencing an image captured from a scanner device so I have also made the assumption that capturing this information should be placed into a byte array. I am also not sure if the Marshal.SizeOf() method will return the size of the data the IntPtr is referencing or the size of the pointer itself. My issue is I am

Getting Array of struct from IntPtr

故事扮演 提交于 2019-12-17 19:19:44
问题 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, but i had memory reading error. This is my methode: public MyStruct[] GetArrayOfStruct(IntPtr pointerToStruct, int length) { var sizeInBytes = Marshal.SizeOf(typeof(TCnt)); MyStruct[] output = new MyStruct[length]; for (int i = 0; i < length; i++) { IntPtr p =

C#, default parameter value for an IntPtr

南笙酒味 提交于 2019-12-17 16:56:10
问题 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? 回答1: Somewhat unintuitive, to put it mildly, you get it by using the new operator: void Foo(IntPtr arg = new IntPtr()) { } That was for fun, you probably enjoy this one better: void Foo(IntPtr arg = default(IntPtr)) { } 回答2: Since IntPtr is a struct, you could use Nullable-of-T?

Using intptr_t instead of void*?

﹥>﹥吖頭↗ 提交于 2019-12-17 06:09:09
问题 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 already read: int -> void* -> int roundtrip is not guaranteed to hold original value; I guess int -> intptr_t -> int will do pointer arithmetics on both void* and intptr_t require casts, so none gets advantage here void* means less explicit casts when

error: cast from 'Foo*' to 'unsigned int' loses precision

点点圈 提交于 2019-12-13 12:50:59
问题 I'm trying to cast a pointer to an int (or unsigned int) and no matter what I try it doesn't want to work. I've tried static_cast<intptr_t>(obj) , reinterpret_cast<intptr_t>(obj) , and various combinations of C style casts, intptr_t 's, unsigned int 's, and I'm including stdint.h. From what I've read, one of the many things I've tried should work. What gives? I didn't bother including the code because it's exactly what I described, but since you asked, I've tried all of these plus other