Trying to setup a proxy via a c++ program. Doesn't work












1















I am trying to set up a proxy via a c++ program but it doesn't work.
The program itself works fine but actually, it doesn't set anything.



This is the code:



#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

void newproxy();

int main (){

int ch = 0;

cout << "Select the option:nnt(1)Set a NEW proxynt(2)View the current proxy settingsnt(3)Clear ALL proxy settingsnt(4)Exitnnt";
cin >> ch;
cin.ignore();
switch (ch){

case 1 :
system("cls");
newproxy();
break;

case 2 :
system("cls");
system("netsh winhttp show proxy");
system("pause");
system("cls");
main();
break;

case 3 :
system("cls");
system("netsh winhttp reset proxy");
system("cls");
cout << "ALL proxy settings clearednn";
main();
break;

case 4 :
return 0;
break;

}
}

void newproxy(){

string ip;
string port;
int ch = 0;

cout << "Insert ip: ";
getline (cin, ip);
cout << "Inster port: ";
getline(cin, port);
cout << "nAre those the correct ip and port? " << ip << ":" << port << "nnt(1)Yes (2)No (3)Abortnnt";
cin >> ch;
cin.ignore();
switch (ch){

case 1 :
break;

case 2 :
system("cls");
newproxy();
break;

case 3 :
system("cls");
main();
break;

}
system(("netsh winhttp set proxy proxy-server="+ip+":"+port).c_str());
system("cls");
cout << "Proxy succesfully settednn";
main();
}


When I set a new proxy the program actually sees it and displays it even if I restart the program, but nothing is really done. If I open the browser and search "my IP" it displays the same public IP not like when I set it up in the Windows setting.



Can someone help me?
Is the problem in the code or is it something else?



Thanks.










share|improve this question























  • Unrelated: main and newproxy call main. Don't call main. Only the runtime should call main. If a program calls main you get Undefined Behaviour (UB), and UB can do anything. Sometimes you get what you want. Sometimes you don't. Sometimes you do until suddenly you don't. UB is the absolute worst thing that can happen in a program. Avoid it at all costs. Instead separate the menu code out into its own function and call the menu function.

    – user4581301
    Nov 21 '18 at 0:14













  • @user4581301 I didn't even notice that. Good call.

    – johnathan
    Nov 21 '18 at 0:24











  • system has to support a large number of possible targets from bare-metal embedded systems on up. It isn't guaranteed to give any usable feedback. The commands your program issues using it could be failing hilariously and there is no way to know. Fortunately you're using Windows and I'm pretty sure system on every tool chain I've used targeting Windows returns the program's exit code. Check the return code to make sure the command worked. If you're not getting a valid return code, try using ShellExecute or ShellExecuteEx, Windows system calls that may provide more details.

    – user4581301
    Nov 21 '18 at 0:24


















1















I am trying to set up a proxy via a c++ program but it doesn't work.
The program itself works fine but actually, it doesn't set anything.



This is the code:



#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

void newproxy();

int main (){

int ch = 0;

cout << "Select the option:nnt(1)Set a NEW proxynt(2)View the current proxy settingsnt(3)Clear ALL proxy settingsnt(4)Exitnnt";
cin >> ch;
cin.ignore();
switch (ch){

case 1 :
system("cls");
newproxy();
break;

case 2 :
system("cls");
system("netsh winhttp show proxy");
system("pause");
system("cls");
main();
break;

case 3 :
system("cls");
system("netsh winhttp reset proxy");
system("cls");
cout << "ALL proxy settings clearednn";
main();
break;

case 4 :
return 0;
break;

}
}

void newproxy(){

string ip;
string port;
int ch = 0;

cout << "Insert ip: ";
getline (cin, ip);
cout << "Inster port: ";
getline(cin, port);
cout << "nAre those the correct ip and port? " << ip << ":" << port << "nnt(1)Yes (2)No (3)Abortnnt";
cin >> ch;
cin.ignore();
switch (ch){

case 1 :
break;

case 2 :
system("cls");
newproxy();
break;

case 3 :
system("cls");
main();
break;

}
system(("netsh winhttp set proxy proxy-server="+ip+":"+port).c_str());
system("cls");
cout << "Proxy succesfully settednn";
main();
}


When I set a new proxy the program actually sees it and displays it even if I restart the program, but nothing is really done. If I open the browser and search "my IP" it displays the same public IP not like when I set it up in the Windows setting.



Can someone help me?
Is the problem in the code or is it something else?



Thanks.










share|improve this question























  • Unrelated: main and newproxy call main. Don't call main. Only the runtime should call main. If a program calls main you get Undefined Behaviour (UB), and UB can do anything. Sometimes you get what you want. Sometimes you don't. Sometimes you do until suddenly you don't. UB is the absolute worst thing that can happen in a program. Avoid it at all costs. Instead separate the menu code out into its own function and call the menu function.

    – user4581301
    Nov 21 '18 at 0:14













  • @user4581301 I didn't even notice that. Good call.

    – johnathan
    Nov 21 '18 at 0:24











  • system has to support a large number of possible targets from bare-metal embedded systems on up. It isn't guaranteed to give any usable feedback. The commands your program issues using it could be failing hilariously and there is no way to know. Fortunately you're using Windows and I'm pretty sure system on every tool chain I've used targeting Windows returns the program's exit code. Check the return code to make sure the command worked. If you're not getting a valid return code, try using ShellExecute or ShellExecuteEx, Windows system calls that may provide more details.

    – user4581301
    Nov 21 '18 at 0:24
















