⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 machineview.cpp

📁 vc++网络程序设计实例详解 人民邮电出版社1-2章源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:

    // Select the root item
    GetTreeCtrl().SelectItem(m_hMachineViewRoot);
    
    // Since the root gets selected, delete all in the remote admin view, as the 
    // root does not show any info
    //::GetRemoteAdminView()->GetListCtrl().DeleteAllItems();
	pRemoteAdminView->GetListCtrl().DeleteAllItems();
}


HTREEITEM CMachineView::ShowNewMachine(CString strTextToBeShown, CMachineInfo* pMachineInfo)
{
    TVINSERTSTRUCT tviHelper    = {0};
        
    // Show a new machine in the hierarchy
    tviHelper.hParent			  = m_hMachineViewRoot;
	tviHelper.hInsertAfter		  = TVI_LAST;
	tviHelper.item.mask			  = TVIF_TEXT |
                                    TVIF_IMAGE | 
                                    TVIF_SELECTEDIMAGE | 
                                    TVIF_PARAM;
    tviHelper.item.pszText		  = strTextToBeShown.GetBuffer(0);
	tviHelper.item.iImage		  = MACHINE_IMAGE;
	tviHelper.item.iSelectedImage = MACHINE_IMAGE;
	tviHelper.item.lParam		  = reinterpret_cast<LPARAM>(pMachineInfo);
	
    HTREEITEM hNewTreeItem = GetTreeCtrl().InsertItem(&tviHelper);

    // Expand the root to show the new machine
    GetTreeCtrl().Expand(m_hMachineViewRoot, TVE_EXPAND);

    return hNewTreeItem;
}

void CMachineView::DeleteMachineFromTree(HTREEITEM hMachineToBeDeleted)
{
    GetTreeCtrl().DeleteItem(hMachineToBeDeleted);
}

void CMachineView::OnRefreshProcess() 
{
    HTREEITEM hSelectedIntem = GetTreeCtrl().GetSelectedItem();

    TVITEM tvItem = {0};
    TCHAR szText[_MAX_PATH] = _T("");

    tvItem.hItem = hSelectedIntem;
    tvItem.mask = TVIF_TEXT | TVIF_HANDLE;
    tvItem.pszText = szText;
    tvItem.cchTextMax = _MAX_PATH;

    GetTreeCtrl().GetItem(&tvItem);
    
    RefreshProcesses(tvItem.pszText);
	
}

void CMachineView::OnLButtonDown(UINT nFlags, CPoint point) 
{
    HTREEITEM hTreeItem = GetTreeCtrl().HitTest(point, NULL); // See if any item has been it?

    if (hTreeItem != NULL)
    {
        TVITEM tvItem = {0};
        TCHAR szText[_MAX_PATH] = _T("");

        tvItem.hItem = hTreeItem;
        tvItem.mask = TVIF_HANDLE | TVIF_PARAM;
        tvItem.pszText = szText;
        tvItem.cchTextMax = _MAX_PATH;

        GetTreeCtrl().GetItem(&tvItem);

        if (tvItem.lParam == 0) // The root has 0 (zero) in it's lParam member
        {
            // Clear the list view when clicked on the root
            MFC_DocView::GetRemoteAdminView()->GetListCtrl().DeleteAllItems();
        }
        else
        {
            tvItem.hItem = hTreeItem;
            tvItem.mask = TVIF_HANDLE | TVIF_TEXT;
            GetTreeCtrl().GetItem(&tvItem);

            RefreshProcesses(tvItem.pszText);
        }
    }

	CTreeView::OnLButtonDown(nFlags, point);
}

void CMachineView::RefreshProcesses(CString strRemoteMachineIP)
{ 
    // Refresh the view to show the new processes
    (MFC_DocView::GetRemoteAdminView())->RefreshProcesses(strRemoteMachineIP);
}

