Please Help
-
I am trying to access some functionality of the underlying RichTextBox control and am getting all mixed up with conversions of types. I know my basic issue is not completely understanding types in C#. I would be much obliged if someone could "educate" me some. I appoligize already for the amount of text in this message. Italic text is unmanged code and bold text is my attempt to write the same thing in C#. Since I need to use the SendMessage of User32.dll I defined the following: [DLLImport ("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); Then I'm trying to create the equivalent code in C# for the following callback function: DWORD EditStreamCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb); by defining the following delegate: public delegate UInt32 EditStreamCallback(IntPtr dwCookie, IntPtr pbBuff, Int32 cb, IntPtr pcb); That seems to work well (at least for the compiler). I then "translated" the following typedef: typedef struct _editstream { DWORD_PTR dwCookie; DWORD dwError; EDITSTREAMCALLBACK pfnCallback; } EDITSTREAM into the following structure: private struct EDITSTREAM { public IntPtr dwCookie; public UInt32 dwError; public EditStreamCallback pfnCallback; } So with all the definitions out of the way I thought I could do something like this: ... FileStream fs = new FileStream(Filename, FileMode.Open); int format = SF_RTF;EDITSTREAM es = new EDITSTREAM(); es.dwCookie = (IntPtr)fs; es.pfnCallback = new EditStreamCallback(StreamIn); SendMessage(this.Handle, EM_STREAMIN, (IntPtr)format, (IntPtr)es); ... but now the complier isn't happy and I get "Cannot convert type 'x' to System.IntPtr" errors where I try to cast to IntPtr. It was my understanding the "new" creates a reference type (i.e. memory address, e.g. pointer) So, why can't I cast to IntPtr here? It is interesting to note that there is no error on the cast of "format". Now on the flip side I have the following code: static UInt32 StreamIn(IntPtr dwCookie, IntPtr pbBuff, Int32 cb, IntPtr pcb) { UInt32 result = 0; FileStream fs = (FileStream)dwCookie; pcb = cb; try { pcb = fs.Read((byte[])pbBuff, 0, cb); } catch (Exception e) { pcb = 0; result = 1; } return result; } This is the callback function delegate used above. I get compile time errors at the attempted unboxing of dwCookie and pbBuff. Also there is