Thread: Login program

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    13

    Login program

    Hi,
    can anybody Write a program for Login that is while enter the password it should be print as * to corresponding character?






    Thanks in advance,
    srinivas k

  2. #2
    Registered User
    Join Date
    Nov 2007
    Location
    Bangalore, India
    Posts
    24
    can anybody Write a program for Login
    Yes programmers can generally do that.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you're writing for Windows....:

    Code:
    char *getPass(char *szBuffer, size_t len)
    {
    	size_t i;
    	HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
    	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    	DWORD dwOld, dwNumRead, dwNumWritten;
    	if((hIn != INVALID_HANDLE_VALUE) && (hOut != INVALID_HANDLE_VALUE))
    	{
    		if(GetConsoleMode(hIn,&dwOld))
    		{
    			if(SetConsoleMode(hIn,ENABLE_PROCESSED_INPUT))
    			{
    				for(i=0;i<len-1;i++)
    				{
    					if(ReadConsole(hIn,&szBuffer[i],1,&dwNumRead,NULL))
    					{
    						if(szBuffer[i] == '\r')
    						{
    							break;
    						}
    						else WriteConsole(hOut,"*",1,&dwNumWritten,NULL);
    					}
    					else break;
    				}
    				WriteConsole(hOut,"\n",1,&dwNumWritten,NULL);
    				szBuffer[i] = '\0';
    				SetConsoleMode(hIn,dwOld);
    				return szBuffer;
    			}
    		}
    	}
    	return NULL;
    }

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by MacGyver View Post
    If you're writing for Windows....:
    Or even easier, just use getch() in a loop and print a '*' for each character the user enters.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by cpjust View Post
    Or even easier, just use getch() in a loop and print a '*' for each character the user enters.
    I frown on anything that promotes getch() and conio.h.

    What I posted should work across Windows for all compilers. What you are suggesting is not guarenteed to work on all Windows compilers.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    13

    Can't we write program without using windows APIs?

    Quote Originally Posted by MacGyver View Post
    If you're writing for Windows....:

    Code:
    char *getPass(char *szBuffer, size_t len)
    {
    	size_t i;
    	HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
    	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    	DWORD dwOld, dwNumRead, dwNumWritten;
    	if((hIn != INVALID_HANDLE_VALUE) && (hOut != INVALID_HANDLE_VALUE))
    	{
    		if(GetConsoleMode(hIn,&dwOld))
    		{
    			if(SetConsoleMode(hIn,ENABLE_PROCESSED_INPUT))
    			{
    				for(i=0;i<len-1;i++)
    				{
    					if(ReadConsole(hIn,&szBuffer[i],1,&dwNumRead,NULL))
    					{
    						if(szBuffer[i] == '\r')
    						{
    							break;
    						}
    						else WriteConsole(hOut,"*",1,&dwNumWritten,NULL);
    					}
    					else break;
    				}
    				WriteConsole(hOut,"\n",1,&dwNumWritten,NULL);
    				szBuffer[i] = '\0';
    				SetConsoleMode(hIn,dwOld);
    				return szBuffer;
    			}
    		}
    	}
    	return NULL;
    }

    Can't we write a program by using c-libray functions without using windows APIs?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    I've worked on this for days. The only way to get it to work was use the function above
    Some said that if you're using visual studio you can just use getch() but I've never tried that.
    I have tried using conio.h and getch() using GCC but they didn't work.

    Do you really need to make it that portable? are you thinking of compiling it for anything other than windows?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kollurisrinu View Post
    Can't we write a program by using c-libray functions without using windows APIs?
    No, this type of code will by it's nature use unportable functions. You may of course be able to use some sort of portable library to "hide" the unportable bits, e.g. ncurses or some such. But there are no standard C library functions to do this - there never has been, and there probably never will be, as there are certainly the possibility that some architectures don't even support this mode of operation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    13
    Quote Originally Posted by matsp View Post
    No, this type of code will by it's nature use unportable functions. You may of course be able to use some sort of portable library to "hide" the unportable bits, e.g. ncurses or some such. But there are no standard C library functions to do this - there never has been, and there probably never will be, as there are certainly the possibility that some architectures don't even support this mode of operation.

    --
    Mats
    i am trying to write a code that works on any platforms using C-library functions but i am unable to implement .

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kollurisrinu View Post
    i am trying to write a code that works on any platforms using C-library functions but i am unable to implement .
    Yes, that's not going to work - you WILL need some system specific code in there - there's no other way. This is because the C library doesn't have functions to turn off the echo (that is, there's no way to read characters from the keyboard without them also being printed to the screen), nor is there a way to "get one character at a time" in the standard library - it's all being processed by the OS into a line before it's being made available to the application. You need a way to bypass both the echo and the "hold it until a line is complee" to implement this, and there's just no way to do that without using system specific code. You can use third party libraries such as ncurses, but the standard C library will not do it. There's just no way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Let's see if he gets it this time that it's not going to work.

    To the OP: Your options are to either use a portable library that has separate implementations for most systems, to write your own, or to simply just use the one on the platform you need right now.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I've always wondered why there isn't a standard function to do this (like getch())? It certainly sounds like a fairly common thing that a lot of programs would need to do.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    I've always wondered why there isn't a standard function to do this (like getch())? It certainly sounds like a fairly common thing that a lot of programs would need to do.
    Probably because it's not guaranteed to be supported - and because once you get that far, you probably want some other control(s) over the input too, and this quickly gets system specific. There are libraries that support it this any much more, such as ncurses. It's just not part of the C standard.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    Probably because it's not guaranteed to be supported
    Why not? getchar() reads a character from the keyboard and prints it to the screen. getch() only does half the work, so you'd think it would be easier to implement than getchar().

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    Why not? getchar() reads a character from the keyboard and prints it to the screen. getch() only does half the work, so you'd think it would be easier to implement than getchar().
    No. Getchar reads a single character from the input stream - the printing is done by the underlaying OS's line input processing. Consider for example a teletype - it has no way to NOT print the character you entered [the way to "hide" passwords on a teletype is to read one char, then output "\10%\10*\10X\10\\\10$" or some such, so that the input gets thoroughly covered in ink]. Yes, I know, there aren't many teletypes around these days. There are, however, still some types of terminals [or emulators of such running on PC's] that do local line processing, and those would fall into the same criteria of not being able to do this.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Login Form for program
    By Houssen in forum C++ Programming
    Replies: 3
    Last Post: 03-29-2008, 02:35 PM
  2. simple login program problem
    By suckss in forum C Programming
    Replies: 11
    Last Post: 11-11-2006, 05:02 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  5. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 05:50 AM