?? ihelper.cpp
字號:
/* This sample application and corresponding sample code is provided
* for example purposes only. It has not undergone rigorous testing
* and as such should not be shipped as part of a final application
* without extensive testing on the part of the organization releasing
* the end-user product.
*/
#include "stdafx.h"
#include "IHelper.h"
CString ReadStream(IStorage* stg, const CString streamName, long size) /*throw (CFileException*)*/ {
ASSERT(stg!=NULL);
CString result;
IStream* stream;
stream = OpenStream(stg, streamName);
result = ReadStream(stream, size);
CloseStream(stream);
return result;
}
CString ReadStream(IStream* stream, long size) /*throw (CFileException*)*/ {
ASSERT(stream != NULL);
CString result;
HRESULT err;
ULONG read;
if(size == 0)
size = GetStreamSize(stream);
err = stream->Read(result.GetBuffer(size+1), size, &read);
result.ReleaseBuffer();
result.SetAt(size, '\0');
if(err != S_OK)
CFileException::ThrowOsError(err);
return result;
}
IStream* OpenStream(IStorage* stg, const CString streamName, bool write) /*throw (CFileException*)*/ {
ASSERT(stg != NULL);
IStream* outStream;
BSTR bStrName = streamName.AllocSysString();
HRESULT result;
result = stg->OpenStream(bStrName, NULL, (write ? STGM_READWRITE : STGM_READ) | STGM_SHARE_EXCLUSIVE, 0, &outStream);
if(result != S_OK)
CFileException::ThrowOsError(result, streamName);
SysFreeString(bStrName);
return outStream;
}
IStream* CreateStream(IStorage* stg, const CString name) /*throw (CFileException*)*/ {
ASSERT(stg != NULL);
IStream* outStream;
BSTR bStrName = name.AllocSysString();
HRESULT result;
result = stg->CreateStream(bStrName, STGM_WRITE | STGM_SHARE_EXCLUSIVE, NULL, NULL, &outStream);
if(result != S_OK)
CFileException::ThrowOsError(result, name);
SysFreeString(bStrName);
return outStream;
}
void CreateStream(IStorage* stg, const CString name, const CString value) /*throw (CFileException*)*/ {
ASSERT(stg != NULL);
IStream* outStream;
outStream = CreateStream(stg, name);
WriteStream(outStream, value);
CloseStream(outStream);
}
void WriteStream(IStream* outStream, const CString value) /*throw (CFileException*)*/ {
ASSERT(outStream != NULL);
HRESULT result;
result = outStream->Write((const char*)value, value.GetLength(), NULL);
if(result != S_OK)
CFileException::ThrowOsError(result);
}
void CloseStream(IStream* stream) {
ASSERT(stream != NULL);
stream->Release();
}
long GetStreamSize(IStream* stream) /*throw (CFileException*)*/ {
ASSERT(stream != NULL);
STATSTG info;
HRESULT result;
result = stream->Stat(&info, STATFLAG_NONAME);
if(result != S_OK)
CFileException::ThrowOsError(result);
return info.cbSize.LowPart;
}
IStorage* OpenStorage(const CString path, bool write) /*throw (CFileException*)*/ {
HRESULT result;
BSTR bPath = path.AllocSysString();
IStorage* stg;
result = StgOpenStorage(bPath, NULL, (write ? STGM_READWRITE : STGM_READ) | STGM_SHARE_EXCLUSIVE, NULL, NULL, &stg);
if(result != S_OK)
CFileException::ThrowOsError(result, path);
SysFreeString(bPath);
return stg;
}
IStorage* CreateStorage(const CString path) /*throw (CFileException*)*/ {
HRESULT result;
BSTR bPath = path.AllocSysString();
IStorage* stg;
result = StgCreateDocfile(bPath, STGM_READWRITE | STGM_CREATE | STGM_TRANSACTED | STGM_SHARE_EXCLUSIVE, NULL, &stg);
if(result != S_OK)
CFileException::ThrowOsError(result, path);
SysFreeString(bPath);
return stg;
}
void SaveStorage(IStorage* stg) {
ASSERT(stg != NULL);
stg->Commit(STGC_DEFAULT);
}
IStorage* OpenSubstorage(IStorage* stg, const CString substgName, bool write) /*throw (CFileException*)*/ {
ASSERT(stg != NULL);
IStorage* outSubstg;
HRESULT result;
BSTR bName = substgName.AllocSysString();
result = stg->OpenStorage(bName, NULL, (write ? STGM_READWRITE : STGM_READ) | STGM_SHARE_EXCLUSIVE, NULL, NULL, &outSubstg);
if(result != S_OK)
CFileException::ThrowOsError(result);
SysFreeString(bName);
return outSubstg;
}
void CloseStorage(IStorage* stg) /*throw (CFileException*)*/ {
ASSERT(stg != NULL);
stg->Release();
}
IStorage* CreateSubstorage(IStorage* stg, const CString substgName) /*throw (CFileException*)*/ {
ASSERT(stg != NULL);
IStorage* outSubstg;
HRESULT result;
BSTR bName = substgName.AllocSysString();
result = stg->CreateStorage(bName, STGM_WRITE | STGM_SHARE_EXCLUSIVE, NULL, NULL, &outSubstg);
if(result != S_OK)
CFileException::ThrowOsError(result);
SysFreeString(bName);
return outSubstg;
}
void EnumElementNames(CStringList* names, IStorage* stg) /*throw (CFileException*)*/ {
CString str;
IEnumSTATSTG* enumElts;
HRESULT err;
STATSTG* elts;
ULONG num = 20;
IMalloc* allocator;
err = stg->EnumElements(NULL, NULL, NULL, &enumElts);
if(err != S_OK)
CFileException::ThrowOsError(S_OK);
CoGetMalloc(1, &allocator);
elts = new STATSTG[20];
do {
err = enumElts->Next(20, elts, &num);
for(ULONG i = 0; i < num; i++) {
names->AddTail(elts[i].pwcsName);
allocator->Free(elts[i].pwcsName);
}
} while(err == S_OK);
delete elts;
enumElts->Release();
allocator->Release();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -