Sign in to follow this  
Kranar

Windows MFC taking in a parameter

Recommended Posts

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 */
}

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this