.h
#pragma once
#include "pch.h"
class m_lu
{
public:
m_lu();
m_lu(CString m_jing_path);
~m_lu();
CString data() {//获取返回值
m_jing_path;
return m_jing_path;
}
void m_jing();
private:
CString m_jing_path;
};
----------------------------------------------------------------------------------------------------------
.cpp
#include "pch.h"
#include "m_lu.h"
m_lu::m_lu()
{
}
m_lu::m_lu(CString m_jing_path1){//初始化
m_jing_path = m_jing_path1;
}
m_lu::~m_lu()
{
}
void m_lu::m_jing()//class
{
HKEY hkey = NULL;//注册表句柄默认为空
TCHAR* lpszSubkey = (TCHAR*)_T("SOFTWARE\\Classes\\ldmnq.apk\\Shell\\Open\\Command");//注册表路径除去开头\\后面的都需要
LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszSubkey, 0, KEY_ALL_ACCESS, &hkey);//打开注册表
if (lRet == ERROR_SUCCESS) {//判断是否打开,打开继续执行
DWORD maxValueDataLen = 0;//数据类型初始化
lRet = RegQueryInfoKey(hkey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueDataLen/*获取数据字符串长度*/, NULL, NULL);
if (lRet == ERROR_SUCCESS) {//再次判断
DWORD dwType = 0;//默认天0
BYTE* lpData = new BYTE[maxValueDataLen + 1];//获取长度+1防止有空格
ZeroMemory(lpData, maxValueDataLen + 1);
lRet = RegQueryValueEx(hkey, NULL, NULL, &dwType, lpData, &maxValueDataLen/*长度*/);
CString strValue((LPCTSTR)lpData, maxValueDataLen); // 使用 CString 构造函数进行转换
/*MessageBox(NULL, strValue, _T("Value"), MB_OK);*/
delete[] lpData;
int found = strValue.ReverseFind('\\'); // 找到最后一个反斜杠的位置
if (found != -1) {
m_jing_path = strValue.Left(found + 1); // 将路径赋值给m_jing_path
m_jing_path.Replace(L"\\", L"\\"); // 将单个反斜杠替换为双反斜杠
}
}
RegCloseKey(hkey);//关闭注册表
}
}
-------------------------------------------------------------------------------------------------
.cpp 声明与使用
int main() {
m_lu ma;
ma.m_jing(); // 这里会从注册表中获取路径并赋值给 m_jing_path
CString result = ma.data(); // 获取 m_jing_path 的值
result;
return 0;
}