c++ win32 command line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ramilol
    New Member
    • Aug 2010
    • 14

    c++ win32 command line

    im using Process::start( "test.exe","tur e");
    it start a process called test.exe and send argument ture but on text.exe i don't know how to get the argument that have been sent,test.exe is win32 window. all i need help with how to get the ture in test.exe?
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    follow the example if it is a console application

    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
     int i;
     for(i=0;i<argc;i++)
     {
      wprintf(L"%s",argv[i]);
     }
     return 0;
    }
    If this is an window application

    Code:
    #include "stdafx.h"
    #include "argument.h"
    
    
    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
        MessageBox(NULL,lpCmdLine,L"Command Line argument",MB_OK);
    	return 1;
    }

    Comment

    • ramilol
      New Member
      • Aug 2010
      • 14

      #3
      MessageBox(NULL ,lpCmdLine,L"Co mmand Line argument",MB_OK );
      will this have the ture value?

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        will this have the ture value?
        why dont you try it and see for yourself?

        Comment

        • ramilol
          New Member
          • Aug 2010
          • 14

          #5
          i would if i can go on my laptop my mom is using it but i will and i tell you the result thanks very much for your time

          Comment

          • ramilol
            New Member
            • Aug 2010
            • 14

            #6
            i tried it and it worked just fine thanks very much

            Comment

            • ramilol
              New Member
              • Aug 2010
              • 14

              #7
              can you help me with this one please. ok since the LPTSTR lpCmdLine is declared in WinMain i used this to create var to store ipcmdline value, LPTSTR args = lpCmdLine;i put it in WinMain but when i try to call it from a different function it says undifined args i know why it is saying that but if i decalre the varabile in the other function ill get a different error. so is there way i can call the varable(args) in winMain funtion ?

              Comment

              • johny10151981
                Top Contributor
                • Jan 2010
                • 1059

                #8
                I have no clue what are you talking about. Can you please post code?

                Comment

                • ramilol
                  New Member
                  • Aug 2010
                  • 14

                  #9
                  ok
                  Code:
                  // Take and Run.cpp : Defines the entry point for the application.
                  //
                  
                  #include "stdafx.h"
                  #include "Take and Run.h"
                  #include <string>
                  #include <objidl.h>
                  #include <gdiplus.h>
                  using namespace Gdiplus;
                  #pragma comment (lib,"Gdiplus.lib")
                  
                  
                  #define MAX_LOADSTRING 100
                  
                  // Global Variables:
                  HINSTANCE hInst;								// current instance
                  TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
                  TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name
                  
                  // Forward declarations of functions included in this code module:
                  ATOM				MyRegisterClass(HINSTANCE hInstance);
                  BOOL				InitInstance(HINSTANCE, int);
                  LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
                  INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
                  
                  VOID onPaint(HDC hdc)
                  {
                     Graphics graphics(hdc);
                     Pen      pen(Color(255, 0, 0, 255));
                     graphics.DrawLine(&pen, 0, 0, 200, 100);
                  }
                  
                  int APIENTRY _tWinMain(HINSTANCE hInstance,
                                       HINSTANCE hPrevInstance,
                                       LPTSTR    lpCmdLine,
                                       int       nCmdShow)
                  {
                  	UNREFERENCED_PARAMETER(hPrevInstance);
                  	UNREFERENCED_PARAMETER(lpCmdLine);
                  
                   	// TODO: Place code here.
                  	// THE VARABILE THAT WILL STORE THE IPCMDLINE VALUE
                  	LPTSTR args = lpCmdLine;
                  	
                  	MSG msg;
                  	HACCEL hAccelTable;
                  
                  	GdiplusStartupInput gdiplusStartupInput;
                      ULONG_PTR           gdiplusToken;
                     
                      // Initialize GDI+.
                      GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
                  
                  	// Initialize global strings
                  	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
                  	LoadString(hInstance, IDC_TAKEANDRUN, szWindowClass, MAX_LOADSTRING);
                  	MyRegisterClass(hInstance);
                  
                  	// Perform application initialization:
                  	if (!InitInstance (hInstance, nCmdShow))
                  	{
                  		return FALSE;
                  	}
                  
                  	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TAKEANDRUN));
                  
                  	// Main message loop:
                  	while (GetMessage(&msg, NULL, 0, 0))
                  	{
                  		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
                  		{
                  			TranslateMessage(&msg);
                  			DispatchMessage(&msg);
                  		}
                  	}
                  	GdiplusShutdown(gdiplusToken);
                  	return (int) msg.wParam;
                  }
                  
                  
                  
                  //
                  //  FUNCTION: MyRegisterClass()
                  //
                  //  PURPOSE: Registers the window class.
                  //
                  //  COMMENTS:
                  //
                  //    This function and its usage are only necessary if you want this code
                  //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
                  //    function that was added to Windows 95. It is important to call this function
                  //    so that the application will get 'well formed' small icons associated
                  //    with it.
                  //
                  ATOM MyRegisterClass(HINSTANCE hInstance)
                  {
                  	WNDCLASSEX wcex;
                  
                  	wcex.cbSize = sizeof(WNDCLASSEX);
                  
                  	wcex.style			= CS_HREDRAW | CS_VREDRAW;
                  	wcex.lpfnWndProc	= WndProc;
                  	wcex.cbClsExtra		= 0;
                  	wcex.cbWndExtra		= 0;
                  	wcex.hInstance		= hInstance;
                  	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TAKEANDRUN));
                  	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
                  	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
                  	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_TAKEANDRUN);
                  	wcex.lpszClassName	= szWindowClass;
                  	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
                  
                  	return RegisterClassEx(&wcex);
                  }
                  
                  //
                  //   FUNCTION: InitInstance(HINSTANCE, int)
                  //
                  //   PURPOSE: Saves instance handle and creates main window
                  //
                  //   COMMENTS:
                  //
                  //        In this function, we save the instance handle in a global variable and
                  //        create and display the main program window.
                  //
                  BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
                  {
                     HWND hWnd;
                  
                     hInst = hInstance; // Store instance handle in our global variable
                  
                     hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
                  
                     if (!hWnd)
                     {
                        return FALSE;
                     }
                  
                     ShowWindow(hWnd, nCmdShow);
                     UpdateWindow(hWnd);
                  
                     return TRUE;
                  }
                  
                  //
                  //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
                  //
                  //  PURPOSE:  Processes messages for the main window.
                  //
                  //  WM_COMMAND	- process the application menu
                  //  WM_PAINT	- Paint the main window
                  //  WM_DESTROY	- post a quit message and return
                  //
                  //
                  LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LPTSTR lpCmdLine)
                  {
                  	int wmId, wmEvent;
                  	PAINTSTRUCT ps;
                  	HDC hdc;
                  
                  	switch (message)
                  	{
                  	case WM_COMMAND:
                  		wmId    = LOWORD(wParam);
                  		wmEvent = HIWORD(wParam);
                  		// Parse the menu selections:
                  		switch (wmId)
                  		{
                  		case IDM_ABOUT:
                  			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                  			break;
                  		case IDM_EXIT:
                  			DestroyWindow(hWnd);
                  			break;
                  		default:
                  			return DefWindowProc(hWnd, message, wParam, lParam);
                  		}
                  		break;
                  	case WM_PAINT:
                  		hdc = BeginPaint(hWnd, &ps);
                  		// HERE I USED THE ARGS VARABILE AND I GET A ERROR THAT SAYS ARGS UNDIFINED IF I MOVE MESSAGBOX(..); TO WINMAIN FUNCTION IT WILL WORK
                  		MessageBox(NULL,args,L"Command Line argument",MB_OK);
                  		EndPaint(hWnd, &ps);
                  		break;
                  	case WM_DESTROY:
                  		PostQuitMessage(0);
                  		break;
                  	default:
                  		return DefWindowProc(hWnd, message, wParam, lParam);
                  	}
                  	return 0;
                  }
                  
                  // Message handler for about box.
                  INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
                  {
                  	UNREFERENCED_PARAMETER(lParam);
                  	switch (message)
                  	{
                  	case WM_INITDIALOG:
                  		return (INT_PTR)TRUE;
                  
                  	case WM_COMMAND:
                  		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
                  		{
                  			EndDialog(hDlg, LOWORD(wParam));
                  			return (INT_PTR)TRUE;
                  		}
                  		break;
                  	}
                  	return (INT_PTR)FALSE;
                  }
                  i declared the varabile in line 43 and i used messagebox in line 183
                  Last edited by ramilol; Aug 9 '10, 06:32 AM. Reason: forgot something

                  Comment

                  • johny10151981
                    Top Contributor
                    • Jan 2010
                    • 1059

                    #10
                    OK,
                    The issue is you need to understand the grammatical rules and structure of c/c++.

                    You have defined args in _tWinMain. So args is a local variable to for _tWinMain

                    But you are trying to access this variable from your WndProc which is a different function. So an obvious compiler error is generating.

                    Your problems solution can be taken from your own source code. Take a look at hInstance variable in _tWinMain this is a local variable. But to access this data from WndProc, you(automated system of visual studio IDE) have defined a global variable name hInst outside main. and then hInst is being accessed from WndProc. So, you can define your args globally. and Dont create message alert from your WM_PAINT

                    Comment

                    • ramilol
                      New Member
                      • Aug 2010
                      • 14

                      #11
                      greate it worked and i was creating message because it was the simple way that it would show me if it works

                      Comment

                      Working...