1












1








1








I am trying to set up a proxy via a c++ program but it doesn't work.
The program itself works fine but actually, it doesn't set anything.



This is the code:



#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

void newproxy();

int main (){

int ch = 0;

cout << "Select the option:nnt(1)Set a NEW proxynt(2)View the current proxy settingsnt(3)Clear ALL proxy settingsnt(4)Exitnnt";
cin >> ch;
cin.ignore();
switch (ch){

case 1 :
system("cls");
newproxy();
break;

case 2 :
system("cls");
system("netsh winhttp show proxy");
system("pause");
system("cls");
main();
break;

case 3 :
system("cls");
system("netsh winhttp reset proxy");
system("cls");
cout << "ALL proxy settings clearednn";
main();
break;

case 4 :
return 0;
break;

}
}

void newproxy(){

string ip;
string port;
int ch = 0;

cout << "Insert ip: ";
getline (cin, ip);
cout << "Inster port: ";
getline(cin, port);
cout << "nAre those the correct ip and port? " << ip << ":" << port << "nnt(1)Yes (2)No (3)Abortnnt";
cin >> ch;
cin.ignore();
switch (ch){

case 1 :
break;

case 2 :
system("cls");
newproxy();
break;

case 3 :
system("cls");
main();
break;

}
system(("netsh winhttp set proxy proxy-server="+ip+":"+port).c_str());
system("cls");
cout << "Proxy succesfully settednn";
main();
}


When I set a new proxy the program actually sees it and displays it even if I restart the program, but nothing is really done. If I open the browser and search "my IP" it displays the same public IP not like when I set it up in the Windows setting.



Can someone help me?
Is the problem in the code or is it something else?



Thanks.










share|improve this question














I am trying to set up a proxy via a c++ program but it doesn't work.
The program itself works fine but actually, it doesn't set anything.



This is the code:



#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

void newproxy();

int main (){

int ch = 0;

cout << "Select the option:nnt(1)Set a NEW proxynt(2)View the current proxy settingsnt(3)Clear ALL proxy settingsnt(4)Exitnnt";
cin >> ch;
cin.ignore();
switch (ch){

case 1 :
system("cls");
newproxy();
break;

case 2 :
system("cls");
system("netsh winhttp show proxy");
system("pause");
system("cls");
main();
break;

case 3 :
system("cls");
system("netsh winhttp reset proxy");
system("cls");
cout << "ALL proxy settings clearednn";
main();
break;

case 4 :
return 0;
break;

}
}

void newproxy(){

string ip;
string port;
int ch = 0;

cout << "Insert ip: ";
getline (cin, ip);
cout << "Inster port: ";
getline(cin, port);
cout << "nAre those the correct ip and port? " << ip << ":" << port << "nnt(1)Yes (2)No (3)Abortnnt";
cin >> ch;
cin.ignore();
switch (ch){

case 1 :
break;

case 2 :
system("cls");
newproxy();
break;

case 3 :
system("cls");
main();
break;

}
system(("netsh winhttp set proxy proxy-server="+ip+":"+port).c_str());
system("cls");
cout << "Proxy succesfully settednn";
main();
}


When I set a new proxy the program actually sees it and displays it even if I restart the program, but nothing is really done. If I open the browser and search "my IP" it displays the same public IP not like when I set it up in the Windows setting.



Can someone help me?
Is the problem in the code or is it something else?



Thanks.







c++ windows proxy netsh






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 23:18









underAlexunderAlex

61




61













  • Unrelated: main and newproxy call main. Don't call main. Only the runtime should call main. If a program calls main you get Undefined Behaviour (UB), and UB can do anything. Sometimes you get what you want. Sometimes you don't. Sometimes you do until suddenly you don't. UB is the absolute worst thing that can happen in a program. Avoid it at all costs. Instead separate the menu code out into its own function and call the menu function.

    – user4581301
    Nov 21 '18 at 0:14













  • @user4581301 I didn't even notice that. Good call.

    – johnathan
    Nov 21 '18 at 0:24











  • system has to support a large number of possible targets from bare-metal embedded systems on up. It isn't guaranteed to give any usable feedback. The commands your program issues using it could be failing hilariously and there is no way to know. Fortunately you're using Windows and I'm pretty sure system on every tool chain I've used targeting Windows returns the program's exit code. Check the return code to make sure the command worked. If you're not getting a valid return code, try using ShellExecute or ShellExecuteEx, Windows system calls that may provide more details.

    – user4581301
    Nov 21 '18 at 0:24





















  • Unrelated: main and newproxy call main. Don't call main. Only the runtime should call main. If a program calls main you get Undefined Behaviour (UB), and UB can do anything. Sometimes you get what you want. Sometimes you don't. Sometimes you do until suddenly you don't. UB is the absolute worst thing that can happen in a program. Avoid it at all costs. Instead separate the menu code out into its own function and call the menu function.

    – user4581301
    Nov 21 '18 at 0:14













  • @user4581301 I didn't even notice that. Good call.

    – johnathan
    Nov 21 '18 at 0:24











  • system has to support a large number of possible targets from bare-metal embedded systems on up. It isn't guaranteed to give any usable feedback. The commands your program issues using it could be failing hilariously and there is no way to know. Fortunately you're using Windows and I'm pretty sure system on every tool chain I've used targeting Windows returns the program's exit code. Check the return code to make sure the command worked. If you're not getting a valid return code, try using ShellExecute or ShellExecuteEx, Windows system calls that may provide more details.

    – user4581301
    Nov 21 '18 at 0:24



















