Solved

Saving OLE embedded objects

Posted on 1997-07-21
3
422 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
[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
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
Issue testing the Class Library 4 68
Need to impress with my knowledge of .NET 5 104
Dialogbox API leak? 18 119
Visual Studio: built-in keystroke automation 2 90
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…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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