程序以递归方式导航给定文件夹,为文件夹创建注册表,但在执行RegSetValueEx函数后,注册表中没有任何事情发生,我应该更改什么?我的意思是该程序工作完美但注册表中RegSetValuesEx函数没有任何变化,只是RegCreateKeys运行良好。
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#include <iostream>
using namespace std;
#define DEFAULT_KEY_DIR "HKEY_CURRENT_USER"
#define DEFAULT_KEY_SUBDIR "Software\\CSSO"
#define DEFAULT_DIRECTORY "D:\\Example"
char xPath[2048] = "Software\\CSSO";
bool ListDirectoryContents(const char *sDir)
{
WIN32_FIND_DATA fdFile;
HANDLE hFind = NULL;
LARGE_INTEGER filesize;
DWORD return_value;
char sPath[2048];
HKEY hKey;
sprintf(sPath, "%s\\*", sDir);
if ((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE)
{
printf("Path not found: [%s]\n", sDir);
return false;
}
do
{
if (strcmp(fdFile.cFileName, ".") != 0
&& strcmp(fdFile.cFileName, "..") != 0)
{
sprintf(sPath, "%s\\%s", sDir, fdFile.cFileName);
if (fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
{
printf("%s\n", sPath);
memset(&xPath[0], 0, sizeof(xPath));
strcpy(xPath, DEFAULT_KEY_SUBDIR);
int j = strlen(DEFAULT_KEY_SUBDIR);
for (int i = strlen(DEFAULT_DIRECTORY); i < strlen(sPath); i++)
{
xPath[j] = sPath[i];
j++;
}
RegCreateKeyEx(
HKEY_CURRENT_USER,
xPath,
0, 0, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, 0, &hKey, 0
);
ListDirectoryContents(sPath); //Recursion, I love it!
}
else {
filesize.LowPart = fdFile.nFileSizeLow;
filesize.HighPart = fdFile.nFileSizeHigh;
_tprintf(TEXT("%s %ld bytes\n"),sPath, filesize.QuadPart);
return_value = RegSetValueEx(
hKey,
fdFile.cFileName, 0,
REG_DWORD,
(BYTE*)filesize.QuadPart,
strlen(sPath)
);
}
}
} while (FindNextFile(hFind, &fdFile));
FindClose(hFind);
return true;
}
int main()
{
ListDirectoryContents("D:\\Example");
system("PAUSE");
return 0;
}
答案 0 :(得分:2)
上面的代码正在经历递归循环,它向注册表中添加了大量垃圾。不建议这样做。
return_value = RegSetValueEx(
hKey,
fdFile.cFileName, 0,
REG_DWORD,
(BYTE*)filesize.QuadPart,
strlen(sPath))
(BYTE*)filesize.QuadPart
错了。它类似于写作:
BYTE* ptr = (BYTE*)123;
这会创建一个指向内存地址123的指针,我们不允许这样做。你的意思是写(BYTE*)&filesize.QuadPart
。
假设您要编写文件大小,请使用REG_QWORD
代替REG_DWORD
。使用sizeof(filesize.QuadPart)
代替strlen(path)
。
if (ERROR_SUCCESS != RegSetValueEx(hKey, fdFile.cFileName, 0, REG_QWORD,
(BYTE*)&filesize.QuadPart, sizeof(filesize.QuadPart))
{
printf("RegSetValueEx error\n");
}
但是,您应该使用注册表仅添加初始化数据。如果您有大量数据,则保存到文件,或者只将其保存在内存中。
答案 1 :(得分:-2)
我弄清楚我做错了什么,谢谢你们的答案,我会发布最终的代码,也许会帮助别人。
#include <iostream>
#include <sstream>
#include <string>
#include <typeinfo>
using namespace std;
//Class Template Stack declaration
template <class T>
class TemplateStack {
public:
typedef T type;
TemplateStack()
{
max_size_ = 50;
TopOfStack = 0;
data_ = new T[max_size_];
} //Default Constructor taking no parameters
void push(T element)
{
if (TopOfStack == max_size_)
throw string("Stack's underlying storage is overflow");
TopOfStack++;
data_[TopOfStack] = element;
}
void pop() {
if (TopOfStack == -1)
throw string("Stack is empty");
TopOfStack--;
}
T top() {
if (TopOfStack == -1)
throw string("Stack is empty");
return data_[TopOfStack];
}
private:
int TopOfStack; //Generic data type for the top element of stack
size_t max_size_;
T* data_;
};
//Function to Evauluate the Postfix notation expresion
int PostfixEvaulation(string input){
//string input;
int operand1, operand2, result,number;
TemplateStack<char>operation;
stringstream temp;
int i=0;
while (i < input.length())
{
if (isdigit(input[i]))
{
operation.push(input[i]);
}
else
{
operand2 = operation.top();
temp << operation.top();
operation.pop();
operand1 = operation.top();
temp << operation.top();
operation.pop();
switch(operand1,operand2)
{
case '+': result=operand1 + operand2;
break;
case '-': result=operand1 - operand2;
break;
case '*': result=operand1 * operand2;
break;
case '/': result=operand1 / operand2;
break;
}
operation.push(result);
}
i++;
}
cout << "The result is: "<<temp.str()<<endl;
//cin.ignore(numeric_limits<streamsize>::max(), '\n');
return 0;
}
//Function to return precedence of operators
int prec(char c)
{
if(c == '^')
return 3;
else if(c == '*' || c == '/')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}
//Function: Convert Infix to Postfix
void infixToPostfix(string s)
{
TemplateStack<char> st;
st.push('N');
int l = s.length();
string ns;
for (int i = 0; i < l; i++) {
// If the scanned character is an operand, add it to output string.
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) {
ns += s[i];
}
// If the scanned character is an ‘(‘, push it to the stack.
else if (s[i] == '(') {
st.push('(');
}
// If the scanned character is an ‘)’, pop and to output string from the stack
// until an ‘(‘ is encountered.
else if (s[i] == ')') {
while (st.top() != 'N' && st.top() != '(') {
char c = st.top();
st.pop();
ns += c;
}
if (st.top() == '(') {
char c = st.top();
st.pop();
}
}
//If an operator is scanned
else {
while (st.top() != 'N' && prec(s[i]) <= prec(st.top())) {
char c = st.top();
st.pop();
ns += c;
}
st.push(s[i]);
}
}
//Pop all the remaining elements from the stack
while (st.top() != 'N') {
char c = st.top();
st.pop();
ns += c;
}
cout << "Here is the ns variable: "<< ns << endl;
PostfixEvaulation(ns);//Call the PostFixEvaluationFunction with the ns (postfix notation) variab
}
//Function: User inputs Expression
void GetInput(){
string input;
cout << "Enter Infix Expression: ";
getline(cin, input);
infixToPostfix(input);
}
int main()
{
GetInput();
}