Unrelated: main and newproxy call main. Don't call main. Only the runtime should call main. If a program calls main you get Undefined Behaviour (UB), and UB can do anything. Sometimes you get what you want. Sometimes you don't. Sometimes you do until suddenly you don't. UB is the absolute worst thing that can happen in a program. Avoid it at all costs. Instead separate the menu code out into its own function and call the menu function.

– user4581301
Nov 21 '18 at 0:14







Unrelated: main and newproxy call main. Don't call main. Only the runtime should call main. If a program calls main you get Undefined Behaviour (UB), and UB can do anything. Sometimes you get what you want. Sometimes you don't. Sometimes you do until suddenly you don't. UB is the absolute worst thing that can happen in a program. Avoid it at all costs. Instead separate the menu code out into its own function and call the menu function.

– user4581301
Nov 21 '18 at 0:14















@user4581301 I didn't even notice that. Good call.

– johnathan
Nov 21 '18 at 0:24





@user4581301 I didn't even notice that. Good call.

– johnathan
Nov 21 '18 at 0:24













system has to support a large number of possible targets from bare-metal embedded systems on up. It isn't guaranteed to give any usable feedback. The commands your program issues using it could be failing hilariously and there is no way to know. Fortunately you're using Windows and I'm pretty sure system on every tool chain I've used targeting Windows returns the program's exit code. Check the return code to make sure the command worked. If you're not getting a valid return code, try using ShellExecute or ShellExecuteEx, Windows system calls that may provide more details.

– user4581301
Nov 21 '18 at 0:24







system has to support a large number of possible targets from bare-metal embedded systems on up. It isn't guaranteed to give any usable feedback. The commands your program issues using it could be failing hilariously and there is no way to know. Fortunately you're using Windows and I'm pretty sure system on every tool chain I've used targeting Windows returns the program's exit code. Check the return code to make sure the command worked. If you're not getting a valid return code, try using ShellExecute or ShellExecuteEx, Windows system calls that may provide more details.

– user4581301
Nov 21 '18 at 0:24














1 Answer
1






active

oldest

votes


















0














 system((std::string("netsh winhttp set proxy ") + ip +":" + port).c_str());


You almost had it.



To use the Netsh.exe tool to configure a proxy server, follow these steps:



Click Start, click Run, type cmd, and then click OK.



At the command prompt, type netsh winhttp set proxy proxyservername:portnumber, and then press ENTER.



Good luck.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403082%2ftrying-to-setup-a-proxy-via-a-c-program-doesnt-work%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














     system((std::string("netsh winhttp set proxy ") + ip +":" + port).c_str());


    You almost had it.



    To use the Netsh.exe tool to configure a proxy server, follow these steps:



    Click Start, click Run, type cmd, and then click OK.



    At the command prompt, type netsh winhttp set proxy proxyservername:portnumber, and then press ENTER.



    Good luck.






    share|improve this answer






























      0














       system((std::string("netsh winhttp set proxy ") + ip +":" + port).c_str());


      You almost had it.



      To use the Netsh.exe tool to configure a proxy server, follow these steps:



      Click Start, click Run, type cmd, and then click OK.



      At the command prompt, type netsh winhttp set proxy proxyservername:portnumber, and then press ENTER.



      Good luck.






      share|improve this answer




























        0












        0








        0







         system((std::string("netsh winhttp set proxy ") + ip +":" + port).c_str());


        You almost had it.



        To use the Netsh.exe tool to configure a proxy server, follow these steps:



        Click Start, click Run, type cmd, and then click OK.



        At the command prompt, type netsh winhttp set proxy proxyservername:portnumber, and then press ENTER.



        Good luck.






        share|improve this answer















         system((std::string("netsh winhttp set proxy ") + ip +":" + port).c_str());


        You almost had it.



        To use the Netsh.exe tool to configure a proxy server, follow these steps:



        Click Start, click Run, type cmd, and then click OK.



        At the command prompt, type netsh winhttp set proxy proxyservername:portnumber, and then press ENTER.



        Good luck.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 30 '18 at 17:25

























        answered Nov 21 '18 at 0:21









        johnathanjohnathan

        2,158819




        2,158819






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403082%2ftrying-to-setup-a-proxy-via-a-c-program-doesnt-work%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            How to fix TextFormField cause rebuild widget in Flutter