473,788 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help me understand

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(Int Ptr hWnd, int msg, IntPtr
wParam, IntPtr lParam);

Then I'm trying to create the equivalent code in C# for the following
callback function:
DWORD EditStreamCallb ack(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG
*pcb);by defining the following delegate:public delegate UInt32
EditStreamCallb ack(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;
EDITSTREAMCALLB ACK pfnCallback;
} EDITSTREAMinto the following structure:priva te struct EDITSTREAM {public
IntPtr dwCookie;public UInt32 dwError;public EditStreamCallb ack
pfnCallback;}So with all the definitions out of the way I thought I could do
something like this:...FileStr eam fs = new FileStream(File name,
FileMode.Open); int format = SF_RTF;EDITSTRE AM es = new
EDITSTREAM();es .dwCookie = (IntPtr)fs;es.p fnCallback = new
EditStreamCallb ack(StreamIn);S endMessage(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".No w 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)dwC ookie; 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
Nov 16 '05 #1
5 2787
Eric,

Please format your message so that it can be read easily.
Don't write everything as a single line, especially code.
Put in line breaks and empty lines in between so the reader gets a chance
to breathe (and space after punctuation as well).

I'm not sure what the answer would be but I notice you don't tell the
compiler where to find the external function (no [DllImport...]).
It may be that you have done so in your original code. Without it you
are sure to get into trouble.

[DllImport("User 32.dll")]
private static extern IntPtr SendMessage(Int Ptr hWnd, int msg, IntPtr
wParam, IntPtr lParam);

Something like this would be much better:
<Eric's Message>

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(Int Ptr hWnd, int msg, IntPtr
wParam, IntPtr lParam);

Then I'm trying to create the equivalent code in C# for the following
callback function:

DWORD EditStreamCallb ack(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG
*pcb);

by defining the following delegate:

public delegate UInt32 EditStreamCallb ack(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;
EDITSTREAMCALLB ACK pfnCallback;
} EDITSTREAM

into the following structure:

private struct EDITSTREAM {
public IntPtr dwCookie;
public UInt32 dwError;
public EditStreamCallb ack pfnCallback;
}

So with all the definitions out of the way I thought I could do something
like this:...

FileStream fs = new FileStream(File name, FileMode.Open);
int format = SF_RTF;EDITSTRE AM es = new EDITSTREAM();
es.dwCookie = (IntPtr)fs;
es.pfnCallback = new EditStreamCallb ack(StreamIn);
SendMessage(thi s.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)dwC ookie;
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
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Why don't you just use C++ Managed Extension and save all the hassals???????

ben
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(Int Ptr hWnd, int msg, IntPtr
wParam, IntPtr lParam);

Then I'm trying to create the equivalent code in C# for the following
callback function:
DWORD EditStreamCallb ack(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG
*pcb);by defining the following delegate:public delegate UInt32
EditStreamCallb ack(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;
EDITSTREAMCALLB ACK pfnCallback;
} EDITSTREAMinto the following structure:priva te struct EDITSTREAM {public
IntPtr dwCookie;public UInt32 dwError;public EditStreamCallb ack
pfnCallback;}So with all the definitions out of the way I thought I could do something like this:...FileStr eam fs = new FileStream(File name,
FileMode.Open); int format = SF_RTF;EDITSTRE AM es = new
EDITSTREAM();es .dwCookie = (IntPtr)fs;es.p fnCallback = new
EditStreamCallb ack(StreamIn);S endMessage(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".No w 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)dwC ookie; 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

Nov 16 '05 #3
If I could I would use C++ Managed Extension. I have to use C#.
Eric
"benben" <be******@yahoo .com.au> wrote in message
news:ui******** ******@TK2MSFTN GP12.phx.gbl...
Why don't you just use C++ Managed Extension and save all the
hassals???????

ben
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(Int Ptr hWnd, int msg, IntPtr
wParam, IntPtr lParam);

Then I'm trying to create the equivalent code in C# for the following
callback function:
DWORD EditStreamCallb ack(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG
*pcb);by defining the following delegate:public delegate UInt32
EditStreamCallb ack(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;
EDITSTREAMCALLB ACK pfnCallback;
} EDITSTREAMinto the following structure:priva te struct EDITSTREAM
{public
IntPtr dwCookie;public UInt32 dwError;public EditStreamCallb ack
pfnCallback;}So with all the definitions out of the way I thought I could

do
something like this:...FileStr eam fs = new FileStream(File name,
FileMode.Open); int format = SF_RTF;EDITSTRE AM es = new
EDITSTREAM();es .dwCookie = (IntPtr)fs;es.p fnCallback = new
EditStreamCallb ack(StreamIn);S endMessage(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".No w 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)dwC ookie; 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


Nov 16 '05 #4
Morten,
Sorry about the formatting issues.
When I created the message in Outlook express it looked as though I had
formated it nicely.
The formating got messed up after I sent the message. I appreciate you
effort in clarifying it.

I did include [DllImport("User 32.dll")] in my code, just didn't include it
in the message.

After reading benben's comments I'm wondering if what I want to do is
possible in C#.

Thanks,
Eric
Nov 16 '05 #5
Eric,

You probably need to create a text message rather than an HTML message.
Even then you will still be fighting word wrap, but it should be a lot more
universally readable to everyone else, especially if you try to keep line
lengths under 75 characters.

You may want to get a copy of ".NET and COM - The Complete Interoperabilit y
Guide" by Adam Nathan (SAMS) -- a terrific resource for P/Invoke and all
other forms of Interop.

Also, try www.pinvoke.net for ideas and inspiration.

--Bob

"Beringer" <bo*********@in valid.com> wrote in message
news:Xm8Ld.1797 $bu.820@fed1rea d06...
Morten,
Sorry about the formatting issues.
When I created the message in Outlook express it looked as though I had
formated it nicely.
The formating got messed up after I sent the message. I appreciate you
effort in clarifying it.

I did include [DllImport("User 32.dll")] in my code, just didn't include it
in the message.

After reading benben's comments I'm wondering if what I want to do is
possible in C#.

Thanks,
Eric

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2610
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for topics to include in a course on object-orientation that I'm going to conduct. (I will later summarize all the replies and discussion, for the
2
1905
by: kevin | last post by:
hi there I am struggling with javascript. Can you please help me I've managed to get the get URL in flash to open a html that has the function that opens the Feedback form I want. I Couldn't get the feedback form to load directly from the flash because I had to use the function in dreamweaver that opens it with the specifications I wanted. That being just a simple box with no menus etc...
9
1603
by: Daz | last post by:
Hello hello! I'm trying to finish off putting my design into HTML and I've come across a problem that I can't get my head around. I've got divs floating in two columns, but I'm having problems trying to put in images and floating them left or right. In safari it leaves a gap inbetween two of my divs and the image overlaps the gap, in IE it does the same unti you mouse over the hyperlink in the div, which then hides the gap and puts...
14
1657
by: HP | last post by:
Hi All i have confussion regarding given problem please help me out 4. What happens with the following program: void main(){ myclass* pmc = new myclass; pmc = 0; delete pmc;}
6
1961
by: Buck Rogers | last post by:
Hi guys! Love your work! The below program is from K&R2, p22. ================================= #include <stdio.h> /* count digits, white space, others */ main() {
23
3287
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application to create certain textboxes, labels, and combo boxes? Any ideas would be appreciated. Thanks
4
1572
by: John | last post by:
Hi all, I have posted this type of question quite a few times but to date, no-one has actually been able to provide me with a solution. I really need to understand how to do this properly. My situation: I have n user controls which, based on user input, interchange with a placeholder on a web form.
2
2190
by: RC | last post by:
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_sort2 You can see above link or read below i copy/paste from above link <script type="text/javascript"> function sortNumber(a, b) { return a - b }
5
3373
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that I could better understand the concept, he was trying to convey to me. I ran my own example and it crashed and burn "what a surprise!" : (. I ran the authors example out of the book and quess what, it crashed also, : 0. I ran them both on my...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10118
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2897
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.