Using VC to manipulate IIS

Source: Internet
Author: User
Tags metabase

Ciisadminbase: ciisadminbase ()
{

Hresult hres = 0;
: Coinitialize (null );
Hres = cocreateinstance (clsid_msadminbase, null, clsctx_all, iid_imsadminbase, & m_pimetabase );
 
If (failed (hres ))
{
Writelogfile ("can't create instance-ciisadminbase create ");
M_pimetabase = NULL;
}
}

Ciisadminbase ::~ Ciisadminbase ()
{
// Destroy the object
If (m_pimetabase)
{
(Iunknown *) m_pimetabase)-> release ();
M_pimetabase = NULL;
}
: Couninitialize ();
}

//////////////////////////////////////// //////////////////////////////
// Www site operations

Bool ciisadminbase: startwebsite ()
{
Bool Bret = setserverstatus ();
If (! Bret)
Writelogfile ("can't start web site ");
Return Bret;
}

Bool ciisadminbase: stopwebsite ()
{
Bool Bret = setserverstatus (false );
If (! Bret)
Writelogfile ("can't stop web site ");
Return Bret;
}

Bool ciisadminbase: addwebsite (cstring strsitename, cstring strsitepath, DWORD dwaccessflag)
{
Bool Bret = createsite (strsitename, "/lm/w3svc/1/root", strsitepath, dwaccessflag );
If (! Bret)
Writelogfile ("can't create Web site:" + strsitename );
Return Bret;
}

Bool ciisadminbase: killwebsite (const cstring & strsitename)
{
If (! M_pimetabase) return false;
 
Hresult hres = 0;
Metadata_handle hroot;
Ciisptr ptriis (imsadminbase *) m_pimetabase );

// Prepare bstrs
Ccombstr bstrsitename = "/1/root/" + strsitename;
Ccombstr bstrsiteroot = "/lm/W3SVC ";

// Open metabase key
Hres = ptriis-> openkey (metadata_master_root_handle, bstrsiteroot,
Metadata_permission_write, 20, & hroot );
If (failed (hres) return false;
 
// Delete a key
Hres = ptriis-> deletekey (hroot, bstrsitename );
If (failed (hres) & hresult_code (hres )! = Error_path_not_found) return false;
 
Ptriis-> closekey (hroot );
Return true;
}

Bool ciisadminbase: createsite (cstring strsitename, cstring strsitevdir,
Cstring strsitedir, DWORD dwaccessflag)
{
If (! M_pimetabase) return false;
 
Hresult hres = 0;
Metadata_handle hroot;
Metadata_record metarecord;
Ciisptr ptriis (imsadminbase *) m_pimetabase );

