OLETEST.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 ;
- STATSTG * pStat;
- LARGE_INTEGER dlibMove=0;
- wchar_t wfilename[120];
- wchar_t wstreamname[64];
- int iMessageSize=96,k,j;
- 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);
- j=strlen(buffer);
- hErr=pStm->Write(buffer,j,(unsigned long *)length);
- hErr=pStm->Commit(0);
- hErr=pStm->Release();
- hErr=pStg->Commit(0);
- hErr=pStg->Release();
- DeinitCOM();return;}
- void main()
- {
- char * buffer;
- long length;
- char mfilename[]="c:\OLE_1.OLE";
- char mstreamname[]="CompObj";
- buffer=(char *)malloc(0Xe0e);
- readOleFile(mfilename,mstreamname,buffer,&length);
- puts(buffer);
- free((char *)buffer);
- }