Files
SimpleRemoter/client/RegisterOperation.cpp
T
yuanyuanxianganddeepseek-flash 315a0855dc Feature: Let the client report structured failures for MCP tools
Four new client-to-server tokens carry a win32/LSTATUS code when a
command fails instead of returning empty data: TOKEN_SERVICE_ERROR
(170), TOKEN_FILE_LIST_ERROR (171), TOKEN_REG_ERROR (172) and
TOKEN_DOWNLOAD_ERROR (173). They sit on byte values that were unused,
so existing numbering is untouched.

The three data paths that used to collapse "failed" and "empty" into
the same packet now distinguish them: OpenSCManager failures, directory
enumeration failures and RegOpenKeyEx failures all report the code that
was actually returned, and UploadToRemoteV2 reports why it found no
file to transfer before the transfer sub-connection is ever created.

Every failure branch still sends the packet the old server expects
(TOKEN_FILE_LIST, the empty TOKEN_REG_PATH/KEY pair, the ClientMsg
popup), so a new client talking to an old server degrades to exactly
today's behaviour instead of timing out. Success paths are byte-for-
byte unchanged.

Co-Authored-By: deepseek-flash
2026-10-25 17:59:17 +01:00

200 lines
6.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// RegisterOperation.cpp: implementation of the RegisterOperation class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "RegisterOperation.h"
#include "Common.h"
#include <IOSTREAM>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
enum MYKEY {
MHKEY_CLASSES_ROOT,
MHKEY_CURRENT_USER,
MHKEY_LOCAL_MACHINE,
MHKEY_USERS,
MHKEY_CURRENT_CONFIG
};
enum KEYVALUE {
MREG_SZ, // REG_SZ
MREG_DWORD, // REG_DWORD
MREG_BINARY, // REG_BINARY
MREG_EXPAND_SZ, // REG_EXPAND_SZ
MREG_MULTI_SZ, // REG_MULTI_SZ
MREG_QWORD, // REG_QWORD
MREG_NONE // REG_NONE / 未知类型
};
struct REGMSG {
int count; //名字个数
DWORD size; //名字大小
DWORD valsize; //值大小
};
RegisterOperation::RegisterOperation(char bToken)
{
switch(bToken) {
case MHKEY_CLASSES_ROOT:
MKEY=HKEY_CLASSES_ROOT;
break;
case MHKEY_CURRENT_USER:
MKEY=HKEY_CURRENT_USER;
break;
case MHKEY_LOCAL_MACHINE:
MKEY=HKEY_LOCAL_MACHINE;
break;
case MHKEY_USERS:
MKEY=HKEY_USERS;
break;
case MHKEY_CURRENT_CONFIG:
MKEY=HKEY_CURRENT_CONFIG;
break;
default:
MKEY=HKEY_LOCAL_MACHINE;
break;
}
ZeroMemory(KeyPath,MAX_PATH);
m_lastOpenStatus = ERROR_SUCCESS;
}
RegisterOperation::~RegisterOperation()
{
}
char* RegisterOperation::FindPath()
{
char *szBuffer=NULL;
HKEY hKey; //注册表返回句柄
/*打开注册表 User kdjfjkf\kdjfkdjf\ */
m_lastOpenStatus = RegOpenKeyEx(MKEY,KeyPath,0,KEY_READ,&hKey);
if(m_lastOpenStatus==ERROR_SUCCESS) { //打开
DWORD dwIndex=0,NameCount,NameMaxLen;
DWORD KeySize,KeyCount,KeyMaxLen,MaxDataLen;
//这就是枚举了
if(RegQueryInfoKey(hKey,NULL,NULL,NULL,&KeyCount, //14
&KeyMaxLen,NULL,&NameCount,&NameMaxLen,&MaxDataLen,NULL,NULL)!=ERROR_SUCCESS) {
return NULL;
}
//一点保护措施
KeySize=KeyMaxLen+1;
if(KeyCount>0&&KeySize>1) {
int Size=sizeof(REGMSG)+1;
DWORD DataSize=KeyCount*KeySize+Size+1; //[TOKEN_REG_PATH][2 11 ccccc\0][11][11]
szBuffer=(char*)LocalAlloc(LPTR, DataSize);
if (szBuffer == NULL) {
return NULL;
}
ZeroMemory(szBuffer,DataSize);
szBuffer[0]=TOKEN_REG_PATH; //命令头
REGMSG msg; //数据头
msg.size=KeySize;
msg.count=KeyCount;
memcpy(szBuffer+1,(void*)&msg,sizeof(REGMSG));
char * szTemp=new char[KeySize];
for(dwIndex=0; dwIndex<KeyCount; dwIndex++) { //枚举项
ZeroMemory(szTemp,KeySize);
DWORD i=KeySize; //21
RegEnumKeyEx(hKey,dwIndex,szTemp,&i,NULL,NULL,NULL,NULL);
strcpy(szBuffer+dwIndex*KeySize+Size,szTemp);
}
delete[] szTemp;
RegCloseKey(hKey);
}
}
return szBuffer;
}
void RegisterOperation::SetPath(char *szPath)
{
ZeroMemory(KeyPath,MAX_PATH);
strcpy(KeyPath,szPath);
}
char* RegisterOperation::FindKey()
{
char *szValueName; //键值名称
LPBYTE szValueData; //键值数据
char *szBuffer=NULL;
HKEY hKey; //注册表返回句柄
m_lastOpenStatus = RegOpenKeyEx(MKEY,KeyPath,0,KEY_READ,&hKey);
if(m_lastOpenStatus==ERROR_SUCCESS) { //打开
DWORD dwIndex=0,NameSize,NameCount,NameMaxLen,Type;
DWORD KeyCount,KeyMaxLen,DataSize,MaxDataLen;
//这就是枚举了
if(RegQueryInfoKey(hKey,NULL,NULL,NULL,
&KeyCount,&KeyMaxLen,NULL,&NameCount,&NameMaxLen,&MaxDataLen,NULL,NULL)!=ERROR_SUCCESS) {
return NULL;
}
if(NameCount>0&&MaxDataLen>0) {
DataSize=MaxDataLen+1;
NameSize=NameMaxLen+100;
REGMSG msg;
msg.count=NameCount; //总个数
msg.size=NameSize; //名字大小
msg.valsize=DataSize; //数据大小
const int msgsize=sizeof(REGMSG);
// 头 标记 名字 数据
DWORD size=sizeof(REGMSG)+
sizeof(BYTE)*NameCount+ NameSize*NameCount+DataSize*NameCount+10;
szBuffer = (char*)LocalAlloc(LPTR, size);
if (szBuffer==NULL) {
return NULL;
}
ZeroMemory(szBuffer,size);
szBuffer[0]=TOKEN_REG_KEY; //命令头
memcpy(szBuffer+1,(void*)&msg,msgsize); //数据头
szValueName=(char *)malloc(NameSize);
szValueData=(LPBYTE)malloc(DataSize);
if (szValueName==NULL||szValueData == NULL) {
return NULL;
}
char *szTemp=szBuffer+msgsize+1;
for(dwIndex=0; dwIndex<NameCount; dwIndex++) { //枚举键值
ZeroMemory(szValueName,NameSize);
ZeroMemory(szValueData,DataSize);
DataSize=MaxDataLen+1;
NameSize=NameMaxLen+100;
RegEnumValue(hKey,dwIndex,szValueName,&NameSize,
NULL,&Type,szValueData,&DataSize);//读取键值
// 值类型映射:REG_SZ/DWORD/BINARY/EXPAND_SZ 之外补 MULTI_SZ/QWORD/NONE,
// 未识别类型一律 MREG_NONE(避免误报为 REG_SZ)。
switch (Type) {
case REG_SZ: szTemp[0] = MREG_SZ; break;
case REG_EXPAND_SZ: szTemp[0] = MREG_EXPAND_SZ; break;
case REG_MULTI_SZ: szTemp[0] = MREG_MULTI_SZ; break;
case REG_DWORD: szTemp[0] = MREG_DWORD; break;
case REG_QWORD: szTemp[0] = MREG_QWORD; break;
case REG_BINARY: szTemp[0] = MREG_BINARY; break;
case REG_NONE: szTemp[0] = MREG_NONE; break;
default: szTemp[0] = MREG_NONE; break;
}
szTemp+=sizeof(BYTE);
strcpy(szTemp,szValueName);
szTemp+=msg.size;
memcpy(szTemp,szValueData,msg.valsize);
szTemp+=msg.valsize;
}
free(szValueName);
free(szValueData);
}
}
return szBuffer;
}