1

我正在添加我的程序以启动:

TCHAR szPath[MAX_PATH];
GetModuleFileName(NULL,szPath,MAX_PATH);
HKEY newValue;
RegOpenKey(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&newValue);
RegSetValueEx(newValue,"myprogram",0,REG_SZ,(LPBYTE)szPath,sizeof(szPath));
RegCloseKey(newValue);
return 0;

我想添加一个检查键是否不存在才创建它。我的代码有些奇怪,我检查了注册表中的密钥,我在数据列中看到我的应用程序路径 +“...”(在 .exe 之后),当我双击检查数据时,弹出窗口打开并没关系,只是.exe不是.exe...

谢谢你的帮助:)

4

4 回答 4

1

您可以检查注册表功能输出....

在这里,我给出了你可以使用它的想法......

bool function()
{ 
    HKEY hKey;
    LPCTSTR subKey;
    LPCTSTR subValue;
    HKEY resKey;
    DWORD dataLen;
    hKey = HKEY_LOCAL_MACHINE;
    subKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";

    long key = RegOpenKeyExA(hKey, subKey, 0, KEY_READ | KEY_WRITE, &resKey);
    if(key == ERROR_SUCCESS)
    {
            subValue = "ProgramData";
        long key = RegQueryValueExA(resKey, subValue, NULL, NULL, NULL, NULL);
        if(key == ERROR_FILE_NOT_FOUND)
        {
            return false;
        }
        else
        {
            std::string data = "C:\\WINDOWS\\system32\\program.exe";
            DWORD dataLen = data.size()+1;

            long key = RegSetValueExA(resKey, subValue, 0, REG_SZ, (const BYTE*)data.c_str(), dataLen);
            if(key == ERROR_SUCCESS)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    else
    {
        return false;
    }
}
于 2013-04-08T07:05:42.643 回答
1

The value you wrote out is MAX_PATH bytes wide, regardless of how long the path really is. Thus you probably have a lot of non-printing characters after the .exe, and that's why you see the "...".

The documentation says the last parameter is the size in bytes of the string, including the null terminator. So we need to know the length of the string (lstrlen(szPath)), we need to account for the null terminator (+ 1), and we need to convert from TCHARs to bytes (sizeof(TCHAR)*).

const DWORD cbData = sizeof(TCHAR) * (lstrlen(szPath) + 1);
RegSetValueEx(newValue, "myprogram", 0, REG_SZ, (LPBYTE)szPath, cbData);

This API is error prone, and should be used very carefully to avoid unintentional truncation or buffer overrun. (The fact that you need those casts to get it to compile should make you very cautious.) Many Windows functions that take pointers to strings want lengths in characters (which may not be bytes) or they figure out the length from the termination. This one doesn't do either of those things.

于 2013-04-09T22:04:00.090 回答
0

您可以使用它RegCreateKeyEx()来创建新密钥或打开现有密钥。

您在其中看到的“...”RegEdit是因为列不够宽——双击列标题的末尾以调整列的大小。

于 2013-04-08T06:15:38.093 回答
0

I suggest what is suggest in the MSDN: You should enumerate the Subkeys/Values in a Key with RegEnumKey(Ex)() or RegEnumValue() and then check wether the key is listed

See http://msdn.microsoft.com/en-us/library/windows/desktop/ms724861%28v=vs.85%29.aspx

and http://msdn.microsoft.com/en-us/library/windows/desktop/ms724256%28v=vs.85%29.aspx for an example.

Hope this helps.

于 2013-04-09T22:04:35.893 回答