Solved

Saving OLE embedded objects

Posted on 1997-07-21
3
413 Views
Last Modified: 2013-12-03
I have a C++ application that uses the 32bit windows SDK.
When the user does a SAVE, it uses the standard Windows I/O routines to open and write the save file (it doesn't use the serialize class).
How can I use 'OleSave' to save embedded objects to this file?
(The file is already open, and application data has already been written to it).
0
Comment
Question by:poster
  • 2
3 Comments
 

Author Comment

by:poster
ID: 1401544
Edited text of question
0
 
LVL 1

Accepted Solution

by:
belov earned 100 total points
ID: 1401545
To save ole object to common file using standard file
routines you should perform these steps:
1. Get IUnknown of object you need to save
2. Query IPersistStorage interface from it
3. Create ole storage on memory
     1. CreateILockBytesOnHGlobal - get ILockBytes interface
     2. StgCreateDocfileOnILockBytes - with ILockBytes
         to get IStorage.
4. Call Save method of IPersistStorage supplying
    pointer to IStorage (pPersistStor->Save(pStorage, true)
5. Call Commit from IStorage pointer pStorage->Commit(STGC_DEFAULT);

6. Now you can obtain information on ILockBytes
pLockBytes->Stat(&statstg, STATFLAG_NONAME);

7, Now you know total size of storage from
statstg structure and can allocate memory
for buffer according to that size
and invoke pLockBytes->ReadAt to fill it with info.

8, Save buffer to file using comon WriteFile.
(And you may want to save classid to allow
yourself to know what object is saved here
and buffer size).

To restore object:

1. Create object according to saved classid
with CoCreateInstance.

2, Create ILockBytes on HGlobal as you did
when saving.

3, Create StgCreateDocfileOnILockBytes on that ILockBytes
with flags
STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE to get IStorage
 
4. Query IPersistStorage from IUnknown of created object
5, Call IPersistStorage->InitNew(IStorage) on created storage
and release storage interfaces

6. Now you must initialize storage with your data,
so read saved buffer from file.

7, Init ILockBytes with read data
  1. Call ILockBytes->SetSize(total buffer size)
  2. Call ILockBytes->WriteAt() to write buffer to storage

8,     ::StgOpenStorageOnILockBytes(ptrLockByte, NULL,
          STGM_READWRITE | STGM_SHARE_EXCLUSIVE, NULL,
           0, &ptrStor)

9, Renew IPersistStorage interface from your object and
call IPersistStorage->Load(ptrStor);

At that point your ole object is restored.

0
 
LVL 1

Expert Comment

by:belov
ID: 1401546
As for OleSave/OleLoad, you can use these functions
as well using the technique described in my answer
to get raw buffer data from storage.
In that case you will need to supply pointers
to IPersistStorage and IStorage created on ILockBytes
to those functions, leaving code that obtains
these interfaces unchanged.

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
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 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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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