Solved

Saving OLE embedded objects

Posted on 1997-07-21
3
407 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
Comment Utility
Edited text of question
0
 
LVL 1

Accepted Solution

by:
belov earned 100 total points
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

10 Experts available now in Live!

Get 1:1 Help Now