?
Solved

MFC _tmain  passing parameters

Posted on 2006-10-31
2
Medium Priority
?
1,520 Views
Last Modified: 2008-01-09
Hello,

Can someone write a short pseudo code to pass a string parameter to a MFC console application that supports MFC?  I am trying to pass a string to the _tmain function.  

int   _tmain(int argc, TCHAR* envp[])

thanks in advance....

regards,

Ken
0
Comment
Question by:kenhoppe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17842464
What do you ask: how to handle parameters in this application, or how to call it?
To call application type in the Command prompt:

your_app_file.exe parameter

Other ways are: to write batch file, create shortcut, add command line arguments to VC++ project properties from running from VC++ IDE.

Inside of application:

int   _tmain(int argc, TCHAR* envp[])
{
    if ( argc > 0 )
    {
          // envp[0] points to _T("parameter") string
    }
}

If you want tu run this program form another program, use CreateProcess function. Parameters should be passed using lpCommandLine functionparameter:
http://windowssdk.msdn.microsoft.com/en-us/library/ms682512.aspx
Replace LPTSTR szCmdline=_tcsdup(TEXT("MyChildProcess")) with LPTSTR szCmdline=_tcsdup(TEXT("MyChildProcess parameter"));
0
 
LVL 18

Accepted Solution

by:
Jose Parrot earned 500 total points
ID: 17842621
Hi,

Comparing to standard console c program
    int main( int argc, char *argv[ ], char *envp[ ] )
we have the equivalent syntax in c console application supporting MFC
    int __cdecl _tmain(int argc, _TCHAR **argv, _TCHAR **envp)
Another format (in essence it is the same, also supporting MFC)
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
       if (argc == 1) // argv[0] is the application self name
       {
            show message "There is no arguments"
       }
       if (argc == 2)
       {
            show argument argv[1]
       }

If the symbol _UNICODE is defined for your program, TCHAR is defined as type wchar_t, a 16-bit character type; otherwise, it is defined as char, the normal 8-bit character type.

You can use the CString type, which is based on the TCHAR data type.
Under Unicode, then, CString objects are composed of 16-bit characters. Without Unicode, they are composed of 8-bit char type.

Jose

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question