一个适应多个Windows版本的RAS检测类

这是我前不久遇到的一个问题,使用Windows的API函数RasEnumConnections检测RAS连接时,每个操作系统版本都必须提供不一样的RASCONN结构体,而我这个程序又必须能够运行在任何的Windows版本下,所以试着写了一个类模板。。。。(初学新手,有什么问题望各位大侠指正)

ExpandedBlockStart.gif ContractedBlock.gif /**/ /* 文件:RasDetectT.h
InBlock.gif * 功能:检测、断开RAS连接
InBlock.gif * 作者:shootingstars (zhouhuis22 at sina.com.cn)
InBlock.gif * 日期:2004-11-4 20:18
InBlock.gif * 使用例子:
InBlock.gif *         CRasDetect *obj = CRasDetect::GetInstance();
InBlock.gif *        obj->EnumConnections();
InBlock.gif *        obj->HangupAll();    
ExpandedBlockEnd.gif 
*/
 
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 结构体定义
InBlock.gif * 针对Windows的各个版本,定义不同的RASCONN结构体
ExpandedBlockEnd.gif 
*/

None.gif
struct  RASCONNWXP    // for WinXP/2003
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif DWORD    dwSize;
InBlock.gif HRASCONN hrasconn;
InBlock.gif CHAR     szEntryName[ RAS_MaxEntryName 
+ 1 ];
InBlock.gif CHAR     szDeviceType[ RAS_MaxDeviceType 
+ 1 ];
InBlock.gif CHAR     szDeviceName[ RAS_MaxDeviceName 
+ 1 ]; 
InBlock.gif
InBlock.gif CHAR     szPhonebook [ MAX_PATH ];
InBlock.gif DWORD    dwSubEntry; 
InBlock.gif
InBlock.gif GUID    guidEntry;
InBlock.gif DWORD dwFlags;
InBlock.gif LUID    luid;
ExpandedBlockEnd.gif}
;
None.gif
struct  RASCONN2000   // for Win2000
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif DWORD    dwSize;
InBlock.gif HRASCONN hrasconn;
InBlock.gif CHAR     szEntryName[ RAS_MaxEntryName 
+ 1 ];
InBlock.gif CHAR     szDeviceType[ RAS_MaxDeviceType 
+ 1 ];
InBlock.gif CHAR     szDeviceName[ RAS_MaxDeviceName 
+ 1 ];
InBlock.gif CHAR     szPhonebook [ MAX_PATH ];
InBlock.gif DWORD    dwSubEntry;
InBlock.gif GUID     guidEntry;
ExpandedBlockEnd.gif}
;
None.gif
struct  RASCONNNT4   // for WinNT4
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif DWORD    dwSize;
InBlock.gif HRASCONN hrasconn;
InBlock.gif CHAR     szEntryName[ RAS_MaxEntryName 
+ 1 ];
InBlock.gif CHAR     szDeviceType[ RAS_MaxDeviceType 
+ 1 ];
InBlock.gif CHAR     szDeviceName[ RAS_MaxDeviceName 
+ 1 ]; 
InBlock.gif
InBlock.gif CHAR     szPhonebook [ MAX_PATH ];
InBlock.gif DWORD    dwSubEntry;
ExpandedBlockEnd.gif}
;
None.gif
struct  RASCONNW9X   // for Win98/95
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif DWORD    dwSize;
InBlock.gif HRASCONN hrasconn;
InBlock.gif CHAR     szEntryName [ RAS_MaxEntryName 
+ 1 ];
InBlock.gif CHAR     szDeviceType[ RAS_MaxDeviceType 
+ 1 ];
InBlock.gif CHAR     szDeviceName[ RAS_MaxDeviceName 
+ 1 ];
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif * 客户使用接口
ExpandedBlockEnd.gif 
*/

None.gif
class  CRasDetect
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif 
virtual bool EnumConnections() = 0// 枚举所有RAS连接
InBlock.gif
 virtual bool HangupAll() = 0;       // 断开所有RAS连接
InBlock.gif
 virtual bool Hangup(int index) = 0// 断开某个(index)RAS连接
InBlock.gif
 static  CRasDetect *GetInstance();  // 返回特定操作系统版本的CRasDetectT对象(Singleton模式,不支持多线程)
