Solved

MFC _tmain  passing parameters

Posted on 2006-10-31
2
1,475 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 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

11 Experts available now in Live!

Get 1:1 Help Now