Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Remote Control PCs

, 20 Sep 2013
Two projects that work together to remote control PCs across a LAN.
// Remote Desktop System - remote controlling of multiple PC's
// Copyright (C) 2000-2009 GravyLabs LLC

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#pragma once

#include <wincrypt.h>

class CHash
{
public:
   CHash() : m_hCryptProv(NULL), m_hHash(NULL), m_hKey(NULL)
   {
      // Get handle to the default provider. 
      if (!CryptAcquireContext(&m_hCryptProv,NULL,MS_ENHANCED_PROV,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT))
      {
         if (!CryptAcquireContext(&m_hCryptProv,NULL,MS_ENHANCED_PROV,PROV_RSA_FULL,CRYPT_NEWKEYSET|CRYPT_VERIFYCONTEXT))
         {
            // If creating a new key container and an error code indicates it existed then some user information changed
            // delete the key container and re-create it
            if (!CryptAcquireContext(&m_hCryptProv,NULL,MS_ENHANCED_PROV,PROV_RSA_FULL,CRYPT_DELETEKEYSET|CRYPT_VERIFYCONTEXT))
               return;
            if (!CryptAcquireContext(&m_hCryptProv,NULL,MS_ENHANCED_PROV,PROV_RSA_FULL,CRYPT_NEWKEYSET|CRYPT_VERIFYCONTEXT))
               return;
         }
      }
   }

   ~CHash()
   {
      // Destroy the Hash
      if (m_hHash)
      {
         CryptDestroyHash(m_hHash); 
         m_hHash = NULL;
      }

      // Destroy the cryptographic session
      if (m_hKey) 
      {
         CryptDestroyKey(m_hKey);
         m_hKey = NULL;
      }
   
      // Release the cryptographic provider
      if (m_hCryptProv)
      {
         CryptReleaseContext(m_hCryptProv,0);
         m_hCryptProv = NULL;
      }
   }

   bool HashData(BYTE * pData,DWORD dwLen)
   {
      if (!m_hHash)
      {
         // Test for a good initialization
         if (!m_hCryptProv)
            return false;

         // Create the cryptographic hash
         if (!CryptCreateHash(m_hCryptProv,CALG_MD5,0,0,&m_hHash))
            return false;
      }

      // Add the data to the hash
      return CryptHashData(m_hHash,pData,dwLen,0) ? true : false;
   }

   // Get the hash 
   bool GetHash(CString & csHash)
   {
      bool bRet = false;
      if (m_hHash)
      {
         DWORD dwHashSize = 0;
         if (CryptGetHashParam(m_hHash,HP_HASHSIZE,NULL,&dwHashSize,0))
         {
            DWORD dwHashLen = 0;
            if (CryptGetHashParam(m_hHash,HP_HASHVAL,NULL,&dwHashLen,0))
            {
               std::auto_ptr<BYTE> Hash(new BYTE[dwHashLen]);
               BYTE * pbHash = Hash.get();;
               if (CryptGetHashParam(m_hHash,HP_HASHVAL,pbHash,&dwHashLen,0))
               {
                  // Create the string hash value
                  for(DWORD i = 0;i < dwHashLen;i++) // P#1089 DWORD was int
                  {
                     char sz[3] = {'\0'};
                     sprintf_s(sz,"%2.2x",pbHash[i]);
                     csHash += sz;
                  }

                  // Destroy the Hash since no more data can be added to it
                  CryptDestroyHash(m_hHash); 
                  m_hHash = NULL;
                  bRet = true;
               }
            }
         }
      }
      return bRet;
   }

protected:
   bool m_bError;
   HCRYPTPROV m_hCryptProv;
   HCRYPTHASH m_hHash;
   HCRYPTKEY m_hKey;
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Andy Bantly
Founder
United States United States
Working as a software developer since 1989. Started out with Basic, FORTRAN and JCL, moved into Visual Basic 1.0, C, then C++, and now I work mainly in C++ using MFC, Win32, and ATL/COM. I use Microsoft Products only because that is what gives me gainful employment. Through work, I have a lot of experience with HTML, JavaScript, XSL transformations, the XMLHTTP object, PHP 4.x, and simple COM object integrations.
 
I've worked for the University of Oklahoma in the school of meteorology (Go SOONERS!), consulting, and now as a Senior Software Engineer. These things keep my lights on and electricity going. My dream job is to own a bowling alley and rub elbows with pro-bowlers! I'm also an avid pedicab driver and have my own cab. I like the hustle of picking up people in downtown and biking them to their destination.
Follow on   Google+   LinkedIn

| Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 20 Sep 2013
Article Copyright 2000 by Andy Bantly
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid