Answered by:
Printing a RichTextBox

Question
-
I use VB in VS2010
I am attempting to print the contents of a RichTextBox using the EM_FORMATRANGE message.
I am using code that appears often in posts about this topic.The API call is:
Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal msg As Long, ByVal wp As Long, ByVal lp As Long) As LongThe codeline that sends the message is:
nNextCharPos = SendMessage(MyText.Handle, EM_FORMATRANGE, wparam, lparam)
nNextCharPos, SendMessage, EM_FORMATRANGE are all declared as long
MyText is IntPtr
wparam, and lparam are all declared as IntPtr as followsDim wparam As IntPtr = New IntPtr(1)
Dim lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fr))
Marshal.StructureToPtr(fr, lparam, False)The code fails with the message .... unbalanced stack ..... possibly due to signature mismatch ....
If I try
nNextCharPos = SendMessage(clng(MyText.Handle), EM_FORMATRANGE, clng(wparam), clng(lparam))
I get the same message
*********************************************************
I have also tried re-declaring SendMessage as
Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As IntPtr, ByVal msg As IntPtr, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtrand declaring nNextCharPos as IntPtr
with the codeline
nNextCharPos = SendMessage(MyText.Handle, cInt(EM_FORMATRANGE), wparam), lparam)
and this time the error message comes out as Trying to read or write to protected memory.
******************************************************
So I am stuck.
Can anyone help?
Thanks
ME
- Moved by Cindy Meister MVP Tuesday, June 25, 2013 12:45 PM not Word-related
Monday, June 24, 2013 6:43 PM
Answers
-
Essentially, this is a question for this forum: http://msdn.microsoft.com/en-US/library/vstudio/ms233833
I can also advise you to go to http://www.pinvoke.net/ , afaik the def. resource about doing win32 api calls from within a managed language.
Kind regards,
Margriet BruggemanLois & Clark IT Services
web site: http://www.loisandclark.eu
blog: http://www.sharepointdragons.com
- Proposed as answer by Ed Price - MSFTMicrosoft employee Thursday, June 27, 2013 8:10 PM
- Marked as answer by Margriet BruggemanEditor Saturday, June 29, 2013 8:00 AM
Tuesday, June 25, 2013 2:28 PMAnswerer -
I'd ask in the Visual Basic language forum.
Karl
When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer
My Blog: http://unlockpowershell.wordpress.com
My Book: Windows PowerShell 2.0 Bible
My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})- Edited by Just Karl Tuesday, June 25, 2013 5:37 PM Link repair
- Proposed as answer by Ed Price - MSFTMicrosoft employee Thursday, June 27, 2013 8:10 PM
- Marked as answer by Margriet BruggemanEditor Saturday, June 29, 2013 8:00 AM
Tuesday, June 25, 2013 5:35 PM
All replies
-
Essentially, this is a question for this forum: http://msdn.microsoft.com/en-US/library/vstudio/ms233833
I can also advise you to go to http://www.pinvoke.net/ , afaik the def. resource about doing win32 api calls from within a managed language.
Kind regards,
Margriet BruggemanLois & Clark IT Services
web site: http://www.loisandclark.eu
blog: http://www.sharepointdragons.com
- Proposed as answer by Ed Price - MSFTMicrosoft employee Thursday, June 27, 2013 8:10 PM
- Marked as answer by Margriet BruggemanEditor Saturday, June 29, 2013 8:00 AM
Tuesday, June 25, 2013 2:28 PMAnswerer -
I'd ask in the Visual Basic language forum.
Karl
When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer
My Blog: http://unlockpowershell.wordpress.com
My Book: Windows PowerShell 2.0 Bible
My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})- Edited by Just Karl Tuesday, June 25, 2013 5:37 PM Link repair
- Proposed as answer by Ed Price - MSFTMicrosoft employee Thursday, June 27, 2013 8:10 PM
- Marked as answer by Margriet BruggemanEditor Saturday, June 29, 2013 8:00 AM
Tuesday, June 25, 2013 5:35 PM