// MTSAdmin.cpp : command line tool for stopping MTS server packages // (c) 1999 Richard Grimes // From Professional Visual C++ MTS Programming (www.wrox.com) #include "stdafx.h" #include #import "C:\WINNT\System32\Mts\mtxcatex.dll" no_namespace, raw_interfaces_only, no_implementation #import "C:\WINNT\System32\Mts\mtxgrp.dll" no_namespace, raw_interfaces_only, no_implementation #import "C:\WINNT\System32\mtsevents.dll" no_namespace, raw_interfaces_only, no_implementation void Usage() { printf("MTSAdmin -refresh|-kill [guid]\n"); printf("where \t-refresh will refresh all component registration from the catalog\n"); printf("\t-kill will shutdown all or a single package based on the package id\n"); } int main(int argc, char* argv[]) { if (argc < 2) { Usage(); return 0; } LPSTR strGuid = NULL; bool bRefresh = false; bool bKill = false; int x = 0; while (++x < argc) { if (strcmpi(argv[x], "-refresh") == 0) { bRefresh = true; break; } if (strcmpi(argv[x], "-kill") == 0) { bKill = true; continue; } strGuid = argv[x]; } if (!bRefresh && !bKill) { Usage(); return 0; } HRESULT hr; hr = CoInitialize(NULL); if (SUCCEEDED(hr)) { CComPtr pSess; pSess.CoCreateInstance(__uuidof(MTSCatalog)); float fVer; pSess->InitializeSession(0, 3, 0, &fVer); if (bRefresh) { CComQIPtr pUp = pSess; hr = pUp->RefreshMtxComponents(); if (FAILED(hr)) printf("Failed to refresh components %08x\n", hr); else printf("Refreshed components\n"); return 0; } if (strGuid == NULL) { // don't wait for all processes to end CComQIPtr pCtrl = pSess; hr = pCtrl->ShutdownProcesses(); if (FAILED(hr)) printf("Could not shutdown packages %08x\n", hr); else printf("All packages have been killed\n"); return 0; } CComPtr pMtsGrp; pMtsGrp.CoCreateInstance(__uuidof(MtsGrp)); long lPackages; pMtsGrp->get_Count(&lPackages); USES_CONVERSION; CComBSTR bstrPackageGuid = A2W(strGuid); CComBSTR bstrName; DWORD dwProcessID = 0; for (int i=0; i pUnk; pMtsGrp->Item(i, &pUnk); CComQIPtr pEvents = pUnk; CComBSTR bstrRunningGUID; pEvents->get_PackageGuid(&bstrRunningGUID.m_str); if (wcscmp(bstrRunningGUID.m_str, bstrPackageGuid.m_str) != 0) continue; pEvents->get_PackageName(&bstrName.m_str); pEvents->GetProcessID((LONG*)&dwProcessID); } HANDLE hProcess = NULL; if (dwProcessID != 0) { hProcess = OpenProcess(SYNCHRONIZE, FALSE, dwProcessID); } if (hProcess == NULL) { printf("The package is not running!\n"); return 0; } CComQIPtr pCtrl = pSess; hr = pCtrl->ShutdownPackage(bstrPackageGuid); if (FAILED(hr)) printf("Error closing down package %08x\n", hr); else { printf("Waiting for the package %S to end\n", bstrName.m_str); WaitForSingleObject(hProcess, INFINITE); } CloseHandle(hProcess); } CoUninitialize(); return 0; }