?
Solved

MFC _tmain  passing parameters

Posted on 2006-10-31
2
Medium Priority
?
1,536 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 …
Six Sigma Control Plans

650 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