//读服务器运行状态
int readRegImgSrvState(char** ppcRunState){
char *pcRunState;
HKEY hKEY;
LPCTSTR keyItem = "SOFTWARE\\Utan\\UtanImg\\";
unsigned char runState[] = {"0"};
long rv = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,keyItem, 0, KEY_READ, &hKEY);
if(rv != ERROR_SUCCESS){ //如果无法打开hKEY,则终止程序的执行
rv = RegCreateKeyEx(HKEY_LOCAL_MACHINE, keyItem,0,REG_NONE,
REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKEY,NULL);
if(rv != ERROR_SUCCESS) {
AfxMessageBox("错误:无法创建注册表项!");
return -1;
}
rv = ::RegSetValueEx(hKEY,"SrvRunState",NULL,REG_SZ,runState,2);
if(rv != ERROR_SUCCESS) {
AfxMessageBox("错误:无法设置有关的注册表信息");
return -1;
}
}else{
LPBYTE state = new BYTE[10];
DWORD type1 = REG_SZ;
DWORD dataLen = 10;
rv =::RegQueryValueEx(hKEY, "UtanImgSrvRunState", NULL, &type1, state, &dataLen);
if( rv != ERROR_SUCCESS){
AfxMessageBox("错误: 无法查询有关注册表信息!");
return -1;
}
pcRunState = new char[dataLen];
memmove(pcRunState,state,dataLen);
*ppcRunState = pcRunState;
//AfxMessageBox(pcRunState);
}
::RegCloseKey(hKEY);
return 0;
}
/**
* 写服务器运行状态
*/
int writeRegImgSrvState(const char* state){
HKEY hKEY;
LPCTSTR keyItem = "SOFTWARE\\Utan\\UtanImg\\";
long rv = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,keyItem, 0, KEY_READ|KEY_WRITE, &hKEY);
if(rv != ERROR_SUCCESS){ //如果无法打开hKEY,则终止程序的执行
rv = RegCreateKeyEx(HKEY_LOCAL_MACHINE, keyItem,0,REG_NONE,
REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKEY,NULL);
if(rv != ERROR_SUCCESS) {
AfxMessageBox("错误:无法创建注册表项!");
return -1;
}
}else{
int size = strlen(state) + 1;
unsigned char* data = new unsigned char[size]; //{"running"};
memmove(data,state,size);
printf("sizeof(data)=%d", sizeof(data));
rv = ::RegSetValueEx(hKEY,"UtanImgSrvRunState",NULL,REG_SZ,data,sizeof(data));
if(rv != ERROR_SUCCESS) {
printf("rv=%d",rv);
AfxMessageBox("错误:无法设置有关的注册表信息");
return -1;
}
}
::RegCloseKey(hKEY);
return 0;
}
参考:http://www.vckbase.com/document/viewdoc/?id=442