Hey guys!
I'm going to start learning C/C++ (again), and I want to start with a very simple program.

It should ask to the user a number and, if it is right, run a program.

Its been a long time that I don't even touch anything from C, but I think that it might look something like this:

void main()
{
int password; //the password will be numerbers only

printf("Please enter the password:\n");
scanf("%d", &password);

if(password == "123456"
{
//code to run external app here
}
else
{
//error message here
}
}

//eof

Is this right? I've been looking for how to start a process, and I ended up with ShellExecute() and CreateProcess() . I'm not going to pass any args to the program, I just need to make sure to run it only if the correct password has been entered. Which one is best?

Thanks! /* sorry for the non-idented code, I'm not using any IDE right now */

Recommended Answers

All 5 Replies

close.
>>if(password == "123456"

Since password is an integer you don't need those quotes, and you do need a ) at the end if(password == 123456)

forgot it, sorry!
but, about the starting an external app thing...
whats the best way?

As for using either CreateProcess() or ShellExecute(), why don't you try both and see which one suites your purpose better. CreateProcess() gives you the most control over the process to be created, but ShellExecute() may be a little easier to use.

The problem is that I really don't know how to use them. I was searching for a while, but nothing was clear enough to me.

Maybe somebody here can give me a little explanation about on how to use? Don't need to explain both, just one is fine to me :)

Example of CreateProcess() which calls Notepad.exe to display a text file. You need to read MSDN article to understand all the parameters. This example will also wait until you close Notepad before continuing.

#include <Windows.h>

int main( )
{
    char args[255] = {0};
    STARTUPINFO sinfo;
    PROCESS_INFORMATION pinfo;
    memset(&sinfo,0,sizeof(sinfo));
    memset(&pinfo,0,sizeof(pinfo));
    sinfo.cb = sizeof(sinfo);
    strcpy(args,"Notepad.exe c:\\dvlp\\test.bat");
    if( CreateProcess(NULL,args,0,0,0,0,0,0,&sinfo,&pinfo) )
    {

        WaitForSingleObject(pinfo.hProcess,INFINITE);
    }
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.