Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Saving OLE embedded objects

Posted on 1997-07-21
3
Medium Priority
?
453 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 200 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

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.  

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

783 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