OLETEST.CPP
上传用户:zdlsnail
上传日期:2007-01-06
资源大小:16k
文件大小:2k
源码类别:

压缩解压

开发平台:

Visual C++

  1. #include <ole2.h>
  2. #include <ole2ver.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <windows.h>
  6. #include <string.h>
  7. BOOL InitCOM()
  8. {
  9.    DWORD coVersion=CoBuildVersion();
  10.    if( HIWORD(coVersion)!=rmm||LOWORD(coVersion)<rup )
  11.    {
  12.       printf("bad version of COMn");
  13.       return FALSE ; 
  14.         }
  15.    HRESULT hErr=CoInitialize(NULL);
  16.    if(FAILED(GetScode(hErr))) 
  17.      { printf("Failed to initialize OLE!n");
  18.        return  FALSE ; }
  19.  return TRUE ;
  20. }
  21. void DeinitCOM()
  22. {
  23.    CoUninitialize();
  24. }
  25. void readOleFile( char * mfilename,
  26.   char * mstreamname,
  27.   char * buffer,
  28.   long * length)
  29. {
  30.   HRESULT    hErr ;
  31.   LPSTORAGE  pStg ;
  32.   LPSTREAM   pStm ;
  33.   STATSTG  * pStat;
  34.   LARGE_INTEGER dlibMove=0;
  35.   wchar_t wfilename[120];
  36.   wchar_t wstreamname[64];
  37.   int iMessageSize=96,k,j;
  38.   if( !InitCOM()) return;
  39.   while((SetMessageQueue(iMessageSize)==0)&&(iMessageSize>0)){
  40.       iMessageSize-=8;}
  41.  
  42.   j=strlen(mfilename);
  43.   k=mbstowcs(wfilename,mfilename,j+1);
  44.   j=strlen(mstreamname);
  45.   k=mbstowcs(wstreamname,mstreamname,j+1);
  46.   
  47.   hErr=StgOpenStorage(
  48.           wfilename,
  49.   NULL,
  50.   STGM_TRANSACTED|STGM_WRITE,
  51.   NULL,
  52.   0,
  53.   & pStg);
  54.  if(FAILED(hErr)) { DeinitCOM();* length=0;return;} 
  55.  hErr=pStg->OpenStream( wstreamname,
  56.                     0,
  57. STGM_READWRITE|STGM_DIRECT|STGM_SHARE_EXCLUSIVE,
  58.                         0,
  59. &pStm );
  60.  if(FAILED(hErr)) {
  61.    pStg->Release(); DeinitCOM();*length=0;return;}
  62.  hErr=pStm->Seek(dlibMove,STREAM_SEEK_END,NULL); 
  63.  j=strlen(buffer);
  64.  hErr=pStm->Write(buffer,j,(unsigned long *)length);
  65.  hErr=pStm->Commit(0);
  66.  hErr=pStm->Release();
  67.  hErr=pStg->Commit(0);
  68.  hErr=pStg->Release();
  69.  DeinitCOM();return;}
  70. void main()
  71. {
  72. char * buffer;
  73. long length;
  74. char  mfilename[]="c:\OLE_1.OLE";
  75. char  mstreamname[]="CompObj";
  76.     buffer=(char *)malloc(0Xe0e);
  77. readOleFile(mfilename,mstreamname,buffer,&length);
  78.     puts(buffer);
  79. free((char *)buffer);
  80. }