intptr

PInvokeStackImbalance exception when using IntPtr in .NET 4? (Works in .NET 3.5)

允我心安 提交于 2019-11-29 14:50:23
Might be a bit of noob question but it's something that's been getting me in a pickle for the past few hours (or days)... I'm calling a method from a DLL in my code in .NET Framework 4.0 [DllImport("xeneth.dll")] public static extern ErrorCode XC_GetFrame(Int32 h, FrameType type, ulong ulFlags, IntPtr buff, uint size); and then using it here: if (XC_GetFrame(myCam, XC_GetFrameType(myCam), 0, IntPtr.Zero, (uint)fs) != ErrorCode.E_NO_FRAME) However, when I run this in .NET 4.0 I get a P/INVOKE error, however... running this in 3.5 does not trigger this error. After myself and another programmer

Proper IntPtr use in C#

。_饼干妹妹 提交于 2019-11-29 13:49:40
问题 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 even understand what it is that it's supposed to "point" to, or reference. On top of that, I have no idea how to even assign or cast an integer, string, char, double, etc. to an IntPtr to create a pointer out of it. Also, does IntPtr require unsafe

Correct way to marshal SIZE_T*?

走远了吗. 提交于 2019-11-29 06:19:42
问题 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 static extern bool FooBar(ref uint arg1); Some of you may notice the same bug I eventually did. This is not 64bit portable. SIZE_T is of variable size (32-64 bit) as is the pointer to it. On the managed size the pointer correctly translates to

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

怎甘沉沦 提交于 2019-11-28 21:14:43
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 into the byte array. It is not a good solution when the data in question is large. Is there any way to cast an IntPtr to a byte array? For example, would the following work: byte[] b = (byte[])intPtr This would eliminate the need for the copy operation. Also: how can we determine the length of data pointed to by IntPtr? As others have mentioned, there

Getting Array of struct from IntPtr

江枫思渺然 提交于 2019-11-28 11:09:31
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 = new IntPtr((pointerToStruct.ToInt32() + i * sizeInBytes)); output[i] = (MyStruct)System.Runtime

IntPtr to Byte Array and Back

偶尔善良 提交于 2019-11-28 10:22:17
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 receiving the error "Type 'System.Byte[]' cannot be marshaled as an unmanaged structure; no meaningful size

How to get window's position? [duplicate]

拥有回忆 提交于 2019-11-28 07:45:03
This question already has an answer here: How to get and set the window position of another application in C# 4 answers I'd like to know the way of getting process'es window position. I've been looking for that on the internet but with no results. Thanks :) Process[] processes = Process.GetProcessesByName("notepad"); Process lol = processes[0]; IntPtr p = lol.MainWindowHandle; ionden Try this: [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr FindWindow(string strClassName, string strWindowName); [DllImport("user32.dll")] public static extern bool GetWindowRect

C#, default parameter value for an IntPtr

旧街凉风 提交于 2019-11-28 01:49:08
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? 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)) { } Since IntPtr is a struct, you could use Nullable-of-T? static void SomeMethod(IntPtr? ptr = null) { var actualPtr = ptr ?? IntPtr.Zero; //... } 来源: https://stackoverflow

C# Process.MainWindowHandle always returns IntPtr Zero

烂漫一生 提交于 2019-11-27 22:20:25
this is my code: using (Process game = Process.Start(new ProcessStartInfo() { FileName="DatabaseCheck.exe", RedirectStandardOutput = true, CreateNoWindow = true, UseShellExecute = false })) { lblLoad.Text = "Loading"; int Switch = 0; while (game.MainWindowHandle == IntPtr.Zero) { Switch++; if (Switch % 1000 == 0) { lblLoad.Text += "."; if (lblLoad.Text.Contains("....")) lblLoad.Text = "Loading."; lblLoad.Update(); game.Refresh(); } } Problem is, that game.MainWindowHandle is always IntPtr.Zero. I need to find the IntPtr of the ran process to confirm that the game was started by the launcher,

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

陌路散爱 提交于 2019-11-27 07:24:00
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 So if I want to pass a list to my callback function through WinApi I use GCHandle // object to IntPtr (before calling WinApi): List<string> list1 = new List<string>(); GCHandle handle1 = GCHandle.Alloc(list1); IntPtr parameter = (IntPtr) handle1; // call WinAPi and pass the parameter here // then free the handle when not needed: