面对现实,超越自己
逆水行舟,不进则退
posts - 269,comments - 32,trackbacks - 0
本文转自:http://www.cppblog.com/humanchao/archive/2008/07/02/55154.html


#import   
"scrrun.dll"   raw_interfaces_only

// 参数格式:"c:\" 或 "c:\test"
ULONGLONG GetPathUseSpace(
const char *szPath)
{
    ASSERT(szPath 
!= NULL);

    
int nLen = strlen(szPath);
    
if (nLen == 0)
        
return 0;

    ULONGLONG result 
= 0;

    
if (nLen == 3)      // c:\
    {
        ULARGE_INTEGER nFreeBytesAvailable;
        ULARGE_INTEGER nTotalNumberOfBytes;
        ULARGE_INTEGER nTotalNumberOfFreeBytes;
        
//
        if (GetDiskFreeSpaceEx(szPath,
              
&nFreeBytesAvailable,
              
&nTotalNumberOfBytes,
              
&nTotalNumberOfFreeBytes))
        {
            result 
= nTotalNumberOfBytes.QuadPart - nFreeBytesAvailable.QuadPart;
        }
    }
    
else
    {
        CoInitialize(NULL);  
        {  
            
try  
            {  
                Scripting::IFileSystem3Ptr   fs;  
                fs.CreateInstance(__uuidof(Scripting::FileSystemObject)); 
                
                Scripting::IFolderPtr   folder;  
                fs
->GetFolder(_bstr_t(szPath),&folder);
                
                _variant_t vsize;
                folder
->get_Size(&vsize);
                result 
= (double)vsize;
            }  
            
catch(_com_error &e)  
            {  
                result 
= -1;
            }  
        }  

        CoUninitialize();   
    }

    
return result;
}

Scrrun.dll枚举文件
CoInitialize(NULL); 

    
try 
    {  
        Scripting::IFileCollectionPtr   files; 
        folder
-> get_Files(&files); 
        
long   count   =   0
        files
-> get_Count(&count); 
        cout 
< < "files   count: " < <count < <endl; 
        IUnknown   
*pUnk=NULL; 
        HRESULT   hr   
=   files-> get__NewEnum(&pUnk); 
        
if(SUCCEEDED(hr)) 
        { 
            CComPtr 
<IEnumVARIANT>   pEnum; 
            hr   
=   pUnk-> QueryInterface(IID_IEnumVARIANT,(void**)&pEnum); 
            
if(pEnum) 
            { 
                pEnum
-> Reset(); 
                ULONG   fget   
=   1
                
while(SUCCEEDED(hr)&&fget> 0
                { 
                    Scripting::IFilePtr   file; 
                    _variant_t   varfile; 
                    hr   
=   pEnum-> Next(1,&varfile,&fget); 
                    
if(SUCCEEDED(hr)   &&   fget> 0
                    { 
                        file   
=              varfile.pdispVal; 
                        BSTR   bspath; 
                        file
-> get_Path(&bspath); 
                        cout 
< < "file   path: " < <(LPCTSTR)_bstr_t(bspath) < <endl; 
                    } 
                    
else 
                    { 
                        cout 
< < "   get   file     return   " < <hr < <endl; 
                    } 
                } 
            } 
        } 
    } 
    
catch(_com_error   &e) 
    { 
         _bstr_t   bstrSource(e.Source()); 
         _bstr_t   bstrDescription(e.Description()); 
          cout 
< < "Get   directory    size   via   FileSystemObject,   by    masterz " < <endl; 
          cout 
< < "COM   error    occurred,source: " < <(LPCTSTR)bstrSource < <endl; 
          cout 
< < "Description: " < <(LPCTSTR)bstrDescription < <endl; 
    } 

CoUninitialize();
posted on 2012-04-13 09:33 王海光 阅读(779) 评论(0)  编辑 收藏 引用 所属分类: C++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理