Click here to Skip to main content
15,914,109 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hello all,
I have created a compound file with .stg extension using StgCreateDocfile function of structured storage. Now i want to add gard disk files to that compound file(.stg). Can anybody suggest me how to move files to compound file(.stg) which act as mini file system so that i can backup the data through programing. Thanking u in advance.....
Posted
Comments
Sergey Alexandrovich Kryukov 12-Mar-11 18:23pm    
The idea seems interesting however application of it is not apparent, my 4.
Now, why it seems to be a problems? What did you try so far?
--SA
MartinTaylor 13-Mar-11 3:14am    
Im trying to backup(copy) the files in single file system(.stg). How can i do that? Can i use MFC classes to copy files to my mini filesystem(.stg)?

To read from a storage:

Open the compound file with StgOpenStorage() - returns an IStorage

Use IStorage->OpenStream() to open a stream (use unicode again) - Will return an IStream

Use IStream->Read() to read from the stream
Eg:

BYTE *b;
// allocate some memory to b
HRESULT res = stream->Read((void*)b,xxx,NULL);
if(res!=S_OK)
   // failed to read.


If you wish to know the size of the stream contained in the storage (to allocate memory or whatever reason),
create a STATSTG structure & pass it to IStream->Stat()

STATSTG stat;
stream->Stat(&stat,STATFLAG_NONAME);
// stat.cbSize.LowPart
// will have the file size ( < 4gb. Else consider HighPart for size > 4gb or QuadPart for 8 byte int)
 
Share this answer
 
Comments
Dalek Dave 13-Mar-11 17:11pm    
Also a good answer.
If you've gone that far, I wonder what's stopping you from doing it.

You will receive an IStorage from StgCreateDocFile()

Use IStorage->CreateStream() to create a stream (A stream in a storage will be the equivalent of a file in a file system)
** Note that you must pass in unicode strings into it. (check MultiByteToWideChar() & WideCharToMultiByte() functions for it - there must be some easier unicode functions in MFC but i do not deal with MFC)

CreateStream() should return you an IStream
Use IStream->Write() to write (consider using IStream->SetSize before write, this may prevent some fragmentation)

Use IStream->Release() to finish

& finally use IStorage->Commit()

PS: You'll have to check MSDN for details on each of the functions
 
Share this answer
 
v2
Comments
MartinTaylor 13-Mar-11 14:47pm    
I have done with creating stream and storage objects(setting stream size,releasing it etc) inside compound file.The only problem that im facing is while using ISequencialTream::Read.
HRESULT Read(
[out] void *pv,
[in] ULONG cb,
[out] ULONG *pcbRead
);
I dont know how to read data from buffer to which pv is pointing. If i have a file then how it is read into the buffer and using pointer pv to stream. Sir, Can u suggest me the code for that? thanx for ur prev valuable reply...
Dalek Dave 13-Mar-11 17:11pm    
Good Answer.
MartinTaylor 13-Mar-11 17:36pm    
Now im facing some other problem for following code

EXTERN_C void wmain()
{
HRESULT hr = S_OK;
IStorage *pIstor;
IStorage *pIstor1;
WCHAR *pwszError = L"";
IStream* pStream;
StgCreateDocfile(L"sumit",STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE, 0, &pIstor); hr=pIstor->CreateStorage(L"sumit444",STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE,0, 0, &pIstor1);
if (pIstor->CreateStream(L"MyStreamName",STGM_CREATE |STGM_READWRITE | STGM_SHARE_EXCLUSIVE,0, 0, &pStream) == S_OK)
{ //some code }
SetCurrentDirectory(L"E:");
IStorage* pStg;
hr=StgCreateStorageEx(L"E",STGM_CREATE | STGM_READWRITE | STGM_TRANSACTED,STGFMT_STORAGE,0,0,0,IID_IStorage,(void**)pStg);
.
.
.
.Im getting runtime break condition stating "Run-Time Check Failure #3 - The variable 'pStg' is being used without being initialized" There is no problem pIstor object which is defined earlier but its there in pStg.. im not understanding why is this happening?
strogg 14-Mar-11 12:56pm    
I see you've made a simple mistake in the call to StgCreateStorageEx(). The last param should be of type void ** (a pointer to pointer)
The proper syntax is (void**)&pStg
Try it & let me know
MartinTaylor 16-Mar-11 17:18pm    
Sir,
I have resolved the problem in StgCreateStorageEx().
Now i want to read data from my hard disk file(eg. any .doc, .mp3, .pdf) and write it in Stream that i have created for backup. How can i us IStream::Write() for this?
HRESULT Write(
void const* pv,
ULONG cb,
ULONG* pcbWritten
);
can pv be the pointer to file to be read for writting instead of buffer?
If not how to use buffer for reading and then writing in stream?
thank u...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900