Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. redirecting output of CMD with administrator privileges

redirecting output of CMD with administrator privileges

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    camillo87
    wrote on last edited by
    #1

    Hello! I have following problem. I want to create a application, which is using Netsh. I have created following code:

    private: System::Void button1_Click( System::Object ^ sender, System::EventArgs ^ e ) {

    Process ^ mojProces = gcnew Process();
    ProcessStartInfo ^ startInfo = gcnew ProcessStartInfo( "cmd.exe" );
    startInfo->Verb = "runas";
    startInfo->Arguments =( "Administrator /c \\"netsh wlan start hostednetwork\\" " );
    startInfo->UseShellExecute = false;
    startInfo->CreateNoWindow = true;
    startInfo->RedirectStandardOutput = true;
    startInfo->RedirectStandardInput = true;
    info->Text = "Sieć została uruchomiona.";
    mojProces->StartInfo = startInfo;
    mojProces->Start();
    
    StreamReader ^ wynik = mojProces->StandardOutput;
    String ^ wynik\_konsoli = wynik->ReadToEnd();
    wynik\_wysw->Text = wynik\_konsoli;
    mojProces->WaitForExit();
    mojProces->Close();
    

    Now I would like to redirect output of CMD, but there is following problem. When I set UseShellExecute=false, I cannot run CMD with administrator privileges, which is required to use Netsh. But when I set true I cannot redirect output. Could you please help, how can I solve this problem? Thank you in advance!

    Richard Andrew x64R D 2 Replies Last reply
    0
    • C camillo87

      Hello! I have following problem. I want to create a application, which is using Netsh. I have created following code:

      private: System::Void button1_Click( System::Object ^ sender, System::EventArgs ^ e ) {

      Process ^ mojProces = gcnew Process();
      ProcessStartInfo ^ startInfo = gcnew ProcessStartInfo( "cmd.exe" );
      startInfo->Verb = "runas";
      startInfo->Arguments =( "Administrator /c \\"netsh wlan start hostednetwork\\" " );
      startInfo->UseShellExecute = false;
      startInfo->CreateNoWindow = true;
      startInfo->RedirectStandardOutput = true;
      startInfo->RedirectStandardInput = true;
      info->Text = "Sieć została uruchomiona.";
      mojProces->StartInfo = startInfo;
      mojProces->Start();
      
      StreamReader ^ wynik = mojProces->StandardOutput;
      String ^ wynik\_konsoli = wynik->ReadToEnd();
      wynik\_wysw->Text = wynik\_konsoli;
      mojProces->WaitForExit();
      mojProces->Close();
      

      Now I would like to redirect output of CMD, but there is following problem. When I set UseShellExecute=false, I cannot run CMD with administrator privileges, which is required to use Netsh. But when I set true I cannot redirect output. Could you please help, how can I solve this problem? Thank you in advance!

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      Does this help? CreateProcessAsUser[^] CreateProcessWithLogonW[^]

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      • C camillo87

        Hello! I have following problem. I want to create a application, which is using Netsh. I have created following code:

        private: System::Void button1_Click( System::Object ^ sender, System::EventArgs ^ e ) {

        Process ^ mojProces = gcnew Process();
        ProcessStartInfo ^ startInfo = gcnew ProcessStartInfo( "cmd.exe" );
        startInfo->Verb = "runas";
        startInfo->Arguments =( "Administrator /c \\"netsh wlan start hostednetwork\\" " );
        startInfo->UseShellExecute = false;
        startInfo->CreateNoWindow = true;
        startInfo->RedirectStandardOutput = true;
        startInfo->RedirectStandardInput = true;
        info->Text = "Sieć została uruchomiona.";
        mojProces->StartInfo = startInfo;
        mojProces->Start();
        
        StreamReader ^ wynik = mojProces->StandardOutput;
        String ^ wynik\_konsoli = wynik->ReadToEnd();
        wynik\_wysw->Text = wynik\_konsoli;
        mojProces->WaitForExit();
        mojProces->Close();
        

        Now I would like to redirect output of CMD, but there is following problem. When I set UseShellExecute=false, I cannot run CMD with administrator privileges, which is required to use Netsh. But when I set true I cannot redirect output. Could you please help, how can I solve this problem? Thank you in advance!

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        camillo87 wrote:

        Could you please help, how can I solve this problem?

        You might also try this forum.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups