0

我正在尝试从服务程序调用二进制文件。二进制文件的路径是从注册表项中获取的。当我将缓冲区分配给字符串时,路径仅包含“C”。我在哪里犯了错误。我正在使用visual studio 2010这里是代码:

std::string path;
getPathFromKey(path);
path = path+"\\myapp.exe";
argument = "start \"\" \""+path+"\"";
system(argument.c_str());

/ *检索密钥* /

void getPathFromKey(std::string &path)
{
HKEY hKey = 0;
char buf[512];
DWORD dwType = 0;
DWORD dwBufSize = sizeof(buf);


if( RegOpenKey(HKEY_LOCAL_MACHINE,L"SOFTWARE\\MYAPP\\MyApp",&hKey) == ERROR_SUCCESS)
{
    dwType = REG_SZ;
    if( RegQueryValueEx(hKey,L"InstallPath",0, &dwType, (LPBYTE)buf, &dwBufSize) == ERROR_SUCCESS)
    {
        for(int i=0;buf[i];i++)
            path[i]=buf[i];
        return;
    }
    else
    {
        RegCloseKey(hKey);

        return;
    }

}

else if( RegOpenKey(HKEY_LOCAL_MACHINE,L"Software\\Wow6432Node\\MYAPP\\MyApp",&hKey) == ERROR_SUCCESS)
{
    dwType = REG_SZ;
    if( RegQueryValueEx(hKey,L"InstallPath",0, &dwType, (BYTE*)buf, &dwBufSize) == ERROR_SUCCESS)
    {
        for(int i=0;buf[i];i++)
            path[i]=buf[i];
        return;
    }
    else
    {
        RegCloseKey(hKey);
        return;
    }

}

}

我收到以下错误: Windows cannot find "C"

4

1 回答 1

0

如果您收到字母 C,则表示您的代码部分工作。然而,这:

for(int i=0;buf[i];i++)
    path[i]=buf[i];

没有意义,至少对我来说没有,因为我什至无法想象这个循环是如何工作的(假设你得到字母 C 可能意味着它运行一次)。一个简单的解决方案是使用这个:

path = std::string(buf);

如果我错了,那么在返回之前检查 buf 值,同时调试。

于 2013-09-16T22:27:46.453 回答