Solved

Saving OLE embedded objects

Posted on 1997-07-21
3
410 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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 is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now