WRITEOLE.CPP
上传用户:zdlsnail
上传日期:2007-01-06
资源大小:16k
文件大小:2k
- #include <ole2.h>
- #include <ole2ver.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <string.h>
- BOOL InitCOM()
- {
- DWORD coVersion=CoBuildVersion();
- if( HIWORD(coVersion)!=rmm||LOWORD(coVersion)<rup )
- {
- printf("bad version of COMn");
- return FALSE ;
- }
- HRESULT hErr=CoInitialize(NULL);
- if(FAILED(GetScode(hErr)))
- { printf("Failed to initialize OLE!n");
- return FALSE ; }
- return TRUE ;
- }
- void DeinitCOM()
- {
- CoUninitialize();
- }
- void readOleFile( char * mfilename,
- char * mstreamname,
- char * buffer,
- long * length)
- {
- HRESULT hErr ;
- LPSTORAGE pStg ;
- LPSTREAM pStm ;
- LARGE_INTEGER dlibMove;
- wchar_t wfilename[120];
- wchar_t wstreamname[64];
- int iMessageSize=96,k,j;
- dlibMove.LowPart=0;
- dlibMove.HighPart=0;
- if( !InitCOM()) return;
- while((SetMessageQueue(iMessageSize)==0)&&(iMessageSize>0)){
- iMessageSize-=8;}
-
- j=strlen(mfilename);
- k=mbstowcs(wfilename,mfilename,j+1);
- j=strlen(mstreamname);
- k=mbstowcs(wstreamname,mstreamname,j+1);
-
- hErr=StgOpenStorage(
- wfilename,
- NULL,
- STGM_TRANSACTED|STGM_WRITE,
- NULL,
- 0,
- & pStg);
- if(FAILED(hErr)) { DeinitCOM();* length=0;return;}
- hErr=pStg->OpenStream( wstreamname,
- 0,
- STGM_READWRITE|STGM_DIRECT|STGM_SHARE_EXCLUSIVE,
- 0,
- &pStm );
- if(FAILED(hErr)) {
- pStg->Release(); DeinitCOM();*length=0;return;}
- hErr=pStm->Seek(dlibMove,STREAM_SEEK_END,NULL);
- if(FAILED(hErr)) {
- pStm->Release();
- pStg->Release();
- DeinitCOM();
- *length=0;
- return;}
- j=strlen(buffer);
- hErr=pStm->Write(buffer,j,(unsigned long *)length);
- if(FAILED(hErr)) {
- pStm->Release();
- pStg->Release();
- DeinitCOM();
- *length=0;
- return;}
- hErr=pStm->Commit(STGC_DEFAULT);
- if(FAILED(hErr)) {
- pStm->Release();
- pStg->Release();
- DeinitCOM();
- *length=0;
- return;}
- hErr=pStm->Release();
- hErr=pStg->Commit(STGC_DEFAULT);
- if(FAILED(hErr)) {
- pStg->Release();
- DeinitCOM();
- *length=0;
- return;}
- hErr=pStg->Release();
- DeinitCOM();return;
- }