void CMachineView::OnExecuteProcess() 
{
    // Get the selected item text, i.e IP of the machine
	TVITEM tvItem = {0};
    TCHAR szText[_MAX_PATH] = _T("");
    tvItem.hItem            = GetTreeCtrl().GetSelectedItem();
    tvItem.mask             = TVIF_TEXT | TVIF_HANDLE | TVIF_PARAM;
    tvItem.pszText          = szText;
    tvItem.cchTextMax       = _MAX_PATH;
    GetTreeCtrl().GetItem(&tvItem);

    CRemoteProcessDlg dlgRemoteProcess;
    
    if (dlgRemoteProcess.DoModal() == IDOK)
    {
        HANDLE hRemoteAdminProcessExecutePipe = GetDocument()->GetRemoteAdminProcessExecutePipe(tvItem.pszText);

        if (hRemoteAdminProcessExecutePipe != NULL)
        {
            // Send a command to continue the thread that will execute the remote process
            SCommand cmd;
            cmd.m_bThreadExit = FALSE;

            // Execute the command on the remote machine with the following parameters
            SExecuteCommand ExeCmd = {0};
            ::memcpy(ExeCmd.m_szDomain,      dlgRemoteProcess.m_strDomain.GetBuffer(0),      _MAX_PATH);
            ::memcpy(ExeCmd.m_szPassword,    dlgRemoteProcess.m_strPassword.GetBuffer(0),    _MAX_PATH);
            ::memcpy(ExeCmd.m_szProcessPath, dlgRemoteProcess.m_strProcessPath.GetBuffer(0), _MAX_PATH);
            ::memcpy(ExeCmd.m_szUsername,    dlgRemoteProcess.m_strUser.GetBuffer(0),        _MAX_PATH);
        
            BOOL bOk = FALSE;
            DWORD dwWritten = 0;

            bOk = ::WriteFile(hRemoteAdminProcessExecutePipe, &cmd,     sizeof(SCommand),        &dwWritten, NULL);
            bOk = ::WriteFile(hRemoteAdminProcessExecutePipe, &ExeCmd,  sizeof(SExecuteCommand), &dwWritten, NULL);

            TCHAR szMessage[_MAX_PATH] = _T("");
            DWORD dwRead = 0;

            // Wait for process triggering acknowledgement.
            bOk = ::ReadFile(hRemoteAdminProcessExecutePipe, szMessage, sizeof(szMessage), &dwRead, NULL);

            // There is some message that the process triggering was not sucessful
            // If it had been the string would be ""
            if (::strcmp(szMessage, _T("")) != 0)
            {
                CString strFormattedErrorMsg = ErrorHandling::ConvertStringTableIDToErrorMsg(tvItem.pszText, IDS_NOT_START_REMOTE_PROCESS);
                ::AfxMessageBox(strFormattedErrorMsg);
            }
        }
    }
}

CString CMachineView::GetSelectedItemTextInTreeView()
{
    HTREEITEM hSelectedItem = NULL;

    // Get the IP of the selected item
    hSelectedItem = GetTreeCtrl().GetSelectedItem();
    TVITEM tvItem = {0};
    tvItem.hItem   = hSelectedItem;
    tvItem.mask    = TVIF_PARAM | TVIF_HANDLE ; 
       
    GetTreeCtrl().GetItem(&tvItem);
        
    // If the root is not the current selection, then some IP is selected.
    // Return zero (0), if the root is selected, else get the selected item, which
    // will be the IP. LPARAM in the tree view item for root is zero.
    if (tvItem.lParam == 0)
    {
        return _T("0");
    }
    else
    {
        return GetTreeCtrl().GetItemText(hSelectedItem);
    }
    
    return _T("");
}

HTREEITEM CMachineView::GetTreeItemForText(CString strText)
{
    HTREEITEM hCurrent = GetTreeCtrl().GetNextItem(m_hMachineViewRoot, TVGN_CHILD);

    while (hCurrent != NULL)
    {
        // Get the text for the item. Notice we use TVIF_TEXT because
        // we want to retrieve only the text, but also specify TVIF_HANDLE
        // because we're getting the item by its handle.
       TVITEM item;
       TCHAR szText[1024];
       item.hItem = hCurrent;
       item.mask = TVIF_TEXT | TVIF_HANDLE;
       item.pszText = szText;
       item.cchTextMax = 1024;

       BOOL bWorked = GetTreeCtrl().GetItem(&item);
       
       if (bWorked)
       {
           if (::strcmp(strText.GetBuffer(0), item.pszText) == 0)
           {
               return hCurrent;
           }
       }
       // Try to get the next item
       hCurrent = GetTreeCtrl().GetNextItem(hCurrent, TVGN_NEXT);
    }

    // Cannot find the tree item with the strText;

    ASSERT(NULL);
    
    return NULL;
}



