tl;dr - Cannot capture output from commands that use RPC to a remote server using any programming language I try, even though these commands output back to the command prompt when typed manually
I've got a function that uses fpipe to issue the netsh command to display stats from our DHCP server. I do this same fpipe buffer-reading technique elsewhere in my program and it works fine, but here I'm getting no output at all:
EDIT: I'm simplifying my code to a more proof-of-concept type snippet. It's actually an all-encompassed .cpp source file that still replicates the issue.
#include <iostream>
#include <string>
#include <stdio.h> // for _popen() and _pclose()
using namespace std;
int main()
{
char buff[512];
buff[0]=0;
string cmd="netsh dhcp server 192.168.200.15 scope 192.168.200.0 show clients 1";
FILE *fpipe = _popen(cmd.c_str(),"r");
if(fpipe==NULL) cout<<"Failed to open"<<endl; else cout<<"Opened pipe successfully";
while(fgets(buff,sizeof(buff),fpipe)!=NULL) cout<<buff<<endl;
_pclose(fpipe);
}
The only output I'm getting is:
Opened pipe successfully
But if I copy/paste that command into another command prompt window, it runs fine and spits out many lines immediately.
So where does it go? Why does nothing ever go into my buffer?
Also, changing the "cmd" string from the netsh to the "dir" command will successfully output dir's output.
EDIT: Even system(cmd.c_str());
gives me no output!
EDIT: Curiously, cmd="netsh dump";
actually outputs fine, but still cmd="netsh dhcp server...
does not. Something weird with the way CMD routes netsh text or... something? Outside of my comfort zone here.
EDIT: This is obviously an issue outside of C++ because when I'm in a Python 2.7 shell (this is Windows 7 64-bit, by the way), I try this:
>>> import subprocess
>>> subprocess.call('dir', shell=True)
Volume in drive C has no label.
Volume Serial Number is 2E2B-B34F
Directory of C:\MyDir
09/30/2013 01:24 PM <DIR> .
09/30/2013 01:24 PM <DIR> ..
09/20/2013 10:11 AM 45 test.txt
1 File(s) 45 bytes
2 Dir(s) 162,260,246,528 bytes free
0
>>> subprocess.call('netsh dhcp server 192.168.200.15 scope 192.168.200.0 show clients 1', shell=True)
0
>>>