Hi everyone,
I've been writing this program and updating it for a while, but until now I can't get rid of this one bug, I can't input data from the keyboard!
Everything used to work fine until I started using main() arguments to open files. To open a file, all you need to do is drag it onto the program's file and the program will start automatically (I'm not sure if this is standard, but the file's path is passed as an argument on Windows XP).
Since it's an encrypting program, you obviously need to input a password, but I can't input one, neither can I press enter to continue.
These are the functions I'm using:
Code:
/* input password from user */
void getpassword(char password[], int maxlength)
{
int i=0, done=0;
char c;
printf("Please input password (max. %d characters): ", maxlength);
fflush(stdout);
while (!done)
{
c = getchar();
switch(c)
{
case '\r':
case '\n':
done = 1;
break;
case '\b':
if (i > 0) /* perform backspace only if characters have already been input */
{
printf("\b \b");
i--;
}
break;
default:
if (i<maxlength && isprint(c)) /* save character in password as long as it's not a none-printing character */
{
password[i++] = c;
putchar('*');
}
break;
}
fflush(stdout);
}
password[i] = '\0';
}
/* like getchar() except without echo */
char mygetch(void)
{
static HANDLE hIn;
static DWORD NumRead;
static INPUT_RECORD InRec;
static char c;
hIn = GetStdHandle(STD_INPUT_HANDLE);
if(hIn == INVALID_HANDLE_VALUE)
{
printf("GetStdHandle poop");
}
else
{
do
{
ReadConsoleInput(hIn, &InRec, 5, &NumRead);
if (InRec.EventType == KEY_EVENT)
{
if (InRec.Event.KeyEvent.bKeyDown == 1)
{
c = InRec.Event.KeyEvent.uChar.AsciiChar;
/* testing print
printf(" KDown Repeat KeyCode ScanCode Unicode Ascii Ctrl-State \n");
printf(" %3x %3x %3x %3x %3x %3x %3lx \n",
InRec.Event.KeyEvent.bKeyDown,
InRec.Event.KeyEvent.wRepeatCount,
InRec.Event.KeyEvent.wVirtualKeyCode,
InRec.Event.KeyEvent.wVirtualScanCode,
InRec.Event.KeyEvent.uChar.UnicodeChar,
InRec.Event.KeyEvent.uChar.AsciiChar,
InRec.Event.KeyEvent.dwControlKeyState );
*/
/* printf("|if %c %02x ", c, c); testing*/
break;
}
}
}
while( c != '\r' );
}
return c;
}
I got the mygetch() function from one of the forum members here (I think Mcgrayer or something like that) and after a lot of debugging I found that the problem is with it, because when I replaced it with getchar() it worked fine (except for the output of course). I am also aware that it only works on Windows.
But why doesn't mygetch() work anymore?
Please note that I also made sure my program worked if you don't drag a file onto it, but it obviously asks you for the file name and inputs it manually using fgets, and when I use this method to open the file the password input always works.
I am using Visual C++ Express 2008.
What's even weirder, is that the problem doesn't appear the first time you run the program, but it appears the second time with the exact same file(s).
I don't mind giving you the source, but it's about 1000 lines and I don't think any of you have the time to do so. Of course if you can, I'd be thankful.
Thank you,