Windows MFC taking in a parameter

Started by
5 comments, last by Kranar 19 years, 7 months ago
I made a new program using the MFC library, and I noticed that having a main isn't nessecary, all I need to do is create an object of type CWinApp and it's constructor will startup the program. But I need my program to take in a parameter. In console programming you could do this using: int main(char* args); How is this done using CWinApp? Basically my program is this:

class UltraTechApp : public CWinApp {
public:
	virtual BOOL InitInstance();
};

UltraTechApp Ultra;

class UltraTechWindow : public CFrameWnd {
public:
	UltraTechWindow();
private:
	CStatic* cs;
};

BOOL UltraTechApp::InitInstance() {
	m_pMainWnd = new UltraTechWindow();
	m_pMainWnd->ShowWindow(m_nCmdShow);
	m_pMainWnd->UpdateWindow();
	/* more code */
}
Advertisement
You may have to check the m_lpCmdLine member of you CWinApp object. Of course, you'll have to parse this command line since all the program arguments are in a single LPTSTR. Not very good, but since it is you only chance to get the command line...

HTH,

[edit]
Actually, your main() should be
int main(int ac, const char **av)


This is forbidden when creating a MFC program (not sure it will even take care of this, since the program entry point is the WinMain() function, and not the main() function).
Its actually extremly(sp?) easy to get the command line arguments. MFC defines two global variables

__argc which defines the number of command line args (the first one being the app name

__argv which is an array of char* containing the command line arguments.

So to check the command line

void CAppCore::DealWithCommandLine(void){  // Do we have any arguments?  // The first argument is the app name, so we need two  if (__argc < 2)    return;  // Just use the first  DoSomethingWithTheFirst(__argv[1]);}


That should help you solve your problem
Spree
Alright, when a file is associated with an exe file, is the parameter passed the path and name of the file?
MFC has a class that handles the command line options, CCommandLineInfo. In InitInstance for your CWinApp, add these lines:

CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);

Calling ProcessCommandLine (cmdInfo) will take care of the standard flags.

If you need custom flags, then derive your own class from CCommandLineInfo, and override ParseParam to handle the custom flags.
Quote:Original post by Kranar
Alright, when a file is associated with an exe file, is the parameter passed the path and name of the file?


As far as I can tell, when you double click an file and it launches your app, the command lines is passed the full path name of the file. The path and file name are _not_ passed as seperate parameters.

Thats the assumption that all my MFC apps make and they work fine

Spree
Thanks a lot for the quick responses!

This topic is closed to new replies.

Advertisement