void GetScriptMaps()
{
::CoInitialize(NULL);
IADsContainer* iContainer;
IADs* iAds;
int i=0;
VARIANT sPort;
SAFEARRAY* psa;
CString tcx;
if(ADsGetObject(L"IIS://localhost/w3svc",IID_IADsContainer,(void**)&iContainer)==S_OK)
{
iContainer->GetObject(_bstr_t("IIsWebServer"),_bstr_t("1"),(IDispatch**)&iAds);
if(iAds==NULL) return;
VariantInit(&sPort);
HRESULT hr=iAds->Get(_bstr_t("ScriptMaps"),&sPort);
if(SUCCEEDED(hr))
{
if(sPort.vt ==VT_ARRAY | VT_VARIANT)
{
VARIANT* varArray=NULL;
SafeArrayAccessData(sPort.parray,(VOID**)&varArray);
UINT uDim = SafeArrayGetDim(sPort.parray);
if(1==uDim)
{
long lLbound,lRbound;
SafeArrayGetLBound(sPort.parray,1,&lLbound);
SafeArrayGetUBound(sPort.parray,1,&lRbound);
long size = lRbound-lLbound+2;
VARIANT* tArray=NULL;
SAFEARRAYBOUND aDim[1];
aDim[0].lLbound= 0;
aDim[0].cElements= size;
psa= SafeArrayCreate(VT_VARIANT,1,aDim); long Lbound,Rbound,index;
SafeArrayGetLBound(psa,1,&Lbound);
SafeArrayGetUBound(psa,1,&Rbound);
SafeArrayAccessData(psa,(VOID**)&tArray);
long sizeo = Rbound-Lbound+1; index=Lbound;
for(long i=lLbound;i<=lRbound;i++)
{
if(index<sizeo)
tArray[index].bstrVal = varArray[i].bstrVal;
index++;
}
tArray[sizeo-1].bstrVal=SysAllocString "*,D:\\myprogram\\DataIN\\DLLRelll\\DataIN.dll,4,GET,POST,HEAD,TRACE"); SafeArrayUnaccessData(psa);
SafeArrayUnaccessData(sPort.parray);
}
}
}
sPort.parray = psa;
iAds->Put(_bstr_t("ScriptMaps"),sPort);
iAds->SetInfo();
iAds->Release();
iContainer->Release();
}
::CoUninitialize();
}
大家帮忙看看这段代码有什么问题.
iAds->Put(_bstr_t("ScriptMaps"),sPort);没有起作用.为什么改不了ScriptMaps的属性呢?以及有什么解决方法?
{
::CoInitialize(NULL);
IADsContainer* iContainer;
IADs* iAds;
int i=0;
VARIANT sPort;
SAFEARRAY* psa;
CString tcx;
if(ADsGetObject(L"IIS://localhost/w3svc",IID_IADsContainer,(void**)&iContainer)==S_OK)
{
iContainer->GetObject(_bstr_t("IIsWebServer"),_bstr_t("1"),(IDispatch**)&iAds);
if(iAds==NULL) return;
VariantInit(&sPort);
HRESULT hr=iAds->Get(_bstr_t("ScriptMaps"),&sPort);
if(SUCCEEDED(hr))
{
if(sPort.vt ==VT_ARRAY | VT_VARIANT)
{
VARIANT* varArray=NULL;
SafeArrayAccessData(sPort.parray,(VOID**)&varArray);
UINT uDim = SafeArrayGetDim(sPort.parray);
if(1==uDim)
{
long lLbound,lRbound;
SafeArrayGetLBound(sPort.parray,1,&lLbound);
SafeArrayGetUBound(sPort.parray,1,&lRbound);
long size = lRbound-lLbound+2;
VARIANT* tArray=NULL;
SAFEARRAYBOUND aDim[1];
aDim[0].lLbound= 0;
aDim[0].cElements= size;
psa= SafeArrayCreate(VT_VARIANT,1,aDim); long Lbound,Rbound,index;
SafeArrayGetLBound(psa,1,&Lbound);
SafeArrayGetUBound(psa,1,&Rbound);
SafeArrayAccessData(psa,(VOID**)&tArray);
long sizeo = Rbound-Lbound+1; index=Lbound;
for(long i=lLbound;i<=lRbound;i++)
{
if(index<sizeo)
tArray[index].bstrVal = varArray[i].bstrVal;
index++;
}
tArray[sizeo-1].bstrVal=SysAllocString "*,D:\\myprogram\\DataIN\\DLLRelll\\DataIN.dll,4,GET,POST,HEAD,TRACE"); SafeArrayUnaccessData(psa);
SafeArrayUnaccessData(sPort.parray);
}
}
}
sPort.parray = psa;
iAds->Put(_bstr_t("ScriptMaps"),sPort);
iAds->SetInfo();
iAds->Release();
iContainer->Release();
}
::CoUninitialize();
}
大家帮忙看看这段代码有什么问题.
iAds->Put(_bstr_t("ScriptMaps"),sPort);没有起作用.为什么改不了ScriptMaps的属性呢?以及有什么解决方法?
IADs *pADs = NULL;
LPWSTR pszADsPath = L"LDAP://CN=JeffSmith,CN=Users,DC=Fabrikam,DC=com";
CoInitialize(NULL);
//////////////////////////////////
// Modifying attributes using IADs
//////////////////////////////////
hr = ADsGetObject(pszADsPath, IID_IADs, (void**) &pADs);
if(SUCCEEDED(hr))
{
VARIANT var;
VariantInit(&var);
// Set the first name.
V_BSTR(&var) = SysAllocString(L"Jeff");
V_VT(&var) = VT_BSTR;
hr = pADs->Put(CComBSTR("givenName"), var);
// Set the last name.
VariantClear(&var);
V_BSTR(&var) = SysAllocString(L"Smith");
V_VT(&var) = VT_BSTR;
hr = pADs->Put(CComBSTR("sn"), var);
VariantClear(&var); // Other Telephones.
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("otherTelephone"), var);
VariantClear(&var);
// Commit the change to the directory.
hr = pADs->SetInfo();
pADs->Release();
}CoUninitialize();
然后 iAds-> Put(_bstr_t("ScriptMaps"),sPort); 就可以输入进去
但输入进去后就把所有的都覆盖了,只有这一个了
你可以尝试 iAds-> Gut(_bstr_t("ScriptMaps"),sPort);
sPort再加上"*,D:\\myprogram\\DataIN\\DLLRelll\\DataIN.dll,4,GET,POST,HEAD,TRACE"
iAds-> Put(_bstr_t("ScriptMaps"),sPort);
我就是在后面加的.
现在,如果我直接把字符串合并了后再put的话,系统指认第一条.我还尝试了在字符串之间加入\n等特殊字符但是还是分割不了.系统就认第一条,在4后面的所有字符串都认为是动作了
CString s(sPort.bstrVal);
AfxMessageBox(s);
s为乱码
就是Put()参数就不支持VARIANT为VT_ARRAY参数的话
如果我要添加一条字符串,那该怎么办?
_bstr_t(_variant_t& var);
_variant_t(const _bstr_t& bstrSrc );
发表于:2007-11-30 18:00:57
The variant should be of type VT_ARRAY and point to a SAFEARRAY. 我就是这么做的.你可以看代码