intptr

Marshalling IntPtrs within structs in C#

心已入冬 提交于 2019-12-13 03:58:57
问题 I intend to model a C# class for parsing custom protocol data I receive as a raw buffer. The buffer is already at hand as a byte array. This parser has to cope with varying field layouts, hence some of the protocol elements are IntPtrs. When I try to access a pointer within a struct after mapping the raw buffer into it, I run into an access violation. I made up a simple example which shows the crucial part: namespace CS_StructMarshalling { class Packet { public PacketStruct Fields;

PostMessage unable to pass a string C#

不羁岁月 提交于 2019-12-12 12:30:43
问题 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, but when I try to get bob back using: string str = Marshal.PtrToStringAuto(m.WParam); // where m = the Message object I don't get bob in str. I'm thinking this has got to

Create and Save a Bitmap from an IntPtr c#

て烟熏妆下的殇ゞ 提交于 2019-12-12 04:34:52
问题 In my application I scan an image from a TWAIN device, then, I need to save this image to a local disk. All I have is an IntPtr that references the image byte data array. How can I create a Bitmap object using the IntPtr value? I tried with: Bitmap _img = new Bitmap(2450, 2450, 2452, PixelFormat.Format8bppIndexed, pixptr); _img.Save("c:\\1.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg); This saves the image, but the image is never the same as I scanned. Any help? 来源: https://stackoverflow

P/Invoke. How to call unmanaged method with marshalling from C#?

你。 提交于 2019-12-12 02:09:49
问题 I've got a problem with P/Invoke. I'm calling .dll (implemented on c++) from c# code. There is a class, that contains the next methods: virtual AudioFileList *API CreateAudioFileList ()=0; virtual bool API DisposeAudioFileList (AudioFileList *iAudioFileList)=0; AudioFileList class looks like: virtual bool API GetFile (long index, std::string *oPath, AudioFileInfo *fileInfo)=0; virtual long API GetNumberFiles ()=0; So, the question is how can I call CreateAudioFileList method and than pass the

Using IntPtr with IComparer<T>

*爱你&永不变心* 提交于 2019-12-11 18:18:44
问题 Three related questions here: The IntPtr structure apparently does not implement < and > operators. Is there a way to perform this comparison without converting the structure to an int or long? Why are the < and > operators not implemented on this structure? I am enumerating windows from a 32 bit process. Windows from 64 bit processes also seem to be included, which is fine. How can I tell whether to use ToInt32 or ToInt64 on the IntPtr structure? Context: Windows 7 (64 bit), VS 2010,

How to release/free IntPtr to function pointer?

我怕爱的太早我们不能终老 提交于 2019-12-11 12:16:29
问题 I've got native DLL (without sources) with two extern methods: Init and DoSomeWork . Here is my class-wrapper: public class MyClass : IDisposable { [DllImport(@"myDLL.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "EntryPoint#1", ExactSpelling = true)] private static extern IntPtr InitNative(); [DllImport(@"myDLL.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "EntryPoint

C# get parent process from window handle

家住魔仙堡 提交于 2019-12-11 10:37:12
问题 I have a C# windows application and ultimately launches a dialog from an interop component. The problem is that this dialog window sometimes appears behind the c# application's main window, forcing a user to alt-tab to find it. I've put measures into place to find this dialog window and bring it forward... private static extern bool SetForegroundWindow(IntPtr hWnd); public class SearchData { public string Wndclass; public string Title; public IntPtr hWnd; } private static extern bool

Simulate keyboard click in GTA:SA

寵の児 提交于 2019-12-11 06:55:07
问题 I'm trying to run a program that sometimes will simulate clicks in GTA:SA. Problem is it doesn't do anything when running. I have tried this code so far: IntPtr calculatorHandle = FindWindow(null, "GTA:SA:MP"); if (calculatorHandle == IntPtr.Zero) { MessageBox.Show("GTA:SA:MP isn't running"); return; } SetForegroundWindow(calculatorHandle); SendKeys.SendWait("T123"); I'd be glad if you tell if something is wrong with my code. Thanks! EDIT This is my code after adding Neill's code: IntPtr

Serialize an IntPtr using XmlSerializer

假如想象 提交于 2019-12-11 06:05:02
问题 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 be, as far I can think. In concrete, I need to serialize a window handle, which is typed IntPtr in .NET framework. Am I doing correct? 回答1: The reason that an

Calling methods in third-party DLLs

只愿长相守 提交于 2019-12-11 03:07:21
问题 I'm using C# and P-Invoke to get access to objects in the Qt framework (http://qt.digia.com/). I don't seem to be having trouble using functions that return simple types (or void), but whenever I try to use a function that returns an object, the application crashes. For example, in QtXml4.dll, there is a method QXmlInputSource::data(void) that returns an object of type QString. Here is my wrapper class: public class QXmlInputSource { // PInvoke - class QString QXmlInputSource::data(void)