Hello i was try to run execute windows command using _popen and its work fine in vc++ in console.
while same code i do in c++ builder vcl form popen return null pointer ?
vc++
std::string exec(const char* cmd) {
char buffer[128];
std::string result = "";
FILE* pipe = _popen(cmd, "r");
if (!pipe) throw std::runtime_error("popen() failed!");
try {
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
}
catch (...) {
_pclose(pipe);
throw;
}
_pclose(pipe);
return result;
}
c++ builder same as above
void __fastcall TForm2::btn_adb_readInfoClick(TObject *Sender) {
const char* cmd = "D:\\adb\\adb.exe devices";
char buffer[128];
std::string result = "";
FILE* pipe = _popen(cmd, "r"); // always null
char* er = strerror(errno);
try
{
if (!pipe) throw std::runtime_error("popen() failed!");
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
}
catch (std::exception &ex) {
_pclose(pipe);
throw;
}
_pclose(pipe);
}