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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. WinMain and argv/argc

WinMain and argv/argc

Scheduled Pinned Locked Moved C / C++ / MFC
helpdebuggingjson
5 Posts 3 Posters 0 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.
  • D Offline
    D Offline
    dellthinker
    wrote on last edited by
    #1

    Hi all, im trying to make an application that requires me to use the API WinMain() and it just so happens i need to use argv and argc. Problem is that WinMain takes 4 arguments in it and doesnt have room for argc/argv[]. So i looked up the issue and as it turns out MS didnt allow the usage for whatever reason (which really isnt important to me because i need to make it work.) So i figured if i cant put it in the calling function then make it apart of a if statement. So this is what i did. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){ string file2="C:\\Debug\\test.exe"; if(__argv[0] == file2){ ofstream file; file.open("test.txt"); file << "Sponcer brought to you by - Subway! Eat Fresh! :) " << endl; file.close(); system(buffer); exit(1); } else{ Sleep(1000); //cout << "Hello World " << endl; exit(1); } return 0; } And it works. But my problem is that i dont want to have to specify the name of the program and file path etc etc. Thats what argv[0] is for. So what im trying to do is this.... Use argv within the WinMain function so that i can print out file/path info. That way i can work my way around other problems. Thanx in advance!

    D C 2 Replies Last reply
    0
    • D dellthinker

      Hi all, im trying to make an application that requires me to use the API WinMain() and it just so happens i need to use argv and argc. Problem is that WinMain takes 4 arguments in it and doesnt have room for argc/argv[]. So i looked up the issue and as it turns out MS didnt allow the usage for whatever reason (which really isnt important to me because i need to make it work.) So i figured if i cant put it in the calling function then make it apart of a if statement. So this is what i did. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){ string file2="C:\\Debug\\test.exe"; if(__argv[0] == file2){ ofstream file; file.open("test.txt"); file << "Sponcer brought to you by - Subway! Eat Fresh! :) " << endl; file.close(); system(buffer); exit(1); } else{ Sleep(1000); //cout << "Hello World " << endl; exit(1); } return 0; } And it works. But my problem is that i dont want to have to specify the name of the program and file path etc etc. Thats what argv[0] is for. So what im trying to do is this.... Use argv within the WinMain function so that i can print out file/path info. That way i can work my way around other problems. Thanx in advance!

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

      I don't quite understand your question, but have you taken a look at WinMain()'s third parameter, lpCmdLine?


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      D 1 Reply Last reply
      0
      • D David Crow

        I don't quite understand your question, but have you taken a look at WinMain()'s third parameter, lpCmdLine?


        "A good athlete is the result of a good and worthy opponent." - David Crow

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        D Offline
        D Offline
        dellthinker
        wrote on last edited by
        #3

        Im trying to get the current file name from argv using WinMain(). But since i cant put argv[] or argc in the function arguments, i have to use another method. Thats what im saying

        D 1 Reply Last reply
        0
        • D dellthinker

          Im trying to get the current file name from argv using WinMain(). But since i cant put argv[] or argc in the function arguments, i have to use another method. Thats what im saying

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

          dellthinker wrote:

          Im trying to get the current file name from...WinMain().

          So how about GetModuleFileName()?

          dellthinker wrote:

          But since i cant put argv[] or argc in the function arguments, i have to use another method.

          And that way is fine. There's nothing wrong with using __argv[0].


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          1 Reply Last reply
          0
          • D dellthinker

            Hi all, im trying to make an application that requires me to use the API WinMain() and it just so happens i need to use argv and argc. Problem is that WinMain takes 4 arguments in it and doesnt have room for argc/argv[]. So i looked up the issue and as it turns out MS didnt allow the usage for whatever reason (which really isnt important to me because i need to make it work.) So i figured if i cant put it in the calling function then make it apart of a if statement. So this is what i did. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){ string file2="C:\\Debug\\test.exe"; if(__argv[0] == file2){ ofstream file; file.open("test.txt"); file << "Sponcer brought to you by - Subway! Eat Fresh! :) " << endl; file.close(); system(buffer); exit(1); } else{ Sleep(1000); //cout << "Hello World " << endl; exit(1); } return 0; } And it works. But my problem is that i dont want to have to specify the name of the program and file path etc etc. Thats what argv[0] is for. So what im trying to do is this.... Use argv within the WinMain function so that i can print out file/path info. That way i can work my way around other problems. Thanx in advance!

            C Offline
            C Offline
            carrivick
            wrote on last edited by
            #5

            You need to use GetCommandLine and CommandLineToArgvW like #include #include #include int __cdecl main() { LPWSTR *szArglist; int nArgs; int i; szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs); if( NULL == szArglist ) { wprintf(L"CommandLineToArgvW failed\n"); return 0; } else for( i=0; i

            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