Click here to Skip to main content
6,295,667 members and growing! (15,190 online)
Email Password   helpLost your password?
Desktop Development » Edit Controls » General     Intermediate License: The Code Project Open License (CPOL)

Use IRichEditOle from C#

By John Fisher

Take control of the OLE objects embedded inside a .NET RichTextBox
C#, Windows, .NET 1.0, .NET 1.1VS.NET2003, Dev
Posted:11 Jul 2003
Updated:13 Jul 2003
Views:104,056
Bookmarked:41 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
25 votes for this article.
Popularity: 5.74 Rating: 4.11 out of 5
2 votes, 8.0%
1
1 vote, 4.0%
2

3
3 votes, 12.0%
4
19 votes, 76.0%
5

Sample Image - RichTextBoxPlus.jpg

Introduction

Recently, I needed to write some C# code to dynamically modify an object inside an RTF document.  I had done this in C++ before, so I knew it was possible, and started searching the internet for a boost (CP first!)

My hunt not only turned up fruitless, but there was outright misinformation out there.  Somewhere, one poster asked how to do this and got the response that it wasn't possible.  Now, that just got me all the more interested.  The die-hard, problem-solving coder in me just wouldn't let that one rest without a fight. 

So, here's the solution (and some interesting tidbits about the journey).

The problem

Basically, we want to be able to fill a RichTextBox with any RTF document, and then manipulate the objects within that document by using their COM interfaces.  If I were using MFC, I'd call the GetIRichEditOle function and start using the returned interface.  .NET has plenty of features which are already part of the RichTextBox class, but it doesn't provide a way to do that task.

Interop to the Rescue

There are plenty of articles on CodeProject about Interop for the .NET platform, so I won't go into much detail about that here.  However, this solution requires both platform invoke and COM Interop.

First, we have to fill in the gap missing in the RichTextBox control.  To accomplish this, we need to use the SendMessage Windows API call.  We'll use the EM_GETOLEINTERFACE message, which is specific to the RichEdit control.  Once we have the appropriate interop code setup (see the downloadable project), this can be done with the following code.  (Note that we need to be careful to handle the LPARAM parameter correctly.)

 // Alloc a pointer to hold the return value for EM_GETOLEINTERFACE

 IntPtr ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(IntPtr)));
 // Clear the pointer

 Marshal.WriteIntPtr(ptr, IntPtr.Zero); 
 try
 {
     if (0 != API.SendMessage(RichTextBox.Handle, 
                              Messages.EM_GETOLEINTERFACE, IntPtr.Zero, ptr))
     {
         // Read the returned pointer.  It is what we're looking for!

         IntPtr pRichEdit = Marshal.ReadIntPtr(ptr);
         try
         {
              // TODO: Do something with the pointer we just got...

         }
         finally
         {
             Marshal.Release(pRichEdit);
         }
     }
     else
     {
         throw new Exception("EM_GETOLEINTERFACE failed.");
     }
 }
 finally
 {
     // Free the ptr memory.

     Marshal.FreeCoTaskMem(ptr);
 }

Now that we have an IntPtr that is the IRichEditOle interface, we can do something with it. This is where the COM Interop stuff really kicks in.

Wrapping the IRichEditOle interface

To use the IRichEditOle interface we just obtained, we need some way for C# to call the methods on that interface.  Normally, we would find the dll from which that interface is defined, add it as a reference to the VS.NET project, and just start using the generated library.  That is either very difficult or not really possible with the information I've found, BUT we can accomplish the same task manually.

The following code is the C# representation of the IRichEditOle interface.  The ComImport, InterfaceType, and Guid attributes are needed in order for the interface to work, so don't leave them out if you're wrapping another interface in a similar fashion.  Browsing the C++ source code that is supplied with VS.NET is the best way to get this information.  (Note that I didn't test all of these methods, and didn't even bother trying to use the last few.  I'm focusing almost solely on the GetObject method.)

 [ComImport]
 [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
 [Guid("00020D00-0000-0000-c000-000000000046")]
 public interface IRichEditOle
 {
  int GetClientSite(IntPtr lplpolesite);
  int GetObjectCount();
  int GetLinkCount();
  int GetObject(int iob, REOBJECT lpreobject, 
                         [MarshalAs(UnmanagedType.U4)]GetObjectOptions flags);
  int InsertObject(REOBJECT lpreobject);
  int ConvertObject(int iob, CLSID rclsidNew, string lpstrUserTypeNew);
  int ActivateAs(CLSID rclsid, CLSID rclsidAs);
  int SetHostNames(string lpstrContainerApp, string lpstrContainerObj);
  int SetLinkAvailable(int iob, int fAvailable);
  int SetDvaspect(int iob, uint dvaspect);
  int HandsOffStorage(int iob);
  int SaveCompleted(int iob, IntPtr lpstg);
  int InPlaceDeactivate();
  int ContextSensitiveHelp(int fEnterMode);
  //int GetClipboardData(CHARRANGE FAR * lpchrg, uint reco, 

  //                                                     IntPtr lplpdataobj);

  //int ImportDataObject(IntPtr lpdataobj, CLIPFORMAT cf, HGLOBAL hMetaPict);

 } 

Now, we have an interface!  But then what?  Well, since my application only needs the GetObject method, we've got to handle that REOBJECT class.  This is done with the following C# class (it could have been a structure, but then you'd need to pass it as a "ref" parameter to the GetObject function in IRichEditOle.)

 [StructLayout(LayoutKind.Sequential)]
 public class REOBJECT
 {
  public REOBJECT()
  {
  }
  // Size of structure

  public int   cbStruct = Marshal.SizeOf(typeof(REOBJECT));  
  public int   cp = 0;                    // Character position of object

  public CLSID  clsid = new CLSID();      // Class ID of object

  public IntPtr  poleobj = IntPtr.Zero;   // OLE object interface

  public IntPtr  pstg = IntPtr.Zero;      // Associated storage interface

  public IntPtr  polesite = IntPtr.Zero;  // Associated client site interface

  public SIZEL  sizel = new SIZEL();      // Size of object (may be 0,0)

  public uint   dvaspect = 0;             // Display aspect to use

  public uint   dwFlags = 0;              // Object status flags

  public uint   dwUser = 0;               // Dword for user's use

 }

