我試圖創建一個型別為REG_SZ且數值超過4個字符的注冊表鍵。
但是我想不出正確的方法來傳遞資料作為引數。
#include <windows.h>
#include <stdio.h>
HKEY OpenKey(HKEY hRootKey, wchar_t* strKey)
{
HKEY hKey。
LONG nError = RegOpenKeyEx(hRootKey, strKey, 0, KEY_ALL_ACCESS, &hKey)。
if (nError == ERROR_FILE_NOT_FOUND)
{
printf("Debug: 創建注冊表鍵
")。)
nError = RegCreateKeyEx(hRootKey, strKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, & hKey, NULL)。
}
if (nError) {
printf("錯誤。無法找到或創建
")。)
}
return hKey。
}
void SetValSZ(HKEY hKey, LPCTSTR lpValue, LPCTSTR data)。
{
LONG nError = RegSetValueEx(hKey, lpValue, 0, REG_SZ, (const BYTE*) data, sizeof(data));
if (nError)
printf("錯誤。無法設定注冊表的值
")。)
}
int main()
{
HKEY hKey = OpenKey(HKEY_LOCAL_MACHINE, L "SOFTWARE/PoliciesMicrosoftFVE")。
const wchar_t data[] = L "AAAABBBBCCCC"/span>;
SetValSZ(hKey, L "RecoveryKeyMessage"/span>, data)。
RegCloseKey(hKey)。
return 0;
當我運行它時,只有前4個字符被保存。 我肯定有一個型別錯誤......
你有什么想法嗎?
你有什么辦法可以幫助我解決這個問題嗎?
謝謝你。
PS:我希望我的英語很清楚,如果不清楚的話,請隨時問我。
uj5u.com熱心網友回復:
void SetValSZ(HKEY hKey, LPCTSTR lpValue, LPCTSTR data)
{
RegSetValueEx(hKey, lpValue, 0, REG_SZ, (const BYTE*) data, sizeof(data))。
data被作為一個指標傳遞給一個函式,所以它的實際大小將被丟失。sizeof(data)被翻譯成指標的大小,在這種情況下是4位元組。
你必須把大小作為一個引數傳入:
你必須把大小作為一個引數傳入。
int len = sizeof(data)。
SetValSZ(..., data, len)。
或者更好的是,使用wcslen()來計算字串的長度,然后乘以sizeof(wchar_t)。
由于你使用了那些T宏,這個函式可以被寫成:
RegSetValueEx(hKey,lpValue,0, REG_SZ, (const BYTE*)data,sizeof(TCHAR)*(lstrlen(data) 1)) 。
編輯:大小還應該包括結束的空字符
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/320272.html
標籤:
上一篇:使用pandas.read_html()將多個表格的內容添加到csv檔案中失敗。
下一篇:多執行緒和等待事件的問題