InBlock.gif
private:
InBlock.gif 
static  CRasDetect *Instance;
ExpandedBlockEnd.gif}
;
None.gifCRasDetect 
* CRasDetect::Instance  =  NULL; 
None.gif
None.giftemplate 
< class  RASCONNT >
None.gif
class  CRasDetectT :  public  CRasDetect
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif CRasDetectT();
InBlock.gif 
~CRasDetectT();
InBlock.gif 
bool EnumConnections();
InBlock.gif 
bool HangupAll();
InBlock.gif 
bool Hangup(int index);
InBlock.gif
protected:
InBlock.gif RASCONNT   
*m_lprasconn;
InBlock.gif DWORD     m_connectionCount;           
// 枚举获得的连接数目
InBlock.gif
 DWORD     m_structSize;                // 当前系统RASCONN结构体的大小
ExpandedBlockEnd.gif
}

None.gif
None.giftemplate 
< class  RASCONNT >
None.gifCRasDetectT
< RASCONNT > ::CRasDetectT()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif m_structSize 
= sizeof(RASCONNT);
ExpandedBlockEnd.gif}
 
None.gif
None.giftemplate 
< class  RASCONNT >
None.gifCRasDetectT
< RASCONNT > :: ~ CRasDetectT()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedBlockEnd.gif}
 
None.gif
None.giftemplate 
< class  RASCONNT >
None.gif
bool  CRasDetectT < RASCONNT > ::EnumConnections()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif m_lprasconn 
= (RASCONNT *)new BYTE[m_structSize];
InBlock.gif DWORD dwCb;
InBlock.gif dwCb 
= m_structSize;
InBlock.gif m_lprasconn
->dwSize = m_structSize;
InBlock.gif DWORD dwErr 
= RasEnumConnections(
InBlock.gif  (RASCONN
*)m_lprasconn,
InBlock.gif  
&dwCb,
InBlock.gif  
&m_connectionCount);
InBlock.gif 
if (ERROR_BUFFER_TOO_SMALL == dwErr)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  delete m_lprasconn;
InBlock.gif  m_lprasconn 
= (RASCONNT *)new BYTE[m_structSize];
InBlock.gif  m_lprasconn
->dwSize = m_structSize;
InBlock.gif  dwErr 
= RasEnumConnections(
InBlock.gif   (RASCONN
*)m_lprasconn,
InBlock.gif   
&dwCb,
InBlock.gif   
&m_connectionCount);
InBlock.gif  
if(dwErr == ERROR_SUCCESS)
InBlock.gif   
return true;
InBlock.gif  
else 
InBlock.gif   
return false;
ExpandedSubBlockEnd.gif }

InBlock.gif 
else if(dwErr == ERROR_SUCCESS)
InBlock.gif  
return true;
InBlock.gif 
else
InBlock.gif  
return false;
ExpandedBlockEnd.gif}
 
None.gif
None.giftemplate 
< class  RASCONNT >
None.gif
bool  CRasDetectT < RASCONNT > ::Hangup( int  index)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif DWORD err 
= RasHangUp(m_lprasconn[index].hrasconn);
InBlock.gif 
if(err == 0)
InBlock.gif  
return true;
InBlock.gif 
else
InBlock.gif  
return false;
ExpandedBlockEnd.gif}
 
None.gif
None.giftemplate 
< class  RASCONNT >
None.gif
bool  CRasDetectT < RASCONNT > ::HangupAll()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
bool result = true;
InBlock.gif 
for(DWORD i = 0 ; i < m_connectionCount ; i++)
InBlock.gif  
if(!Hangup(i))
InBlock.gif   result 
= false;
InBlock.gif 
return result;
ExpandedBlockEnd.gif}
 
None.gif
None.gif
//  返回当前操作系统的CRasDetectT对象
None.gif
CRasDetect *  CRasDetect::GetInstance()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
if(Instance)
InBlock.gif  
return Instance; 
InBlock.gif
InBlock.gif OSVERSIONINFOEX osvi;
InBlock.gif BOOL bOsVersionInfoEx; 
InBlock.gif
InBlock.gif ZeroMemory(
&osvi, sizeof(OSVERSIONINFOEX));
InBlock.gif osvi.dwOSVersionInfoSize 
= sizeof(OSVERSIONINFOEX); 
InBlock.gif
InBlock.gif 
if!(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *&osvi)) )
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  osvi.dwOSVersionInfoSize 
= sizeof (OSVERSIONINFO);
InBlock.gif  
if (! GetVersionEx ( (OSVERSIONINFO *&osvi) ) 
InBlock.gif   
return NULL;
ExpandedSubBlockEnd.gif }
 
InBlock.gif
InBlock.gif 
switch(osvi.dwPlatformId)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif 
case VER_PLATFORM_WIN32_WINDOWS: // Win95/Win98
InBlock.gif
  return new CRasDetectT<RASCONNW9X>;
