B
Beringer
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 reference types in C#. I would
be much obliged if someone could "educate" me some.
Since I need to use the SendMessage of User32.dll I defined the following:
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
ublic 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;
} EDITSTREAMinto the following structure
rivate 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 call back function delegate
used above. I get compile time errors at the attempted unboxing of dwCookie
and pbBuff. Also there is an error at pcb = cb ("cannot implictly convert
int to IntPtr")I know I am asking alot here but I am really stuck and since
I'm trying to learn C# on my own I could really use some help.Thank you in
advance,Eric
control and am getting all mixed up with conversions of types. I know my
basic issue is not completely understanding reference types in C#. I would
be much obliged if someone could "educate" me some.
Since I need to use the SendMessage of User32.dll I defined the following:
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

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;
} EDITSTREAMinto the following structure

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 call back function delegate
used above. I get compile time errors at the attempted unboxing of dwCookie
and pbBuff. Also there is an error at pcb = cb ("cannot implictly convert
int to IntPtr")I know I am asking alot here but I am really stuck and since
I'm trying to learn C# on my own I could really use some help.Thank you in
advance,Eric