Jump to content
  • Advertisement
Sign in to follow this  
Kranar

Windows MFC taking in a parameter

This topic is 5130 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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).

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
Alright, when a file is associated with an exe file, is the parameter passed the path and name of the file?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!