InBlock.gif 
case VER_PLATFORM_WIN32_NT:
InBlock.gif  
if(osvi.dwMajorVersion>=5)       // Windows 2000( NT5.0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif
  dot.gif{
InBlock.gif   
if(osvi.dwMinorVersion >= 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return new CRasDetectT<RASCONNWXP>;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return new CRasDetectT<RASCONN2000>;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }
  
InBlock.gif  
else    // NT 4.0
ExpandedSubBlockStart.gifContractedSubBlock.gif
  dot.gif{
InBlock.gif   
return new CRasDetectT<RASCONNNT4>;
ExpandedSubBlockEnd.gif  }
  
InBlock.gif  
break;
ExpandedSubBlockEnd.gif }

InBlock.gif 
return NULL;
ExpandedBlockEnd.gif}

None.gif
None.gif

转载于:https://www.cnblogs.com/shootingstars/archive/2004/11/20/66159.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的RAS加密解密程序的C++实现: ``` #include <iostream> #include <openssl/rsa.h> #include <openssl/pem.h> #include <openssl/err.h> using namespace std; // 生成RSA密钥对 RSA* generateRSAKeyPair(int keyLength) { RSA* rsa = NULL; BIGNUM* bn = NULL; FILE* fp = NULL; int ret = 0; // 创建RSA结构体 rsa = RSA_new(); if (rsa == NULL) { return NULL; } // 生成大数 bn = BN_new(); ret = BN_set_word(bn, RSA_F4); if (ret != 1) { goto free_all; } // 生成RSA密钥对 ret = RSA_generate_key_ex(rsa, keyLength, bn, NULL); if (ret != 1) { goto free_all; } // 返回生成的RSA密钥对 return rsa; free_all: if (rsa != NULL) { RSA_free(rsa); } if (bn != NULL) { BN_free(bn); } if (fp != NULL) { fclose(fp); } return NULL; } // RSA加密 int RSAEncrypt(const unsigned char* inData, int inLen, unsigned char* outData, RSA* rsa) { int ret = 0; // 获取RSA公钥 const BIGNUM* e = RSA_get0_e(rsa); const BIGNUM* n = RSA_get0_n(rsa); // 创建RSA公钥结构体 RSA* pubRSA = RSA_new(); ret = RSA_set0_key(pubRSA, const_cast<BIGNUM*>(n), const_cast<BIGNUM*>(e), NULL); if (ret != 1) { goto free_all; } // RSA加密 ret = RSA_public_encrypt(inLen, inData, outData, pubRSA, RSA_PKCS1_PADDING); if (ret < 0) { goto free_all; } // 释放RSA公钥结构体 RSA_free(pubRSA); // 返回加密数据长度 return ret; free_all: if (pubRSA != NULL) { RSA_free(pubRSA); } return -1; } // RSA解密 int RSADecrypt(const unsigned char* inData, int inLen, unsigned char* outData, RSA* rsa) { int ret = 0; // 获取RSA私钥 const BIGNUM* d = RSA_get0_d(rsa); const BIGNUM* n = RSA_get0_n(rsa); // 创建RSA私钥结构体 RSA* priRSA = RSA_new(); ret = RSA_set0_key(priRSA, const_cast<BIGNUM*>(n), const_cast<BIGNUM*>(d), NULL); if (ret != 1) { goto free_all; } // RSA解密 ret = RSA_private_decrypt(inLen, inData, outData, priRSA, RSA_PKCS1_PADDING); if (ret < 0) { goto free_all; } // 释放RSA私钥结构体 RSA_free(priRSA); // 返回解密数据长度 return ret; free_all: if (priRSA != NULL) { RSA_free(priRSA); } return -1; } int main() { RSA* rsa = NULL; unsigned char inData[1024] = "Hello, World!"; unsigned char outData[1024] = {0}; int outLen = 0; // 生成RSA密钥对 rsa = generateRSAKeyPair(1024); if (rsa == NULL) { cout << "Generate RSA key pair failed!" << endl; return -1; } // RSA加密 outLen = RSAEncrypt(inData, strlen((const char*)inData), outData, rsa); if (outLen < 0) { cout << "RSA encrypt failed!" << endl; return -1; } // RSA解密 outLen = RSADecrypt(outData, outLen, inData, rsa); if (outLen < 0) { cout << "RSA decrypt failed!" << endl; return -1; } // 输出解密结果 cout << "RSA decrypt result: " << inData << endl; // 释放RSA密钥对 RSA_free(rsa); return 0; } ``` 这个程序使用了OpenSSL库来实现RAS加密解密。在程序中,先使用`generateRSAKeyPair`函数生成RSA密钥对,然后使用`RSAEncrypt`函数来加密数据,使用`RSADecrypt`函数来解密数据。程序最终输出解密结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值