Solved

MFC _tmain  passing parameters

Posted on 2006-10-31
2
1,479 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
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:
JoseParrot earned 125 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 128
topping3 challenge 14 70
Adoquery sql  left join does not work 25 80
Programatically extract date from website 8 67
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
This is about my first experience with programming Arduino.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now