The CLSID member could be handled better, but I provided a structure with the necessary byte size to fill that gap.  We already know the CLSID, anyway.

Putting it all together

So, we have a way to get the IRichEditOle pointer value into an IntPtr variable and we have a C# interface that wraps IRichEditOle.  Now, we can fill in the gap in the first code section of this article.  Using GetTypedObjectForIUnknown, we can take that IntPtr and wrap it in our interface! But first, we need to make sure that we have the honest-to-goodness IRichEditOle interface pointer - not a pointer to another interface in that same COM object.  To do that, we'll use QueryInterface, supplying the GUID that we already used when wrapping the IRichEditOle interface above.

 // Query for the IRichEditOle interface.

 Guid guid = new Guid("00020D00-0000-0000-c000-000000000046");
 Marshal.QueryInterface(pRichEdit, ref guid, out this.IRichEditOlePtr);
       
 // Wrap it in the C# interface for IRichEditOle.

 this.IRichEditOleValue = (IRichEditOle)Marshal.GetTypedObjectForIUnknown(
                                  this.IRichEditOlePtr, typeof(IRichEditOle));
 if (this.IRichEditOleValue == null)
 {
   throw new Exception("Failed to get the object wrapper for the interface.");
 }

Putting that all together into one function, we've got a nifty way to get the IRichEditOle pointer from a RichText Box. 

The code

To make it all nice and easy, I've derived a RichTextBoxPlus class from the RichTextBox class.  It is part of the RichTextBoxPlus.dll project, which also has the needed code for wrapping and using the IRichEditOle interface.  Add that dll as a reference to any project, then use RichTextBoxPlus in place of RichTextBox any time you need the IRichEditOle interface!

The demo project

There is a (lovely) demo application in the download, which loads an RTF document containing a Windows Media Player object.  (I tried to find an object that everyone would have access to.)  It then uses the IRichEditOle object to get an interface to WMPPlayer, and sets the URL to a MIDI file in the executable's folder.  The "Play song" menu lets you use the IRichEditOle interface to start the song, then switches to "Stop song" so you can call the stop function of the WMPPlayer object.  (If you want to use the buttons on Windows Media Player itself, you may need to double-click the object first.  This has something to do with the RTF control itself.)

If you have any questions, comments, or improvements, don't hesitate to contact me.

Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

John Fisher


Member

Occupation: Web Developer
Location: United States United States

Other popular Edit Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 52 (Total in Forum: 52) (Refresh)FirstPrevNext
GeneralRemoving Ole Objects from RTB PinmemberThe Innovator10:24 24 Mar '09  
GeneralRe: Removing Ole Objects from RTB PinmemberJohn Fisher17:38 24 Mar '09  
GeneralRe: Removing Ole Objects from RTB PinmemberThe Innovator18:27 24 Mar '09  
QuestionIs it possible to activate powerpoint object embedded in richtextbox for editing in powerpoint instead of opening as slideshow Pinmembermailtogj4:37 18 Mar '09  
GeneralThanks PinmemberNick Butler0:14 22 Dec '08  
QuestionInsert Object Dialog Box PinmemberChizbabaj23:07 10 Sep '08  
AnswerRe: Insert Object Dialog Box PinmemberJohn Fisher18:01 11 Sep '08  
GeneralRe: Insert Object Dialog Box PinmemberChizbabaj3:47 12 Sep '08  
GeneralRe: Insert Object Dialog Box PinmemberAleksei Krassovskikh0:35 15 Nov '08  
GeneralStoring DOC files PinmemberSteve Straley13:16 9 Aug '07  
GeneralRe: Storing DOC files PinmemberJohn Fisher17:48 9 Aug '07  
Generalplease reply:Having problem with add new property to reobject Pinmembermohadese12:44 6 Aug '07  
GeneralRe: please reply:Having problem with add new property to reobject PinmemberJohn Fisher17:16 6 Aug '07  
GeneralRe: please reply:Having problem with add new property to reobject Pinmembermohadese22:57 6 Aug '07  
GeneralRe: please reply:Having problem with add new property to reobject PinmemberJohn Fisher17:08 7 Aug '07  
GeneralRe: please reply:Having problem with add new property to reobject Pinmembermohadese4:36 8 Aug '07  
GeneralRe: please reply:Having problem with add new property to reobject Pinmembermohadese10:12 16 Aug '07  
GeneralRe: please reply! Pinmembermohadese7:55 17 Aug '07  
GeneralOLE Object field of Access DB using C# Code to Read/Write PinmemberSaghir Ahmed0:36 8 Nov '06  
Generalmisinformation PinmemberDragos Dumitru Sbarlea22:38 13 Sep '06  
GeneralRe: misinformation PinmemberJohn Fisher17:23 6 Aug '07  
GeneralHow to use Insert Object PinmemberRakeshKG2:20 11 Sep '05  
AnswerRe: How to use Insert Object PinmemberAnthony Queen11:18 4 Dec '06  
GeneralThat's all I need Pinmembervamicom091014:16 9 Mar '05  
GeneralHotlink in a RichTextBox ? PinmemberBIGFish200518:54 7 Feb '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Jul 2003
Editor: Nick Parker
Copyright 2003 by John Fisher
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project