intptr

Pointer to function for unmanaged code in C#

Hi, I have a dll which accepts a struct that contains a pointer to a function to do a callback. How can I get an IntPtr to a function of my application to build the struct? [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public class OPERATION { public uint OperationID; public IntPtr Context; ...

C# HEX Value to IntPtr

Hi all. I have a hex value to a window i found using Spy++. the value is: 00010010 Thanks to an answer to a question i asked earlier, i have this code: IntPtr hwndf = this.Handle; IntPtr hwndParent = FindWindow("WINDOW HERE", null); ; SetParent(hwndf, hwndParent); this.TopMost = false; Now, as far as i understand it, IntPtr hwndPa...

How can I avoid creating new wrapper objects all over the place?

I have various classes that wrap an IntPtr. They don't store their own data (other than the pointer), but instead use properties and methods to expose the data at the pointer using an unmanaged library. It works well, but I've gotten to the point where I need to be able to refer to these wrapper objects from other wrapper objects. For ex...

System.IO.UnmanagedMemoryStream - why Byte* instead of IntPtr

Anybody have any idea why the BCL team chose to use Byte* instead of IntPtr in the constructors for UnmanagedMemoryStream? This forces you into using an unsafe context in order to construct the type. It seems like they could have used IntPtr and that wouldn't have forced the unsafe context. ...

semi-unmanaged code with c#

public delegate void KeyboardHookCaptureHandler(KeyboardHookEventArgs keyboardEvents); public class KeyboardHookEventArgs : EventArgs { private Keys _pressedKey; private int _pressedKeyCode; public Keys PressedKey { get { return _pressedKey; } } public int PressedKeyCode { get { return _pressedKeyCode; } } pub...

PostMessage unable to pass a string C#

Here is my prototype: [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern bool PostMessage(int hhwnd, uint msg, IntPtr wparam, IntPtr lparam); And here is how I'm using it: PostMessage(HWND_BROADCAST, msg, Marshal.StringToHGlobalAuto("bob"), IntPtr.Zero); In a different thread I can intercept this message...

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

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[]). I...

How to declare a IntPtr?

Hi, I have a Window handle Picker and it says my handle is 0094167C. When I declare the variable in c# the letter in this code gives an error. How to declare? public const IntPtr WinHandle = 0094167C; ...

How to marshal int* in C#?

Hi, I would like to call this method in unmanaged library: void __stdcall GetConstraints( unsigned int* puiMaxWidth, unsigned int* puiMaxHeight, unsigned int* puiMaxBoxes ); My solution: Delegate definition: [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate void GetConstraintsDel(UIntPtr puiMaxWidth,...

Screen shot in asp.net

Hi All, I am using asp.net 2.0 with C#. I am taking screenshot of the page in that, one of the parameter which I have to pass in that function is IntPtr, when I am passing it as IntPtr iptr = new IntPtr(1000); it is giving me a very small image. I want to take the picture of the browser, no matter what is the size of that. please help...

IntPtr and avoiding unsafe code

I have an external library that takes an IntPtr. Is there any safe way to do this... int BytesWritten = 0; Output.WriteBytes(buffer, new IntPtr(&BytesWritten)); ...without having to use 'unsafe' code? I'm not that familiar with IntPtrs, but I'd like to do something like this: fixed int BytesWritten = 0; Output.WriteBytes(buffer, In...

Difference between SafeProcessHandle and IntPtr for an API call

While working with WinAPI, I decided to implement a call to GetProcessAfinityMask in my C# application. However, I've seen two different signatures for this function. One of them uses SafeProcessHandle for the handle: [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)] public static extern bool GetProcessAffinityMask(...

Serialize an IntPtr using XmlSerializer

I'm wondering why the IntPtr type is not supported by the XmlSerializer implementation. When I try to serialize a class including a field of IntPtr type, the serialization fails telling me that IntPtr is not supported, and ignore that member. To workaround this, I translate the IntPtr value to a Int64... but is it a good idea? It should...

IntPtr To String conversion in Windows messages

I'm getting in trouble by hooking window messages. I need to detect window text (caption) changes, so I intercept the WM_SETTEXT message for the interesting windows (I do so because at window creation the window caption is not specified). Reading the documentation of the WM_SETTEXT documentation, the lParam parameter specify a pointer t...

Passing a const char* character string from unmanaged to managed

I have two communicating components - one managed, the other unmanaged. The managed needs to retrieve a character string from the unmanaged implementation (the same string or just a copy). I tried the following code. // Unmanaged code const char* GetTestName(Test* test) { return test->getName(); } // Managed wrapper [DllImport(DllN...

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

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 shou...

C# calling C++ method that returns a pointer. Explain memory management.

Can someone explain what exactly is happening at a low level / memory management perspective on the 2 C# lines in "Main" in the following? C++ Code (unmanaged): #define DLLEXPORT extern "C" __declspec(dllexport) DLLEXPORT MyClass* MyClass_MyClass() { return new MyClass(); } DLLEXPORT void MyClass_setName(My...