#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#pragma comment(lib,'comsupp.lib')
#pragma comment(lib,'ActiveDS.lib')
#pragma comment(lib,'adsiid.lib')
#pragma comment(lib, 'WS2_32.lib')
int CPage1::CheckW3SvcExist(BOOL bStart)
{
int iErrCode;
SC_HANDLE hHandle = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
SC_HANDLE hService;
if (hHandle ==NULL ) //创建句柄失败
{
iErrCode = ERRORCODE_CHECKSERVICEERROR; //检查服务失败
SetDlgItemText(IDC_SERVER_STATE,'检查IIS服务失败!');
return iErrCode;
}
ENUM_SERVICE_STATUS service;
DWORD dwBytesNeeded = 0;
DWORD dwServicesReturned = 0;
DWORD dwResumedHandle = 0;
DWORD dwServiceType = SERVICE_WIN32; //只读取WIN32的服务 | SERVICE_DRIVER;
char* chServiceName;
char* chFind;
BOOL bResult;
bool bHasService=false;
chFind='W3SVC';
chFind=_strupr(_strdup(chFind)); //_strdup The _strdup function calls malloc to allocate storage space for a copy of strSource and then copies strSource to the allocated space. 所以要释放
// 查找服务列表
bResult = ::EnumServicesStatus(hHandle,
dwServiceType,
SERVICE_STATE_ALL,
&service,
sizeof(ENUM_SERVICE_STATUS),
&dwBytesNeeded,
&dwServicesReturned,
&dwResumedHandle );
if (!bResult)
{
// 获取应需要的内存空间
if (GetLastError() == ERROR_MORE_DATA)
{
DWORD dwBytes = sizeof(ENUM_SERVICE_STATUS) + dwBytesNeeded;
ENUM_SERVICE_STATUS* pServices = NULL;
pServices = new ENUM_SERVICE_STATUS [dwBytes];
// 获取服务列表
EnumServicesStatus(hHandle,
dwServiceType,
SERVICE_STATE_ALL,
pServices,
dwBytes,
&dwBytesNeeded,
&dwServicesReturned,
&dwResumedHandle );
// 提取每个列表
for (unsigned iIndex = 0; iIndex < dwServicesReturned; iIndex++)
{
//chServiceName=(pServices+iIndex)->lpDisplayName;
//AfxMessageBox((pServices+iIndex)->lpDisplayName);
chServiceName=_strupr(_strdup((pServices+iIndex)->lpServiceName));
if(strcmp(chFind,chServiceName)==0)
{
switch((pServices+iIndex)->ServiceStatus.dwCurrentState)
{
case SERVICE_STOPPED:
SetDlgItemText(IDC_SERVER_STATE,'IIS已经停止');
GetDlgItem(IDC_SERVER_START)->EnableWindow(true);
GetDlgItem(IDC_SERVER_STOP)->EnableWindow(false);
if(bStart)
{
hService= OpenService(hHandle,chServiceName,SERVICE_ALL_ACCESS);
if(hService==0)
{
iErrCode=ERRORCODE_CANNTSTARTSERVICE;
SetDlgItemText(IDC_SERVER_STATE,'无法启动IIS!');
return iErrCode;
}
else
{
bResult=StartService(hService,0,NULL);
if(!bResult)
{
SetDlgItemText(IDC_SERVER_STATE,'无法启动IIS!');
iErrCode=ERRORCODE_CANNTSTARTSERVICE;
return iErrCode;
}
else
{
SetDlgItemText(IDC_SERVER_STATE,'已经成功启动了IIS');
iErrCode=ERRORCODE_STARTED;
GetDlgItem(IDC_SERVER_START)->EnableWindow(false);
GetDlgItem(IDC_SERVER_STOP)->EnableWindow(true);
return iErrCode;
}
CloseServiceHandle(hService);
}
}
break;
case SERVICE_RUNNING:
GetDlgItem(IDC_SERVER_START)->EnableWindow(false);
GetDlgItem(IDC_SERVER_STOP)->EnableWindow(true);
iErrCode=ERRORCODE_RUNNING;
SetDlgItemText(IDC_SERVER_STATE,'IIS正在运行中...');
break;
}
bHasService=true;
free(chServiceName);
break; //退出循环
}
free(chServiceName);
}
delete [] pServices;
pServices = NULL;
}
CloseServiceHandle(hHandle);
}
else
{
SetDlgItemText(IDC_SERVER_STATE,'检查IIS服务失败!');
iErrCode=ERRORCODE_CHECKSERVICEERROR;
}
free(chFind);
if(!bHasService)
{
iErrCode=ERRORCODE_NOSERVICE;
SetDlgItemText(IDC_SERVER_STATE,'你的计算机还没有安装IIS!');
}
return iErrCode;
}
BOOL CPage1::CreateVirtualDirectry(LPCTSTR lpszVirtualDirName, LPCTSTR lpszDiskPath)
{
UpdateData();
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(this->m_bRead?true:false));
iAds->Put(_bstr_t('AccessWrite'),_variant_t(this->m_bWrite?true:false));
iAds->Put(_bstr_t('AccessScript'),_variant_t(this->m_bScript?true:false));
iAds->Put(_bstr_t('AccessExecute'),_variant_t(this->m_bExecute?true:false));
iAds->Put(_bstr_t('EnableDirBrowsing'),_variant_t(this->m_bBrowse?true:false));
iAds->Put(_bstr_t('EnableDefaultDoc'),_variant_t(this->m_bExecute?true:false));
iAds->Put(_bstr_t('Path'),_variant_t(lpszDiskPath));
iAds->SetInfo();
DISPID dispid;
LPWSTR szDispName = L'AppCreate2';
HRESULT hresult = iAds->GetIDsOfNames(IID_NULL,&szDispName,1,
LOCALE_SYSTEM_DEFAULT,&dispid);
if(hresult == S_OK)
{
DISPPARAMS par;
VARIANTARG vName;
VariantInit(&vName);
vName.vt = VT_BOOL;
vName.boolVal = false;
par.cArgs = 1;
par.rgvarg = &vName;
par.cNamedArgs = 0;
par.rgdispidNamedArgs = NULL;
hresult = iAds->Invoke(dispid,IID_NULL,LOCALE_SYSTEM_DEFAULT,
DISPATCH_METHOD,&par,NULL, NULL,NULL);
SetDlgItemText(IDC_STATE,'创建虚拟目录成功!');
}
iAds->Release();
iContainer->Release();
return true;
}
else
{
SetDlgItemText(IDC_STATE,'创建虚拟目录失败!');
iAds->Release();
iContainer->Release();
return false;
}
}
else
{
SetDlgItemText(IDC_STATE,'创建虚拟目录失败!');
iAds->Release();
iContainer->Release();
}
}
else
{
SetDlgItemText(IDC_STATE,'创建虚拟目录失败!');
iAds->Release();
}
iContainer->Release();
}
return false;
}
BOOL CPage1::DeleteVirtualDirectry(LPCTSTR lpszVirtualDirName)
{
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)
{
if(iContainer->Delete(_bstr_t('IIsWebVirtualDir'), _bstr_t(lpszVirtualDirName))==S_OK)
SetDlgItemText(IDC_STATE,'删除虚拟目录成功!');
else
SetDlgItemText(IDC_STATE,'删除虚拟目录失败!');
}
else
{
iAds->Release();
iContainer->Release();
SetDlgItemText(IDC_STATE,'删除虚拟目录失败!');
}
}
else
{
SetDlgItemText(IDC_STATE,'删除虚拟目录失败!');
iAds->Release();
}
iContainer->Release();
}
return true;
}