繁体中文
设为首页
加入收藏
当前位置:程序开发首页 >> 其他开发语言 >> 在VC中使用FileSystemObject

在VC中使用FileSystemObject

2005-10-23 10:34:09  作者:vcmute  来源:互联网  浏览次数:13  文字大小:【】【】【
简介:今天在论坛里遇到这样一个问题,如果不用递归的方式得到一个文件夹的大小?第一反应就是使用FSO可以通过folder.size属性得到,终于在网上找到How to use scrrun.dll in VisualC++?http://www.xtremevbtalk.com/ar...
关键字:FileSystemObject

今天在论坛里遇到这样一个问题,如果不用递归的方式得到一个文件夹的大小?

第一反应就是使用FSO可以通过folder.size属性得到,终于在网上找到

How to use scrrun.dll in VisualC++?

http://www.xtremevbtalk.com/archive/index.php/t-230203.html

虽然并未完成,但基本框架已经出来

使用方法大致如下

1.通过OLEView得到idl,并作相应修改

2.通过midl得到头文件

3.include该头文件通过CoCreateInstance调用

添加代码如下:

#include "scrrun.h"

#define SAFE_RELEASE(o) if(o){o->Release();o=NULL;}

int GetFolderSize(BSTR FolderPath)

{

IFileSystem *FileSystem=NULL;

IFolder *Folder=NULL;

LONG nRet = -1;

HRESULT hr;

hr = CoCreateInstance(CLSID_FileSystemObject,NULL,CLSCTX_INPROC_SERVER,IID_IFileSystem, (void**) &FileSystem);

if(FAILED(hr))goto quit;

hr = FileSystem->GetFolder(FolderPath,&Folder);

if(FAILED(hr))goto quit;

if(Folder)

{

VARIANT vt={0};

Folder->get_Size(&vt);

nRet = vt.lVal;

}

quit:

SAFE_RELEASE(Folder);

SAFE_RELEASE(FileSystem);

return nRet;

}

调用方法

GetFolderSize(L"c:\\windows\\system32");

参考文献

Calling Visual Basic ActiveX DLLs from Visual C++

http://www.codeproject.com/com/vb_from_vc.asp

Component Object Model API

http://www.catalyst.com/support/help/internetmail/index.html?page=html%2Fmail%2Fguide%2Fobjectmodel.html

注:该函数暂不适宜超过2G的文件夹,但理论上应该可以

责任编辑:admin
相关文章