// prepare bstrs
ccombstr bstrsitename = strsitename;
ccombstr bstrsitevdir = strsitevdir;
ccombstr bstrsitevname = "" + strsitename;
ccombstr bstrsitedir = strsitedir;
// open metabase key
hres = ptriis-> openkey (delimiter, bstrsitevdir,
delimiter | metadata_permission_write, 20, & hroot);
If (failed (hres)
{< br> writelogfile ("Can't open metabase key:" + strsitevdir);
return false;
}< br>
// Add a key
hres = ptriis-> addkey (hroot, bstrsitevname);
If (failed (hres) & hresult_code (hres )! = Error_already_exists)
{< br> writelogfile ("can't add key to metabase:/" + strsitename);
return false;
}

///////////////////////////////////// /// //
// set site properties
// key type
ccombstr bstrkeytype ("iiswebvirtualdir ");
zeromemory (& metarecord, sizeof (metarecord);
metarecord. dwmddatatype = string_metadata;
metarecord. dwmdidentifier = md_key_type;
metarecord. dwmdusertype = iis_md_ut_server;
metarecord. pbmddata = (unsigned char *) (unsigned short *) bstrkeytype;
metarecord. dwmddatalen = bstrkeytype. length () * 2 + 3;
hres = ptriis-> setdata (hroot, bstrsitevname, & metarecord);
If (failed (hres ))
{< br> writelogfile ("can't set data for key type: iiswebvirtualdir");
return false;
}

// friendly name
zeromemory (& metarecord, sizeof (metarecord);
metarecord. dwmddatatype = string_metadata;
metarecord. dwmdidentifier = md_app_friendly_name;
metarecord. dwmdusertype = iis_md_ut_wam;
metarecord. pbmddata = (unsigned char *) (unsigned short *) bstrsitename;
metarecord. dwmddatalen = bstrsitename. length () * 2 + 3;
hres = ptriis-> setdata (hroot, bstrsitevname, & metarecord);
If (failed (hres ))
{< br> writelogfile ("can't set data for friendly name");
return false;
}

// app root path
ccombstr bstrsiteroot = strsitevdir + "/" + strsitename;
zeromemory (& metarecord, sizeof (metarecord ));
metarecord. dwmdattributes = metadata_inherit;
metarecord. dwmddatatype = string_metadata;
metarecord. dwmdidentifier = md_app_root;
metarecord. dwmdusertype = iis_md_ut_file;
metarecord. pbmddata = (unsigned char *) (unsigned short *) bstrsiteroot;
metarecord. dwmddatalen = bstrsiteroot. length () * 2 + 3;
hres = ptriis-> setdata (hroot, bstrsitevname, & metarecord);
If (failed (hres ))
{< br> writelogfile ("can't set data for app root path");
return false;
}

// path
zeromemory (& metarecord, sizeof (metarecord);
metarecord. dwmdattributes = metadata_inherit;
metarecord. dwmddatatype = string_metadata;
metarecord. dwmdidentifier = md_vr_path;
metarecord. dwmdusertype = iis_md_ut_file;
metarecord. pbmddata = (unsigned char *) (unsigned short *) bstrsitedir;
metarecord. dwmddatalen = bstrsitedir. length () * 2 + 3;
hres = ptriis-> setdata (hroot, bstrsitevname, & metarecord);
If (failed (hres ))
{< br> writelogfile ("can't set data for path");
return false;
}

// access flag
DWORD dwflag = dwaccessflag; // md_access_read | md_access_execute | md_access_script;
zeromemory (& metarecord, sizeof (metarecord ));
metarecord. dwmdattributes = metadata_inherit;
metarecord. dwmddatatype = dword_metadata;
metarecord. dwmdidentifier = md_access_perm;
metarecord. dwmdusertype = iis_md_ut_file;
metarecord. pbmddata = (unsigned char *) & dwflag;
metarecord. dwmddatalen = sizeof (DWORD);
hres = ptriis-> setdata (hroot, bstrsitevname, & metarecord);
If (failed (hres ))
{< br> writelogfile ("can't set data for access flag");
return false;
}

// dir browse flag
dwflag = 1073741886;
zeromemory (& metarecord, sizeof (metarecord);
metarecord. dwmdattributes = metadata_inherit;
metarecord. dwmddatatype = dword_metadata;
metarecord. dwmdidentifier = md_directory_browsing;
metarecord. dwmdusertype = iis_md_ut_file;
metarecord. pbmddata = (unsigned char *) & dwflag;
metarecord. dwmddatalen = sizeof (DWORD);
hres = ptriis-> setdata (hroot, bstrsitevname, & metarecord);
If (failed (hres ))
{< br> writelogfile ("can't set data for Dir browse flag");
return false;
}

// App isolate
Dwflag = 2;
Zeromemory (& metarecord, sizeof (metarecord ));
Metarecord. dwmdattributes = metadata_inherit;
Metarecord. dwmddatatype = dword_metadata;
Metarecord. dwmdidentifier = md_app_isolated;
Metarecord. dwmdusertype = iis_md_ut_wam;
Metarecord. pbmddata = (unsigned char *) & dwflag;
Metarecord. dwmddatalen = sizeof (DWORD );
Hres = ptriis-> setdata (hroot, bstrsitevname, & metarecord );
If (failed (hres ))
{
Writelogfile ("can't set data for app isolate ");
Return false;
}

// Site properties complete, close the metabase key
Ptriis-> closekey (hroot );

Return true;
}

Bool ciisadminbase: setserverstatus (bool bstart, bool bw3site)
{
If (! M_pimetabase) return false;
 
Hresult hres = 0;
Metadata_handle hrootw3;
Ciisptr ptriis (imsadminbase *) m_pimetabase );

// Open metabase key
Ccombstr bstrsitename;
If (bw3site) bstrsitename = "/lm/W3SVC ";
Else bstrsitename = "/lm/msftpsvc /";

Hres = ptriis-> openkey (metadata_master_root_handle, bstrsitename,
Metadata_permission_read | metadata_permission_write, 20, & hrootw3 );
If (failed (hres) return false;

// Get metabase key data
Metadata_record metarecord;
DWORD dwbuffer;
 
If (bstart) dwbuffer = md_server_command_start;
Else dwbuffer = md_server_command_stop;

Zeromemory (& metarecord, sizeof (metarecord ));
Metarecord. dwmdidentifier = md_server_command;
Metarecord. dwmdattributes = metadata_inherit;
Metarecord. dwmdusertype = iis_md_ut_server;
Metarecord. dwmddatatype = dword_metadata;
Metarecord. dwmddatalen = sizeof (DWORD );
Metarecord. pbmddata = (unsigned char *) & dwbuffer;

Hres = ptriis-> setdata (hrootw3, ccombstr ("/1"), & metarecord );

If (failed (hres) return false;

Ptriis-> closekey (hrootw3 );
 
Return true;
}

bool ciisadminbase: createsite (cstring strsitename, cstring strsitevdir,
cstring strsitedir, DWORD dwaccessflag)
{< br> If (! M_pimetabase) return false;
hresult hres = 0;
metadata_handle hroot;
metadata_record metarecord;
ciisptr ptriis (imsadminbase *) m_pimetabase);

// prepare bstrs
ccombstr bstrsitename = strsitename;
ccombstr bstrsitevdir = strsitevdir;
ccombstr bstrsitevname = "" + strsitename;
ccombstr bstrsitedir = strsitedir;
// open metabase key
hres = ptriis-> openkey (delimiter, bstrsitevdir,
delimiter | metadata_permission_write, 20, & hroot);
If (failed (hres)
{< br> writelogfile ("Can't open metabase key:" + strsitevdir);
return false;
}< br>
// Add a key
hres = ptriis-> addkey (hroot, bstrsitevname);
If (failed (hres) & hresult_code (hres )! = Error_already_exists)
{< br> writelogfile ("can't add key to metabase:/" + strsitename);
return false;
}

///////////////////////////////////// /// //
// set site properties
// key type
ccombstr bstrkeytype ("iiswebvirtualdir ");
zeromemory (& metarecord, sizeof (metarecord);
metarecord. dwmddatatype = string_metadata;
metarecord. dwmdidentifier = md_key_type;
metarecord. dwmdusertype = iis_md_ut_server;
metarecord. pbmddata = (unsigned char *) (unsigned short *) bstrkeytype;
metarecord. dwmddatalen = bstrkeytype. length () * 2 + 3;
hres = ptriis-> setdata (hroot, bstrsitevname, & metarecord);
If (failed (hres ))
{< br> writelogfile ("can't set data for key type: iiswebvirtualdir");
return false;
}

// friendly name
zeromemory (& metarecord, sizeof (metarecord);
metarecord. dwmddatatype = string_metadata;
metarecord. dwmdidentifier = md_app_friendly_name;
metarecord. dwmdusertype = iis_md_ut_wam;
metarecord. pbmddata = (unsigned char *) (unsigned short *) bstrsitename;
metarecord. dwmddatalen = bstrsitename. length () * 2 + 3;
hres = ptriis-> setdata (hroot, bstrsitevname, & metarecord);
If (failed (hres ))
{< br> writelogfile ("can't set data for friendly name");
return false;
}

// app root path
ccombstr bstrsiteroot = strsitevdir + "/" + strsitename;
zeromemory (& metarecord, sizeof (metarecord ));
metarecord. dwmdattributes = metadata_inherit;
metarecord. dwmddatatype = string_metadata;
metarecord. dwmdidentifier = md_app_root;
metarecord. dwmdusertype = iis_md_ut_file;
metarecord. pbmddata = (unsigned char *) (unsigned short *) bstrsiteroot;
metarecord. dwmddatalen = bstrsiteroot. length () * 2 + 3;
hres = ptriis-> setdata (hroot, bstrsitevname, & metarecord);
If (failed (hres ))
{< br> writelogfile ("can't set data for app root path");
return false;
}

// path
zeromemory (& metarecord, sizeof (metarecord);
metarecord. dwmdattributes = metadata_inherit;
metarecord. dwmddatatype = string_metadata;
metarecord. dwmdidentifier = md_vr_path;
metarecord. dwmdusertype = iis_md_ut_file;
metarecord. pbmddata = (unsigned char *) (unsigned short *) bstrsitedir;
metarecord. dwmddatalen = bstrsitedir. length () * 2 + 3;
hres = ptriis-> setdata (hroot, bstrsitevname, & metarecord);
If (failed (hres ))
{< br> writelogfile ("can't set data for path");
return false;
}

// access flag
DWORD dwflag = dwaccessflag; // md_access_read | md_access_execute | md_access_script;
zeromemory (& metarecord, sizeof (metarecord ));
metarecord. dwmdattributes = metadata_inherit;
metarecord. dwmddatatype = dword_metadata;
metarecord. dwmdidentifier = md_access_perm;
metarecord. dwmdusertype = iis_md_ut_file;
metarecord. pbmddata = (unsigned char *) & dwflag;
metarecord. dwmddatalen = sizeof (DWORD);
hres = ptriis-> setdata (hroot, bstrsitevname, & metarecord);
If (failed (hres ))
{< br> writelogfile ("can't set data for access flag");
return false;
}

// dir browse flag
dwflag = 1073741886;
zeromemory (& metarecord, sizeof (metarecord);
metarecord. dwmdattributes = metadata_inherit;
metarecord. dwmddatatype = dword_metadata;
metarecord. dwmdidentifier = md_directory_browsing;
metarecord. dwmdusertype = iis_md_ut_file;
metarecord. pbmddata = (unsigned char *) & dwflag;
metarecord. dwmddatalen = sizeof (DWORD);
hres = ptriis-> setdata (hroot, bstrsitevname, & metarecord);
If (failed (hres ))
{< br> writelogfile ("can't set data for Dir browse flag");
return false;
}

// App isolate
Dwflag = 2;
Zeromemory (& metarecord, sizeof (metarecord ));
Metarecord. dwmdattributes = metadata_inherit;
Metarecord. dwmddatatype = dword_metadata;
Metarecord. dwmdidentifier = md_app_isolated;
Metarecord. dwmdusertype = iis_md_ut_wam;
Metarecord. pbmddata = (unsigned char *) & dwflag;
Metarecord. dwmddatalen = sizeof (DWORD );
Hres = ptriis-> setdata (hroot, bstrsitevname, & metarecord );
If (failed (hres ))
{
Writelogfile ("can't set data for app isolate ");
Return false;
}

// Site properties complete, close the metabase key
Ptriis-> closekey (hroot );

Return true;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.