//VC6.0+SP5, SDK200210, windows 2000 server
#include <windows.h>
#include <Iads.h>
#include <comdef.h>
#include <tchar.h>
#include <stdio.h>
#include <Adshlp.h>
#pragma comment(lib,"ActiveDS")
#pragma comment(lib,"adsiid")
BOOL CreateWebServer(LPCTSTR bindaddress,LPCTSTR domain,LPCTSTR DiskPath);
void main()
{
CoInitialize(NULL);
if(TRUE==CreateWebServer(_T("192.168.0.224:80"),_T("www.masterz.com"),_T("d://tmp")))
printf("create site ok/n");
else
printf("create site failed/n");
CoUninitialize();
}
BOOL CreateWebServer(LPCTSTR bindaddress,LPCTSTR domain,LPCTSTR pathname)
{
if(bindaddress==NULL||NULL==domain||NULL==pathname)
return FALSE;
IADsContainer *pCont=NULL;
IADs* pAds=NULL;
IADs* pVrAds=NULL;
IADsServiceOperations *pSrvOp=NULL;
IDispatch *pDisp = NULL;
IDispatch *pVrDisp = NULL;
_bstr_t WNumer="123";
_bstr_t newBindings=_bstr_t(bindaddress)+":"+domain;
HRESULT hr;
if(ADsGetObject(L"IIS://localhost/w3svc",IID_IADsContainer,(void**)&pCont)==S_OK)
{
if(pCont->Create(L"IIsWebServer",WNumer,&pDisp)==S_OK)
{
hr=pDisp->QueryInterface(IID_IADs, (void**)&pAds);
hr=pDisp->QueryInterface(IID_IADsServiceOperations, (void**)&pSrvOp);
pAds->Put(L"ServerSize",_variant_t(long(1)));
pAds->Put(L"ServerComment",_variant_t(_bstr_t("masterz")));
pAds->Put(L"ServerBindings",_variant_t(newBindings));
pAds->SetInfo();
hr=pCont->GetObject(L"IIsWebServer",(WNumer),&pDisp);
if(pDisp->QueryInterface(IID_IADsContainer,(void**)&pCont)==S_OK)
{
if(pCont->Create(L"IIsWebVirtualDir",L"Root",&pVrDisp)==S_OK)
{
hr=pVrDisp->QueryInterface(IID_IADs, (void**)&pVrAds);
pVrAds->Put(L"AccessRead",_variant_t(true));
pVrAds->Put(L"AccessWrite",_variant_t(true));
pVrAds->Put(L"AccessScript",_variant_t(true));
pVrAds->Put(L"EnableDirBrowsing",_variant_t(true));
pVrAds->Put(L"Path",_variant_t(pathname));
pVrAds->Put(L"AppRoot",_variant_t(pathname));
pVrAds->SetInfo();
pVrAds->Release();
pAds->Release();
pCont->Release();
}
hr=pSrvOp->Start();
hr=pSrvOp->Release();
}
}
}
return true;
}
Top
2 楼masterz(www.fruitfruit.com)回复于 2003-02-24 16:16:53 得分 0 在Visual C++中使用ADSI,需要#include activeds.h,连接activeds.lib和adsiid.lib库文件,并要使用Unicode字符串。
这个例子是使用VC在默认Web站点建立的虚拟目录。
*/
///////////////////////////////////////////////
// CreateVirtualDirection参数说明
// lpszVirtualDirName需要建立的虚拟目录的目录名字
// lpszDiskPath 需要建立虚拟目录的本地磁盘目录
//////////////////////////////////////////////
BOOL CreateVirtualDirection(LPCTSTR lpszVirtualDirName,LPCTSTR lpszDiskPath)
{
IADsContainer* iContainer;
IADs* iAds;
if(ADsGetObject(L"IIS://localhost/w3svc",IID_IADsContainer,(void**)&iContainer)==S_OK)
{
//等到默认站点
iContainer->GetObject(_bstr_t("IIsWebServer"), _bstr_t("1"),(IDispatch**)&iAds);
if(iAds->QueryInterface(IID_IADsContainer,(void**)&iContainer)==S_OK)
{
//得到默认站点的根目录
iContainer->GetObject(_bstr_t("IIsWebVirtualDir"),_bstr_t("Root"),(IDispatch**)&iAds);
//获得访问虚拟目录
if(iAds->QueryInterface(IID_IADsContainer,(void**)&iContainer)==S_OK)
{
//先删除了虚拟目录
iContainer->Delete(_bstr_t("IIsWebVirtualDir"), _bstr_t(lpszVirtualDirName));
//建立虚拟目录
if(iContainer->Create(_bstr_t("IIsWebVirtualDir"), _bstr_t(lpszVirtualDirName),(IDispatch**)&iAds)==S_OK)
{
//设置虚拟目录的属性
iAds->Put(_bstr_t("AccessRead"),_variant_t("True"));//注意跟VB中的设置属性比较
iAds->Put(_bstr_t("AccessWrite"),_variant_t("True"));
iAds->Put(_bstr_t("Path"),_variant_t(lpszDiskPath));
iAds->SetInfo();
iAds->Release();
iAds->Release();
iContainer->Release();
iContainer->Release();
return TRUE;
}
else
{
iAds->Release();
iAds->Release();
iContainer->Release();
iContainer->Release();
return FALSE;
}
}
else
{
iAds->Release();
iContainer->Release();
}
}
else
{
iAds->Release();
}
iContainer->Release();
}
return FALSE;
}
//使用VC需要初始化COM环境,别忘了APP类的InitInstance中AfxOleInit()函数的调用哦。
method
The IADs::PutEx method modifies the values of an attribute in the ADSI attribute cache. For example, for properties that allow multiple values, you can append additional values to an existing set of values, modify the values in the set, remove specified values from the set, or delete values from the set.
Syntax
HRESULT PutEx( [in] LONG lnControlCode, [in] BSTR bstrName, [in] VARIANT vProp );
Parameters
-
lnControlCode [in]
-
Control code that indicates the mode of modification: Append, Replace, Remove, and Delete. For more information and a list of values, see ADS_PROPERTY_OPERATION_ENUM.
bstrName [in]
-
Contains a BSTR that specifies the property name.
vProp [in]
-
Contains a VARIANT array that contains the new value or values of the property. A single-valued property is represented as an array with a single element. If InControlCode is set to ADS_PROPERTY_CLEAR, the value of the property specified by vProp is irrelevant.
Return value
This method supports standard return values, as well as the following.
For more information, see ADSI Error Codes.
Return code | Description |
---|---|
| The method succeeded. |
| The supplied dwControlCode parameter is invalid. |
| The ADSI data type cannot be converted to or from the native data type of the underlying directory. |
Remarks
PutEx is usually used to set values on multi-value attributes. Unlike the IADs::Put method, with PutEx, you are not required to get the attribute values before you modify them. However, because PutEx only makes changes to attributes values contained in the ADSI property cache, you must use IADs::SetInfo after each PutEx call in order to commit changes to the directory.
PutEx enables you to append values to an existing set of values in a multi-value attribute using ADS_PROPERTY_APPEND. When you update, append, or delete values to a multi-value attribute, you must use an array.
Active Directory does not accept duplicate values on a multi-valued attribute. If you call PutEx to append a duplicate value to a multi-valued attribute of an Active Directory object, the PutEx call succeeds, but the duplicate value is ignored.
Similarly, if you use PutEx to delete one or more values from a multi-valued property of an Active Directory object, the operation succeeds, that is, it will not produce an error, even if any or all of the specified values are not set on the property.
Note The NDS, WinNT, and NWCOMPAT providers ignore the value passed by the InControlCode argument and perform the equivalent of an ADS_PROPERTY_UPDATE request when using PutEx.
Examples
The following code example shows how to use the IADs.PutEx method.
Dim x As IADs On Error GoTo Cleanup Set x = GetObject("LDAP://CN=JeffSmith,CN=Users,DC=Fabrikam,DC=com") '---------------------------------------------------------- ' Assume the otherHomePhone has the values ' 425-707-9790, 425-707-9791 '---------------------------------------------------------- ' Adding a value x.PutEx ADS_PROPERTY_APPEND, "otherhomePhone", Array("425-707-9792") x.SetInfo ' Now the values are 425-707-9790,425-707-9791,425-707-9792. deleting two values x.PutEx ADS_PROPERTY_DELETE, "otherHomePhone", Array("425-707-9790", "425-707-9791") x.SetInfo ' Now the values are 425-707-9792. ' Changing the remaining value x.PutEx ADS_PROPERTY_UPDATE, "otherHomePhone", Array("425-707-9793", "425-707-9794") x.SetInfo ' Now the values are 425-707-9793,425-707-9794. ' Deleting the value x.PutEx ADS_PROPERTY_CLEAR, "otherHomePhone", vbNullString x.SetInfo ' Now the property has no value. Cleanup: If(Err.Number<>0) Then MsgBox("An error has occurred. " & Err.Number) End If Set x = Nothing
The following code example shows how to use the IADs::PutEx method.
HRESULT hr; IADs *pADs=NULL; LPWSTR pszADsPath = L"LDAP://CN=JeffSmith,CN=Users,DC=Fabrikam,DC=com"; CoInitialize(NULL); hr = ADsGetObject(pszADsPath, IID_IADs, (void**) &pADs); if(SUCCEEDED(hr)) { VARIANT var; VariantInit(&var); LPWSTR pszPhones[] = { L"425-707-9790", L"425-707-9791" }; DWORD dwNumber = sizeof(pszPhones)/sizeof(LPWSTR); hr = ADsBuildVarArrayStr(pszPhones, dwNumber, &var); hr = pADs->Put(CComBSTR("otherHomePhone"), var); VariantClear(&var); hr = pADs->SetInfo(); // The phone list is now 425-707-9790, 425-707-9791. // Append another number to the list. LPWSTR pszAddPhones[]={L"425-707-9792"}; hr = ADsBuildVarArrayStr(pszAddPhones, 1, &var); hr = pADs->PutEx(ADS_PROPERTY_APPEND, CComBSTR("otherHomePhone"), var); hr = pADs->SetInfo(); // The list becomes // 425-707-9790, 425-707-9791, 425-707-9792. VariantClear(&var); hr = ADsBuildVarArrayStr(pszPhones, dwNumber, &var); hr = pADs->PutEx(ADS_PROPERTY_DELETE, CComBSTR("otherHomePhone"), var); hr = pADs->SetInfo(); // The list becomes 425-707-9792. pszPhones[0] = L"425-707-9793"; pszPhones[1] = L"425-707-9794"; hr = ADsBuildVarArrayStr(pszPhones, dwNumber, &var); hr = pADs->PutEx(ADS_PROPERTY_UPDATE, CComBSTR("otherHomePhone"), var); hr = pADs->SetInfo(); // The list becomes 425-707-9793, 425-707-9794. VariantClear(&var); V_VT(&var)=VT_NULL; hr = pADs->PutEx(ADS_PROPERTY_CLEAR, CComBSTR("otherHomePhone"), var); hr = pADs->SetInfo(); // The list is empty. VariantClear(&var); pADs->Release(); } hr = CoUninitialize();
Requirements
Minimum supported client | Windows 2000 Professional [desktop apps only] |
---|---|
Minimum supported server | Windows 2000 Server [desktop apps only] |
Header |
|
DLL |
|
IID | IID_IADs is defined as FD8256D0-FD15-11CE-ABC4-02608C9E7553 |
See also
Send comments about this topic to Microsoft
Build date: 10/26/2012