如何从服务启动外部程序
How to start an external program from service
我正在尝试从服务程序调用二进制文件。二进制文件的路径是从注册表项中提取的。当我将缓冲区分配给字符串时,路径仅包含"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"
如果你得到字母C,这意味着你的代码部分工作。但是,这:
for(int i=0;buf[i];i++)
path[i]=buf[i];
没有意义,至少对我来说没有意义,因为我什至无法想象这个循环将如何工作(鉴于你得到字母 C 可能意味着它运行一次)。一个简单的解决方案是使用它:
path = std::string(buf);
如果我错了,请在调试时在返回之前检查 buf 值。
相关文章:
- 如何运行外部程序,向其传递内存地址以读取/写入?
- C++外部程序的输入和输出管道
- 如何调用外部程序并在 C/C++ 中获取多个返回值
- GetAsyncKeyState() - 外部程序会干扰它
- 运行外部程序并获得返回的整数
- 在 c++ 中捕获外部程序的退出代码
- 从外部程序中捕获标准和标准输出 C++.
- 如何通过C 在Mac OS中运行外部程序
- 一个唱片播放器如何使用外部程序在CS:GO中杀人
- Qt:如何通过外部程序打开文件,"open with..."对话框
- C++外部程序集:我的代码中的错误在哪里
- 通过Qt崩溃运行外部程序
- Cpp 中的插件 - 执行外部程序函数
- Qt 执行外部程序
- 停止来自 C++ 代码的外部程序
- 视觉 从 C++ 应用程序执行的外部程序中的 CPU 负载有限
- 如何从服务启动外部程序
- C++暂停外部程序
- 几次调用后,外部程序的执行失败
- 如何在Windows中不显示cmd窗口的情况下运行带参数的外部程序