void CMachineView::OnShutdownHalt() 
{
    CString strIP = GetSelectedItemTextInTreeView();
    HANDLE hRemoteAdminSysShutDownPipe = GetDocument()->GetRemoteAdminSysShutDownPipe(strIP);

    if (hRemoteAdminSysShutDownPipe != NULL)
    {
        extern UINT g_iShutdownDelay; // Time given to shutdown, updated by the CTimeSettingsDlg
    
        SCommand cmd = {0};
        cmd.m_bThreadExit = FALSE;

        SSysShutDownInfo shutdownInfo = {0};
        shutdownInfo.bReboot = FALSE;
        shutdownInfo.bShutDown = TRUE;
        shutdownInfo.iTimeToShutDown = g_iShutdownDelay/1000; // we require time in secs.

        DWORD dwWritten = 0;
        DWORD dwRead    = 0;
        BOOL bOk        = FALSE;

        TCHAR szMessage[_MAX_PATH] = _T("");

        bOk = ::WriteFile(hRemoteAdminSysShutDownPipe, &cmd,     sizeof(SCommand),        &dwWritten, NULL);
        bOk = ::WriteFile(hRemoteAdminSysShutDownPipe, &shutdownInfo,  sizeof(SSysShutDownInfo), &dwWritten, NULL);
    
        bOk = ::ReadFile(hRemoteAdminSysShutDownPipe, szMessage, sizeof(szMessage), &dwRead, NULL);

        // There is some message that the process triggering was not sucessful
        // If it had been the string would be ""
        if (::strcmp(szMessage, _T("")) != 0)
        {
            ::AfxMessageBox(szMessage);
        }
    }
}

void CMachineView::OnShutdownReboot() 
{
    CString strIP = GetSelectedItemTextInTreeView();
    HANDLE hRemoteAdminSysShutDownPipe = GetDocument()->GetRemoteAdminSysShutDownPipe(strIP);

    if (hRemoteAdminSysShutDownPipe != NULL)
    {
        extern UINT g_iShutdownDelay; // Time given to shutdown, updated by the CTimeSettingsDlg

        // No need to quit the thread on the server side
        SCommand cmd = {0};
        cmd.m_bThreadExit = FALSE;

        // Fill the system shutdown requirements structure
        SSysShutDownInfo shutdownInfo = {0};
        shutdownInfo.bReboot          = TRUE;
        shutdownInfo.bShutDown        = TRUE;
        shutdownInfo.iTimeToShutDown  = g_iShutdownDelay/1000; // we require time in secs.

        DWORD dwWritten = 0;
        DWORD dwRead    = 0;
        BOOL bOk        = FALSE;

        TCHAR szMessage[_MAX_PATH] = _T("");

        bOk = ::WriteFile(hRemoteAdminSysShutDownPipe, &cmd,     sizeof(SCommand),        &dwWritten, NULL);
        bOk = ::WriteFile(hRemoteAdminSysShutDownPipe, &shutdownInfo,  sizeof(SSysShutDownInfo), &dwWritten, NULL);
    
        bOk = ::ReadFile(hRemoteAdminSysShutDownPipe, szMessage, sizeof(szMessage), &dwRead, NULL);

        // There is some message that the process triggering was not sucessful
        // If it had been the string would be ""
        if (::strcmp(szMessage, _T("")) != 0)
        {
            ::AfxMessageBox(szMessage);
        }
    }
}


void CMachineView::OnShutdownAbort() 
{
    CString strIP = GetSelectedItemTextInTreeView();
    HANDLE hRemoteAdminSysShutDownPipe = GetDocument()->GetRemoteAdminSysShutDownPipe(strIP);
    
    if (hRemoteAdminSysShutDownPipe != NULL)
    {
        SCommand cmd = {0};
        cmd.m_bThreadExit = FALSE;

        SSysShutDownInfo shutdownInfo = {0};
        shutdownInfo.bShutDown = FALSE;

        DWORD dwWritten = 0;
        DWORD dwRead    = 0;
        BOOL bOk        = FALSE;

        TCHAR szMessage[_MAX_PATH] = _T("");

        bOk = ::WriteFile(hRemoteAdminSysShutDownPipe, &cmd,     sizeof(SCommand),        &dwWritten, NULL);
        bOk = ::WriteFile(hRemoteAdminSysShutDownPipe, &shutdownInfo,  sizeof(SSysShutDownInfo), &dwWritten, NULL);
    
        bOk = ::ReadFile(hRemoteAdminSysShutDownPipe, szMessage, sizeof(szMessage), &dwRead, NULL);

        // There is some message that the process triggering was not sucessful
        // If it had been the string would be ""
        if (::strcmp(szMessage, _T("")) != 0)
        {
            ::AfxMessageBox(szMessage);
        }
    }
}
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -