본문 바로가기
VisualStudio.C++.C#/코딩팁,함수활용,단편

MFC 응용프로그램 실행 인자 전달하기.

by i.got.it 2016. 1. 28.

 

 

개요.

  MFC응용프로그램 실행시점 인자 전달된 것 코드에서 확보하는법.


필요성.
커맨드라인으로 응용프로그램 실행시키면서 인자 전달하고  프로그램내에서 인자 확보하여 처리 요구되는 경우.


이해사항.
MFC CWinApp 에서 상속받은 응용프로그램은 코드에서 특별한 코드 작성 하지 않아도 인자 입력 받을 수 있게 구성되어있다.
한편, 커맨드라인으로 인자 전달하면서 실행시킨 경우 코드내에서 인자 받기 위해서는 LPTSTR 타입의  CWinApp의 멤버변수 m_lpCmdLine 값을 참조 한다.  InitInstance 함수 내부에서 m_lpCmdLine 값 참조한다.


더보기
CWinApp::m_pCmdLine


Corresponds to the lpCmdLine parameter passed by Windows to WinMain.
 
복사
LPTSTR m_lpCmdLine;
Points to a null-terminated string that specifies the command line for the application. Use m_lpCmdLine to access any command-line arguments the user entered when the application was started. m_lpCmdLine is a public variable of type LPTSTR.
C++
복사
if (m_lpCmdLine[0] == _T('\0')) { // Create a new (empty) document. OnFileNew(); } else { // Open a file passed as the first command line parameter. OpenDocumentFile(m_lpCmdLine); }
Header: afxwin.h



혹은  더 쉬운 방법으로 인자수량을  __argc , 인자 문자열 배열을 __wargv[], __argv[] 로 받는 방법이 있다. 

인자 전달하지 않은 경우 

__wargv[0] : 실행파일명(확장자 포함)과 절대 경로가 기록되어있다. __argc = 1 

외부에서 인자 전달한 경우 

__wargv[0] : 절대경로 없는 실행파일명(확장자 포함)만 기록되어있고, __argc 는 외부 에서 전달한 수량 + 1 의 값을 갖는다. 



더보기


The __argc global variable is a count of the number of command-line arguments passed to the program. __argv is a pointer to an array of single-byte-character or multi-byte-character strings that contain the program arguments, and __wargv is a pointer to an array of wide-character strings that contain the program arguments. These global variables provide the arguments to main or wmain.
 
복사
extern int __argc; extern char ** __argv; extern wchar_t ** __wargv;
In a program that uses the main function, __argc and __argv are initialized at program startup by using the command line that's used to start the program. The command line is parsed into individual arguments, and wildcards are expanded. The count of arguments is assigned to __argc and the argument strings are allocated on the heap, and a pointer to the array of arguments is assigned to __argv. In a program compiled to use wide characters and a wmain function, the arguments are parsed and wildcards are expanded as wide-character strings, and a pointer to the array of argument strings is assigned to __wargv.
For portable code, we recommend you use the arguments passed to main to get the command-line arguments in your program.
Generic-Text Routine Mappings
Tchar.h routine _UNICODE not defined _UNICODE defined
__targv __argv __wargv
 
Global variable Required header
__argc, __argv, __wargv <stdlib.h>, <cstdlib> (C++)
__argc, __argv, and __wargv are Microsoft extensions. For compatibility information, see Compatibility.


   

코드예 

- __argc 는 MFC 프로그램 실행시 전달된 인자 수량이며,  MFC App 클래스의 InitInstance 시점부터 해당 값을 받을 수 있다. 

 


BOOL CCyP2L1_CyFinBot__App::InitInstance()
{

// __argc 는 인자 수량. 인자전달 없는 경우에도 기본1기록되어있다. 
// __wargv[0] 에 항상 실행파일경로와 이름이 기록되어있음. 
	CString cst;
	cst.Format(L"argc = %d", __argc); 
	AfxMessageBox(cst); 

// 모든 전달된 인자 접근. 
for (int i = 1 ; i < __argc ; i++)
{
    __argv[i];
}

	....
    
}

 

 

 

 

 


첫 등록 : 2016.01.28

최종 수정 : 2022.02.13

단축 주소 : https://igotit.tistory.com/628


 

댓글



 

비트코인




암호화폐       외환/나스닥/골드       암호화폐/외환/나스닥/골드 암호화폐/외환/나스닥/골드   암호화폐/외환/나스닥/골드
     
현물 |선물 인버스 |선물 USDT       전략매니저(카피트레이딩)     롤오버 이자 없는 스왑프리계좌
( 스왑프리 암호화폐도 거래 가능 )    
MT4, MT5 , cTrader 모두 지원     FTMO 계좌 매매운용. MT4,MT5