第一种方法:
下面的程序可以给iis增加一个虚拟目录
下面的程序是用bcb写的,我不用delphi,
所以没安装delphi,不过可以很容易化成delphi
#define INITGUID
#include <iadmw.h>
#include <iiscnfg.h>
CoInitialize(NULL);
try
{
IMSAdminBase* pi;
int err=CoCreateInstance(CLSID_MSAdminBase, NULL, CLSCTX_ALL,IID_IMSAdminBase, (void **) &pi);
if(err!=S_OK)
throw Exception("Error:"+IntToHex(err,8));
try
{
METADATA_HANDLE hmeta;
err=pi->OpenKey(METADATA_MASTER_ROOT_HANDLE,L"/lm/w3svc",METADATA_PERMISSION_READ|METADATA_PERMISSION_WRITE,0,&hmeta);
if(err!=ERROR_SUCCESS)
throw Exception("Error:"+IntToHex(err,8));
err=pi->AddKey(hmeta,L"/1/root/testVdir");
if(err!=ERROR_SUCCESS)
ShowMessage("addkey Error:"+IntToHex(err,8));
wchar_t buf1[]=L"IIsWebVirtualDir";
METADATA_RECORD rec1={MD_KEY_TYPE,METADATA_NO_ATTRIBUTES,IIS_MD_UT_SERVER,STRING_METADATA,sizeof buf1,(BYTE*)buf1,0};
err=pi->SetData(hmeta,L"/1/root/testVdir",&rec1);
if(err!=ERROR_SUCCESS)
ShowMessage("setdata Error:"+IntToHex(err,8));
wchar_t buf2[]=L"e://temp//testVdir";
METADATA_RECORD rec2={MD_VR_PATH,METADATA_INHERIT,IIS_MD_UT_FILE,STRING_METADATA,sizeof buf2,(BYTE*)buf2,0};
err=pi->SetData(hmeta,L"/1/root/testVdir",&rec2);
if(err!=ERROR_SUCCESS)
ShowMessage("setdata Error:"+IntToHex(err,8));
pi->CloseKey(hmeta);
ShowMessage("ok");
}
__finally
{
pi->Release();
}
}
__finally
{
CoUninitialize();
}
注:在c++程序中 L"abcd" 是表示unicode字符串"abcd"(在常数字符串前加L表示)