Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

For chensu : Rich OLE Interface

Posted on 1999-06-28
7
Medium Priority
?
621 Views
Last Modified: 2013-12-03
chensu :

I read your comment , giving the URL on "how to embedd a bitmap to a RTF document" . I tried to convert the source from MFC to plain API using this function , that is supposed to draw the given bitmap to the current position of the cursor in the richedit "rEdit" : ( I 've not make it doing cleanups , so don't except a delete[] to a new for example )

The problem is , that the OleCreateFromFile always fails , with an error of "file not found" , although the file exists . Can you figure whats wrong with the code ?

int InsertBitmap(HWND rEdit,LPCOLESTR lpszFileName)
   {
   LPRICHEDITOLE      m_pRichEditOle;
   LPOLEOBJECT      m_lpObject;
   LPSTORAGE         m_lpStorage;
   LPOLECLIENTSITE      m_lpClientSite;

   ::SendMessage((HWND)rEdit, EM_GETOLEINTERFACE, 0, (LPARAM)&m_pRichEditOle);
      if (m_pRichEditOle == NULL)
      return false;

   // Create the embedded object

      LPLOCKBYTES lpLockBytes = NULL;
   
      CLSID clsid = CLSID_NULL;
   OLERENDER render = OLERENDER_DRAW;
      CLIPFORMAT cfFormat = 0;
   LPFORMATETC lpFormatEtc = new FORMATETC;
   SCODE sc;

      sc = ::CreateILockBytesOnHGlobal(NULL, TRUE, &lpLockBytes);      if (sc != S_OK)
      if (sc != S_OK)
      return false;

      sc = ::StgCreateDocfileOnILockBytes(lpLockBytes,
            STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &m_lpStorage);
      if (sc != S_OK)
      return false;

     
      // fill in FORMATETC struct      FORMATETC formatEtc;      lpFormatEtc = &formatEtc;
      lpFormatEtc->cfFormat = cfFormat;
   lpFormatEtc->ptd = NULL;
   lpFormatEtc->dwAspect = DVASPECT_CONTENT;
   lpFormatEtc->lindex = -1;
      lpFormatEtc->tymed = (DWORD)-1;      // attempt to create the object
      m_pRichEditOle->GetClientSite(&m_lpClientSite);
      sc = ::OleCreateFromFile(clsid,
      (LPCOLESTR)lpszFileName,
            IID_IUnknown, OLERENDER_NONE, lpFormatEtc, m_lpClientSite, m_lpStorage,
            (void**)&m_lpObject);

   if (sc != S_OK)
      {
            char lpMsgBuf[10000];
            ::FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL,
                                     ::GetLastError(),
                                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                                     (LPTSTR) &lpMsgBuf, 10000, NULL );
            MessageBox( 0, lpMsgBuf,"Error", MB_OK );
      return false;
      }

      // m_lpObject is currently an IUnknown, convert to IOleObject
      if (m_lpObject != NULL)      {
               LPUNKNOWN lpUnk = m_lpObject;
            lpUnk->QueryInterface(IID_IOleObject, (void**)&m_lpObject);
      lpUnk->Release();
            if (m_lpObject == NULL)
         return false;
      }
      // all items are "contained" -- this makes our reference to this object
      //  weak -- which is needed for links to embedding silent update.
      OleSetContainedObject(m_lpObject, TRUE);

   // 3 . Ready !
   REOBJECT reobject;
   ZeroMemory(&reobject, sizeof(REOBJECT));
      reobject.cbStruct = sizeof(REOBJECT);

      sc = m_lpObject->GetUserClassID(&clsid);
   if (sc != S_OK)
            return false;
   reobject.clsid = clsid;
      reobject.cp = REO_CP_SELECTION;
   reobject.dvaspect = DVASPECT_CONTENT;
      reobject.dwFlags = REO_RESIZABLE | REO_BELOWBASELINE;
   reobject.dwUser = 0;
      reobject.poleobj = m_lpObject;
      reobject.polesite = m_lpClientSite;
      reobject.pstg = m_lpStorage;
   SIZEL sizel;
   sizel.cx = sizel.cy = 0;
      reobject.sizel = sizel;

   // 4 . Insert
      m_pRichEditOle->InsertObject(&reobject);
   return true;
   }
0
Comment
Question by:WxW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 23

Expert Comment

by:naveenkohli
ID: 1401509
Just a comment..
Have you initialized your COM libraries..??
OleInitialize (NULL);
0
 
LVL 6

Author Comment

by:WxW
ID: 1401510
Yes.

0
 
LVL 23

Expert Comment

by:chensu
ID: 1401511
1. lpszFileName should be a Unicode string.
2. Make sure m_lpClientSite is valid.
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 6

Author Comment

by:WxW
ID: 1401512
The m_lpClientSite is valid

You mean that passing a standard ASCIIZ string won't work ? How can I convert it ?

for example , convert "c:\\image.bmp\0"
0
 
LVL 23

Expert Comment

by:chensu
ID: 1401513
ANSI strings won't work. Pass something like L"c:\\image.bmp".

// To convert an ANSI string lpszA to a Unicode string lpszW

int nLen = ::MultiByteToWideChar(CP_ACP, 0, lpszA, -1, NULL, NULL);

LPWSTR lpszW = new WCHAR[nLen];

::MultiByteToWideChar(CP_ACP, 0, lpszA, -1, lpszW, nLen);

// use lpszW here

delete []lpszW;



0
 
LVL 6

Author Comment

by:WxW
ID: 1401514
Great , that did it . Please answer .
I wonder why it wasn't made to work with ansi strings .
0
 
LVL 23

Accepted Solution

by:
chensu earned 200 total points
ID: 1401515
Thank you.

>I wonder why it wasn't made to work with ansi strings.

All the COM/OLE functions use Unicode.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question