现在的位置: 首页 > 综合 > 正文

无线承载(二)

2018年06月06日 ⁄ 综合 ⁄ 共 11950字 ⁄ 字号 评论关闭
/********************
MyLog.h
*********************/
/********************************************
* Function: 输出Log
*
*********************************************/
#pragma once
#ifdef _DEBUG // 判断是否定义_DEBUG 
#define new DEBUG_NEW
// 定义调试new宏,取代new关键字 
#endif // 结束
#include <fstream>
using namespace std;
#define OutMyLog(line)
outMyTrace<<line //
#define CloseMyLog()
outMyTrace.close()
//关闭
//#define OUT_MY_LOG
/********************
WlanDefine.h
*********************/
#pragma once
#include <afxstr.h>
#ifdef _DEBUG // 判断是否定义_DEBUG 
#undef THIS_FILE // 取消THIS_FILE的定义 
static char THIS_FILE[]=__FILE__;
// 定义THIS_FILE指向文件名 
#define new DEBUG_NEW
// 定义调试new宏,取代new关键字 
#endif // 结束
#define REG_ROOT_KEY
HKEY_CURRENT_USER
#define REG_KEY_NAME
"Software\\Microsoft\\UniproTool\\WlanShareTool\\WlanData"
#define TEMP_LENGTH
512
#define S_HOST_SSID
40
#define S_USER_PASSWD
42
#define S_HOST_SSID_STRING
_T("SSID")
#define S_USER_PASSWD_STRING
_T("PASSWD")
//Login结构体
typedef struct WLAN_DATA_A{
CStringA strSSID;
//SSID
CStringA strPasswd;
//密码
WLAN_DATA_A & operator=(const WLAN_DATA_A& SrcStruct)
{
strSSID
= SrcStruct.strSSID;
strPasswd
= SrcStruct.strPasswd;
return *this;
}
}WlanDataA,*WlanDataAPtr;
//Login结构体
#if 1
typedef struct WLAN_DATA{
CString strSSID;
//地址或者名字localhost ip
CString strPasswd;
//端口号
WLAN_DATA & operator=(const WLAN_DATA& SrcStruct)
{
strSSID
= SrcStruct.strSSID;
strPasswd
= SrcStruct.strPasswd;
return *this;
}
}WlanData,*WlanDataPtr;
#endif
typedef struct REG_DATA_STRUCT_TC{
TCHAR sMainKeyName[TEMP_LENGTH];
//
TCHAR sSetKeyName[TEMP_LENGTH];
//
TCHAR sSetKeyValue[TEMP_LENGTH];
//
REG_DATA_STRUCT_TC & operator=(const REG_DATA_STRUCT_TC& SrcStruct)
{
_tcscpy(sMainKeyName,SrcStruct.sMainKeyName);
_tcscpy(sSetKeyName,SrcStruct.sSetKeyName);
_tcscpy(sSetKeyValue,SrcStruct.sSetKeyValue);
return *this;
}
}RegDataStructTc,*RegDataStructTcPtr;
/********************
WlanData.h
*********************/
#pragma once
#include "WlanDefine.h"
#ifdef _DEBUG // 判断是否定义_DEBUG 
#define new DEBUG_NEW
// 定义调试new宏,取代new关键字 
#endif // 结束
#define TEMP_LENGTH
512
/******************************************
* Name: CreateMainKeyA
* Func: 创建主键
*******************************************/
int CreateMainKeyA ( HKEY ReRootKey,HKEY &hKey,char * sCreatKeyName,char * sResult = NULL);
int SetValue_S_A( HKEY ReRootKey,HKEY &hKey,char *ReSubKey,char *ReValueName, BYTE *ReSetContent_S,char * sLog = NULL);
int GetValue_S_A ( HKEY ReRootKey,HKEY &hKey,char *cSubKey,char *cValueName,char *cResult,char *sLog = NULL);
void GetKeyNameAndValueFromStructA(int iID,CStringA &sSetKeyName,CStringA &sSetKeyValue,const WlanDataA &tWlanDataA);
void GetKeyNameFromStructA(int iID,CStringA &sSetKeyName);
void SaveKeyValueToStructA(int iID,char *sSetKeyValue,WlanDataA &tWlanDataA);
void ChangeCStringATochar(CStringA sSrc,char *sDest);
void ChangeCharToCStringA(char *sSrc,CStringA &sDest);
void ChangeCStringToTCHAR(CString sSrc,TCHAR *sDest);
void ChangeTCHARToCString(TCHAR *sSrc,CString &sDest);
/********************************************************
* FUNC: CStringA转BYTE
* OTHER:存在内存泄露 new了之后没有delete
*********************************************************/
LPBYTE CStringAToBYTE(CStringA str);
/**********************************************************
* FUNC: 读取注册表CStringA数据,初始化WlanData数据,若获取注册表数据失败,则采用默认配置
***********************************************************/
bool InitWlanDataRegA(HKEY &hKey,WlanDataA &tWlanDataA,char *sLog);
/**********************************************************
* FUNC: 保存WlanData中CStringA类型数据,存入注册表 
***********************************************************/
bool SaveWlanDataRegA(HKEY hKey,const WlanDataA &tWlanDataA,char *sLog);
/**********************************************************
* FUNC: 默认配置WlanData     CStringA
***********************************************************/
void DefaultWlanDataA(WlanDataA &tWlanDataA);
/**********************************************************
* FUNC: CStrAToCStrW:CStringA转CStingW    CStrWToCStrA:CStringW转CStringA
***********************************************************/
CStringW CStrAToCStrW(const CStringA &cstrSrcA);
CStringA CStrWToCStrA(const CStringW &cstrSrcW);
/**********************************************************
* FUNC: 以结构体强转:CStrAToCStrW:CStringA转CSting    CStrWToCStrA:CString转CStringA
***********************************************************/
void CopyWlanDataToA(const WlanData &tWlanData,WlanDataA &tWlanDataA);
void CopyWlanDataATo(const WlanDataA &tWlanDataA,WlanData &tWlanData);
/************************************
Func: CString转CStringA
************************************/
CStringA CStrToCStrA(CString sSrc);
/************************************
Func: 设置 注册表WlanA数据
************************************/
bool SetWlanDataRegA( HKEY hMainKey, HKEY &hKey, CStringA sHostA, char *sName);
bool SetWlanDataReg( HKEY hMainKey, HKEY &hKey, CString strSSID, char *sName);
/************************************
Func: 查询 注册表Wlan数据
************************************/
CString GetWlanDataReg(HKEY hMainKey,HKEY &hKey,char *sQuest,int iLen);
/*****************
WlanData.cpp
*****************/
#pragma once
#include "stdafx.h"
#include "WlanData.h"
#include "MyLog.h"
#ifdef OUT_MY_LOG
extern bool bMyTrace;
extern ofstream outMyTrace;
//启用
#endif
/*********************
* Name: CreateMainKeyA
* Func: 创建主键
* Input:HKEY 项 sCreateKeyName项路径
* Out: 0 TRUE 1 FALSE
sResult结果(默认NULL) 
**********************/
int CreateMainKeyA ( HKEY ReRootKey,HKEY &hKey,char * sCreatKeyName,char * sResult)
{
ASSERT(sCreatKeyName!=NULL);
#ifdef OUT_MY_LOG
OutMyLog("创建注册表Main项\n");
#endif
if((RegCreateKeyA(ReRootKey,sCreatKeyName,&hKey))!=ERROR_SUCCESS)
{
if(sResult != NULL)
sprintf(sResult,"创建指定KEY失败!");
return 1;
}
return 0;
}
int SetValue_S_A( HKEY ReRootKey,HKEY &hKey,char *ReSubKey,char *ReValueName, BYTE *ReSetContent_S,char * sLog)
{
#ifdef OUT_MY_LOG
OutMyLog("设置注册表键值\n");
OutMyLog(ReSetContent_S);
#endif
int i=0; //操作结果:0==succeed
if(RegOpenKeyExA(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey)==ERROR_SUCCESS)
{
if(RegSetValueExA(hKey,ReValueName,NULL,REG_SZ,ReSetContent_S,CStringA(ReSetContent_S).GetLength())!=ERROR_SUCCESS)
{
if(sLog != NULL)
sprintf(sLog,"错误:无法设置有关的注册表信息\n");
i=1;
}
RegCloseKey(hKey);
}
else
{
if(sLog != NULL)
sprintf(sLog,"错误:无法查询有关的注册表信息\n");
i=1;
}
return i;
}
/*********************
* Name: GetValue_S
* Func: 查看键值的函数
* Input:HKEY 路径项 hKey当前项
cSubKey项路径 cValueName获取的键名
cResult获取的值
* Out: 0 TRUE 1 FALSE
sLog 反馈字符串(默认NULL)
**********************/
int GetValue_S_A ( HKEY ReRootKey,HKEY &hKey,char *cSubKey,char *cValueName,char *cResult,char *sLog)
{
#ifdef OUT_MY_LOG
OutMyLog("获取注册表键值\n");
#endif
ASSERT(cSubKey!=NULL);
ASSERT(cValueName!=NULL);
ASSERT(cResult!=NULL);
int i=0; //操作结果:0==succeed
DWORD dwLen  = TEMP_LENGTH;
if(RegOpenKeyExA(ReRootKey,cSubKey,0,KEY_READ,&hKey)==ERROR_SUCCESS)
{
if(RegQueryValueExA(hKey,cValueName,0,NULL,(LPBYTE)cResult,&dwLen) != ERROR_SUCCESS)
{
if(sLog != NULL)
sprintf(sLog,"错误:查询注册表数据失败!\n");
i=1;
}
RegCloseKey(hKey);
}
else
{
if(sLog != NULL)
sprintf(sLog,"错误:无法打开有关的hKEY\n");
i=1;
}
return i;
}
void GetKeyNameAndValueFromStructA(int iID,CStringA &sSetKeyName,CStringA &sSetKeyValue,const WlanDataA &tWlanDataA)
{
switch(iID)
{
case S_HOST_SSID:
{
sSetKeyName
= S_HOST_SSID_STRING;
sSetKeyValue= tWlanDataA.strSSID;
}break;
case S_USER_PASSWD:
{
sSetKeyName = S_USER_PASSWD_STRING;
sSetKeyValue= tWlanDataA.strPasswd;
}break;
default:
return;
}
}
void GetKeyNameFromStructA(int iID,CStringA &sSetKeyName)
{
switch(iID)
{
case S_HOST_SSID:
{
sSetKeyName = S_HOST_SSID_STRING;
}break;
case S_USER_PASSWD:
{
sSetKeyName = S_USER_PASSWD_STRING;
}break;
default:
return;
}
}
void SaveKeyValueToStructA(int iID,char *sSetKeyValue,WlanDataA &tWlanDataA)
{
CStringA sTempValue;
ChangeCharToCStringA(sSetKeyValue,sTempValue);
switch(iID)
{
case S_HOST_SSID:
{
tWlanDataA.strSSID = sTempValue;
}break;
case S_USER_PASSWD:
{
tWlanDataA.strPasswd = sTempValue;
}break;
default:
break;
}
}
void ChangeCStringToTCHAR(CString sSrc,TCHAR *sDest)
{
int iLen = sSrc.GetLength();
_tcscpy(sDest,sSrc.GetBuffer(iLen));
sSrc.ReleaseBuffer(iLen);
}
void ChangeCStringATochar(CStringA sSrc,char *sDest)
{
int iLen = sSrc.GetLength();
sprintf(sDest,sSrc.GetBuffer(iLen));
sSrc.ReleaseBuffer(iLen);
}
void ChangeTCHARToCString(TCHAR *sSrc,CString &sDest)
{
sDest.Format(_T("%s"),sSrc);
}
void ChangeCharToCStringA(char *sSrc,CStringA &sDest)
{
sDest.Format(("%s"),sSrc);
}
/***********************************
* Name: LPBYTE CStringAToBYTE(CString str)
* FUNC: CStringA转BYTE
* OTHER:使用后必须delete,否则造成 内存泄露
************************************/
LPBYTE CStringAToBYTE(CStringA str)
{
LPBYTE btTempPtr=new BYTE [str.GetLength()+1];
for(int i=0;i<str.GetLength();i++)
btTempPtr[i]=str[i];
btTempPtr[str.GetLength( )]=0;
return btTempPtr;
}
bool InitWlanDataRegA(HKEY &hKey,WlanDataA &tWlanDataA,char *sLog)
{
#ifdef OUT_MY_LOG
OutMyLog("从注册表获取数据\n");
#endif
char cMainKeyName[TEMP_LENGTH]
= {0}; //项名
char cSetKeyName[TEMP_LENGTH]
= {0}; //键名
char cSetKeyValue[TEMP_LENGTH]
= {0}; //键值
CStringA csAMainKeyName,csASetKeyName;
csAMainKeyName = REG_KEY_NAME;
ChangeCStringATochar(csAMainKeyName,cMainKeyName);
if(CreateMainKeyA(HKEY_CURRENT_USER,hKey,REG_KEY_NAME,sLog)==1)
{
DefaultWlanDataA(tWlanDataA);
//默认配置
return false;
}
for(int i=S_HOST_SSID;i<S_USER_PASSWD+1;i++)
{
memset(cSetKeyName,0,sizeof(cSetKeyName));
memset(cSetKeyValue,0,sizeof(cSetKeyValue));
GetKeyNameFromStructA(i,csASetKeyName);
ChangeCStringATochar(csASetKeyName,cSetKeyName);
if(GetValue_S_A(REG_ROOT_KEY,hKey,cMainKeyName,cSetKeyName,cSetKeyValue,sLog)==1)
{
DefaultWlanDataA(tWlanDataA);
//默认配置
return false;
}
SaveKeyValueToStructA(i,cSetKeyValue,tWlanDataA);
}
return true;
}
bool SaveWlanDataRegA(HKEY hKey,const WlanDataA &tWlanDataA,char *sLog)
{
#ifdef OUT_MY_LOG
OutMyLog("信息存入注册表\n");
#endif
CStringA csAMainKeyName,csASetKeyName,csASetKeyValue;
char sMainKeyName[TEMP_LENGTH]
= {0}; //项名
char sSetKeyName[TEMP_LENGTH]
= {0}; //键名
char sSetKeyValue[TEMP_LENGTH]
= {0}; //键值
csAMainKeyName = REG_KEY_NAME;
ChangeCStringATochar(csAMainKeyName,sMainKeyName);
for(int i=S_HOST_SSID;i<S_USER_PASSWD+1;i++)
{
GetKeyNameAndValueFromStructA(i,csASetKeyName,csASetKeyValue,tWlanDataA);
ChangeCStringATochar(csASetKeyName,sSetKeyName);
ChangeCStringATochar(csASetKeyValue,sSetKeyValue);
SetValue_S_A(REG_ROOT_KEY,hKey,sMainKeyName,sSetKeyName,(LPBYTE)sSetKeyValue,sLog);
}
return true;
}
void DefaultWlanDataA(WlanDataA &tWlanDataA)
{
#ifdef OUT_MY_LOG
OutMyLog("默认配置\n");
#endif
tWlanDataA.strSSID
= "admin"; //SSID
tWlanDataA.strPasswd
= "12345678";
//密码
}
CStringW CStrAToCStrW(const CStringA &cstrSrcA)
{
int len = MultiByteToWideChar(CP_ACP, 0, LPCSTR(cstrSrcA), -1, NULL, 0);
wchar_t *wstr = new wchar_t[len];
memset(wstr, 0, len*sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, LPCSTR(cstrSrcA), -1, wstr, len);
CStringW cstrDestW = wstr;
delete[] wstr; 
return cstrDestW;
CStringA CStrWToCStrA(const CStringW &cstrSrcW)  
 {  
     int len = WideCharToMultiByte(CP_ACP, 0, LPCWSTR(cstrSrcW), -1, NULL, 0, NULL, NULL);  
     char *str = new char[len];  
     memset(str, 0, len);  
     WideCharToMultiByte(CP_ACP, 0, LPCWSTR(cstrSrcW), -1, str, len, NULL, NULL);  
     CStringA cstrDestA = str;  
     delete[] str;  
   
     return cstrDestA;  
 } 
void CopyWlanDataToA(const WlanData &tWlanData,WlanDataA &tWlanDataA)
{
#ifdef UNICODE
tWlanDataA.strSSID
= CStrWToCStrA(tWlanData.strSSID);
tWlanDataA.strPasswd
= tWlanData.strPasswd;
#else
tWlanDataA.strSSID
= tWlanData.strSSID;
tWlanDataA.strPasswd
= tWlanData.strPasswd;
#endif
}
void CopyWlanDataATo(const WlanDataA &tWlanDataA,WlanData &tWlanData)
{
#ifdef UNICODE
tWlanData.strSSID
= CStrAToCStrW(tWlanDataA.strSSID);
tWlanData.strPasswd
= tWlanDataA.strPasswd;
#else
tWlanData.strSSID
= tWlanDataA.strSSID;
tWlanData.strPasswd
= tWlanDataA.strPasswd;
#endif
}
/************************************
Func: CString转CStringA
************************************/
CStringA CStrToCStrA(CString sSrc)
{
CStringA sDest;
#ifdef UNICODE
sDest = CStrWToCStrA(sSrc);
#else
sDest = sSrc;
#endif
return sDest;
}
/************************************
Func: 设置 注册表WlanA数据
************************************/
bool SetWlanDataRegA( HKEY hMainKey, HKEY &hKey, CStringA sHostA, char *sName)
{
bool bResult = false;
BYTE *pBtHost = CStringAToBYTE(sHostA);
if(0 == SetValue_S_A(hMainKey,hKey,REG_KEY_NAME,sName,pBtHost))
bResult = true;
delete pBtHost;
return bResult;
}
bool SetWlanDataReg( HKEY hMainKey, HKEY &hKey, CString strSSID, char *sName)
{
CStringA sHostA;
sHostA = CStrToCStrA( strSSID);
return SetWlanDataRegA(hMainKey,hKey,sHostA,sName);
}
/************************************
Func: 查询 注册表Wlan数据
************************************/
CString GetWlanDataReg(HKEY hMainKey,HKEY &hKey,char *sQuest,int iLen)
{
char* cQuestResult = NULL;
CString csQuestResult;
cQuestResult = ( char* )calloc( iLen, sizeof(char));
GetValue_S_A( hMainKey, hKey, REG_KEY_NAME, sQuest, cQuestResult);
csQuestResult = cQuestResult;
delete cQuestResult;
return csQuestResult;
}

抱歉!评论已关闭.