WRITEOLE.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.   LARGE_INTEGER dlibMove;
  34.   wchar_t wfilename[120];
  35.   wchar_t wstreamname[64];
  36.   int iMessageSize=96,k,j;
  37.   dlibMove.LowPart=0;
  38.   dlibMove.HighPart=0;
  39.   if( !InitCOM()) return;
  40.   while((SetMessageQueue(iMessageSize)==0)&&(iMessageSize>0)){
  41.       iMessageSize-=8;}
  42.  
  43.   j=strlen(mfilename);
  44.   k=mbstowcs(wfilename,mfilename,j+1);
  45.   j=strlen(mstreamname);
  46.   k=mbstowcs(wstreamname,mstreamname,j+1);
  47.   
  48.   hErr=StgOpenStorage(
  49.           wfilename,
  50.   NULL,
  51.   STGM_TRANSACTED|STGM_WRITE,
  52.   NULL,
  53.   0,
  54.   & pStg);
  55.  if(FAILED(hErr)) { DeinitCOM();* length=0;return;} 
  56.  hErr=pStg->OpenStream( wstreamname,
  57.                     0,
  58. STGM_READWRITE|STGM_DIRECT|STGM_SHARE_EXCLUSIVE,
  59.                         0,
  60. &pStm );
  61.  if(FAILED(hErr)) {
  62.    pStg->Release(); DeinitCOM();*length=0;return;}
  63.  hErr=pStm->Seek(dlibMove,STREAM_SEEK_END,NULL); 
  64.  if(FAILED(hErr)) {
  65.     pStm->Release();
  66. pStg->Release();
  67.    DeinitCOM();
  68.    *length=0;
  69.    return;}
  70.  j=strlen(buffer);
  71.  hErr=pStm->Write(buffer,j,(unsigned long *)length);
  72.  if(FAILED(hErr)) {
  73.     pStm->Release();
  74. pStg->Release();
  75.    DeinitCOM();
  76.    *length=0;
  77.    return;}
  78.  hErr=pStm->Commit(STGC_DEFAULT);
  79.  if(FAILED(hErr)) {
  80.     pStm->Release();
  81. pStg->Release();
  82.    DeinitCOM();
  83.    *length=0;
  84.    return;}
  85.  hErr=pStm->Release();
  86.  hErr=pStg->Commit(STGC_DEFAULT);
  87.  if(FAILED(hErr)) {
  88.    pStg->Release();
  89.    DeinitCOM();
  90.    *length=0;
  91.    return;}
  92.  hErr=pStg->Release();
  93.  DeinitCOM();return;
  94. }