#include "stdafx.h"
#include "addPath.h" #define _AFXDLL
#include <afxwin.h>
#include <iostream>
#include <tchar.h>
#include <afx.h>
#include <afxpriv.h>
using namespace std ; int AddPath(char * strPath)
{
HKEY hkResult;//键的句柄
LPCTSTR environmentKey =_T("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment");
//HKEY_CURRENT_USER HKEY_LOCAL_MACHINE
//LPCTSTR environmentKey =_T("Environment");
if(ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, environmentKey , ,KEY_SET_VALUE | KEY_READ , &hkResult)) //打开键environment
{
MessageBox(NULL,_T("访问注册表出错!\n请使用管理员帐户执行本程序!"),_T("ERROR"),MB_ICONERROR);
return ;
}
DWORD BufferSize = ; //数据长度
DWORD cbData; ////数据长度
DWORD dwRet;
VOID* PerfData = malloc( BufferSize );//数据存放地址
cbData = BufferSize; LPCTSTR myVariable=_T("PATH");//环境变量
dwRet = RegQueryValueEx( hkResult,myVariable, NULL, NULL, (LPBYTE) PerfData, &cbData ); //读取某子键下特定名称的值
while (dwRet == ERROR_MORE_DATA)
{
BufferSize += ;
PerfData = realloc( PerfData, BufferSize );
cbData = BufferSize;
dwRet = RegQueryValueEx( hkResult,myVariable,NULL,NULL,(LPBYTE) PerfData, &cbData );
}
//MessageBox(NULL,(LPCTSTR)PerfData,_T("我的系统变量Path"),MB_OK); if (dwRet == ERROR_SUCCESS)//名称不存在时,添加这个名称并设定其值
{
//判断是否存在myValue
if(IsPathExist(PerfData,strPath))
{
//MessageBox(NULL,_T("环境变量已经存在!"),_T("信息"),MB_OK);
RegCloseKey(hkResult);//释放键句柄
free(PerfData);
return ;
} LPTSTR myValueEx = new TCHAR[strlen(strPath)+];//此处,不是用sizeof() 函数,注意区别
strcpy((char*)myValueEx,strPath);
TCHAR lastChar=((LPTSTR)PerfData)[cbData-];//字符串以\0结尾
if(lastChar!=';')
{
strcpy((char*)myValueEx,";");
strcat((char*)myValueEx,strPath);
//printf("%s %s\n",strPath,myValueEx);
//printf("%d %d\n",strlen(strPath),strlen((char*)myValueEx));
} CString strMyValueEx;
USES_CONVERSION;
strMyValueEx = A2T((LPCSTR)myValueEx);
//MessageBox(NULL,strMyValueEx,_T("信息"),MB_OK); TCHAR * temp = new TCHAR[cbData+strlen((char*)myValueEx)];
_tcscpy_s(temp, cbData, (LPTSTR)PerfData);//拷贝path中原有的字符串到temp中
//printf("temp:%s\n\n",temp); LPTSTR pathValue = _tcscat(temp, strMyValueEx); //类似memcpy
//printf("pathValue:%s\n\n",pathValue); long setResult = RegSetValueEx(hkResult,myVariable,, /*REG_SZ*/REG_EXPAND_SZ, (LPBYTE) pathValue,_tcsclen(pathValue)*sizeof(TCHAR));//设置某子键下特定名称的值。
DWORD dwResult;
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE , ,LPARAM(_T("Environment")), SMTO_ABORTIFHUNG, , &dwResult);//广播立即执行
/*if(ERROR_SUCCESS == setResult)
{
MessageBox(NULL,_T("环境变量设置完成!"),_T("信息"),MB_OK);
}
else
{
MessageBox(NULL,_T("环境变量设置失败!"),_T("信息"),MB_OK);
}*/
delete []myValueEx;
delete []temp;
temp = NULL ;
}
else
{
MessageBox(NULL,_T("环境变量读取失败!"),_T("信息"),MB_OK);
}
//printf("\nEnd Register\n");
RegCloseKey(hkResult);//释放键句柄
free(PerfData);
PerfData = NULL ;
//printf("\nEnd AddPath\n");
return ;
} int IsPathExist(void *PerfData,char *myPath)
{
//return 0 ;
char * myoldPath = (char*)PerfData;
int i = ;
while(myoldPath[i] != '\0')
{
i = i + ;
}
char * path_t = new char[i/+]; i = ;
while(myoldPath[i] != '\0')
{
path_t[i/] = myoldPath[i];
i = i + ;
}
path_t[i/] = '\0';
path_t[i/+] = '\0'; CString strMyoldPath;
CString strMyPath;
USES_CONVERSION;
strMyoldPath = A2T((LPCSTR)path_t);
strMyPath = A2T((LPCSTR)myPath); if(strMyoldPath[strMyoldPath.GetLength()-] != ';')
{
strMyoldPath = strMyoldPath + _T(";") ;
}
if(strMyPath[strMyPath.GetLength()-] != ';')
{
strMyPath = strMyPath + _T(";") ;
} if(- == strMyoldPath.Find(strMyPath,))
{
delete path_t;
return ;
} delete path_t;
return ;
}

http://download.csdn.net/download/autumoonchina/8747921

最新文章

  1. linux不知道文件在哪,想查找文件内的字符串
  2. CF721C. Journey
  3. 【MySQL】frm文件解析
  4. python Django 学习笔记(四)—— 使用MySQL数据库
  5. Git 局域网简单配置
  6. mvc5 _ViewStart.cshtml 模板页如何定义
  7. cat命令的作用
  8. 使用val()另一个妙用------选中select/checkbox/radio的值
  9. iOS开发 自定义navigationleftItem 之后手势失效的问题
  10. 关于HTML5中audio标签在手机中的autoplay
  11. Java的图片处理工具类
  12. 1-3 Spring Bean 的属性值设置
  13. sdoi2018旧试题 证明
  14. 20165214 2018-2019-2 《网络对抗技术》Exp3 免杀原理与实践 Week5
  15. 深入C#的String类
  16. WPF插件开发:使用FrameworkElementAdapters时VS报错的问题
  17. Jquery中.attr()和.data()的区别
  18. 【linux基础】查看硬盘位置信息
  19. windows共享连接显示无法打开
  20. jmeter-plugins-dubbo &amp; DevToolBox

热门文章

  1. Java中try catch finally语句中含return语句的执行情况总结-编程陷阱
  2. Myeclipse自定义注释
  3. Kafka 简易教程
  4. C++结构体中的静态变量
  5. Mysql 查询一天中,每个小时数据的数量
  6. SDUT-3441_数据结构实验之二叉树二:遍历二叉树
  7. ros 工作空间下cpp文件调用其他cpp文件的函数或变量
  8. winfrom 中 label 文字随着窗体大小变化
  9. win10 子系统ubuntu中文乱码
  10. Pytorch - GPU ID 指定 pytorch gpu 指定