我有一個簡單的程序,讀取包含用戶提供的路徑的基本配置文件。我正在嘗試使用該路徑來調用系統來執行駐留在所提供位置的.bat文件。我能夠將存儲的路徑直接傳遞到系統中,並嘗試運行.bat文件,但它需要嘗試以管理員身份運行它。我遇到以下帖子:How to call system() in an opened administrator program and gives it the same privileges?調用system()(正如管理員)從特定位置執行.bat的正確路徑語法?
我正在建立字符串,如上面的帖子所示,但是當我嘗試傳遞這個新的字符串到系統,它告訴我「系統找不到指定的文件」。這是我構建我傳入系統的字符串(很可能是錯誤的)。
std::string adminFilePath = "runas /user:<admin-user> \"";
adminFilePath.append(configFileSettings.path.c_str()); //Append the path of the file that we got from the config file.
adminFilePath.append("\"");
system(adminFilePath.c_str());
我的假設是,我應該嘗試建立代表什麼,我會輸入正確的進入cmd窗口來執行.bat基本字符串,但顯然我錯了地方。
運行的RunAs如果打印出來'adminFilePath'它看起來有效嗎?如果您嘗試手動運行,會發生什麼情況? – NathanOliver
當我打印'adminFilePath'它看起來像我期待它給... '的RunAs /用戶:「C:\ WINDOWS \ batFile.bat」' 的路徑看起來與打印我是如果我調用'system(configFileSettings.path.c_str());'它執行.bat就好了。試圖調用'system(adminFilePath.c_str());'結果在「系統找不到指定的文件」 –
WannabeCoder
如果你只是硬編碼並使用'system(R「(runas/user:」C: \的Windows \ batFile.bat 「)」);'?請注意這是一個C++ 11功能。 –
NathanOliver