_getch() doesn't work on windows server 2012 - c++

I need a DLL for users can type password and not echo on the screen.
So I use _getch() for getting chars with no echo like this,
//get character with no echo
ch = _getch();
and compile the code use microsoft vs2005.
It works on windows server 2003/2008, but on the new windows server 2012, it echos the characters to the screen.
My problem is why _getch() echo characters only on windows server 2012? and how to fix it?

Use cin.get() in place of getch() .

Related

How can I keep open the command prompt in my windows form application in c++?

I wrote a project in windows form application in C++ by Visual Studio 2010. I need to open the cmd and then type special command and run other program.
I use this function :
system("cmd.exe /c dir c:\\");
but by this function I just saw cmd for a second and then it was disappeared.
then I add this line :
cin.get();
but it did not work.
also I use this function :
char program[] = "C:\Windows\System32\cmd.exe";
WinExec((LPCSTR)program, SW_SHOWMINIMIZED);
but it did not work either! Can you help me please?
Have you tried the following?
system("cmd /k dir c:\\");
/k keeps the cmd prompt window open after the executing process has terminated.
But, to be honest, it may be better to use the Windows Terminal Services API for finer control, if you so desire. But, depending on what you want to do - that might be overkill.
And, regarding your second question: don't forget to escape your backslashes in:
const char program[] = "C:\\Windows\\System32\\cmd.exe";
WinExec((LPCSTR)program, SW_SHOWMINIMIZED);
References:
https://superuser.com/questions/306167/how-to-prevent-the-command-prompt-from-closing-after-execution
You could try
cin.ignore();
maybe also in combination with cin.get()
cin.get();
cin.ignore();
I think normally cin.get() worked fine in my programs.

Visual Studio .exe files open and immediately close

I have a problem, when I try to open my Hello World.exe file (that I created by following a tutorial). It immediately closes without giving me the chance to read or see if I have done everything correctly.
As you can see, I need help on how to keep it open, without instantly closing.
You can either put a break point before the end of main or try the following:
int main()
{
//...
std::cin.get();
return 0;
}
It is going to wait for you to press some key to exit the console.
EDIT: It is better to add break point which do not change existing code.
In console applications there are a couple of things you can do to stop the window from closing on you such as using system("pause") (not so recommended though), getch(), std::cin >> x etc at the end of the application.
Another option is to start a cmd window, cd to the location of the exe and run it like any other console application is meant to be ran, that way it wont just close on you, it'll simply exit.
In VS2017, you can specify that the executable is a Console app, not a Windows app. This makes your application run in a "Microsoft Visual Studio Debug Console", with "Press Any Key To Close This Window" appearing at the end of the console output:
Right click the project to bring up Properties.
Linker > System > Subsystem > Select "Console".
Press Apply before closing.

_tprintf with Unicode characters in a console app

I'm doing this simple output from a Unicode-built console application (using C++ and Visual Studio 2008). This code is intended to run on Windows:
_tprintf(L"Some sample string\n");
Everything works fine. But if I add an non-ASCII character in there:
_tprintf(L"Some sample € string\n");
what gets output to the console is everything until that character:
Some sample
What am I doing wrong here?
By default, windows console does not process wide characters. Probably the simplest way to enable that functionality is to call _setmode:
_setmode(_fileno(stdout), _O_WTEXT);
See MSDN for the required includes, usage examples, and other available modes.

C++ on windows closes a program immediately after launching

I installed minGW and the eclipse CDT, and the console keeps doing something weird. The code of the program is
using namespace std;
#include <iostream>
int main() {
cout << "Hello, windows (8, c++)" << endl;
//system("PAUSE");
return 0;
}
You all know it, its the Hello World program. Now when I run this the Eclipse console displays some stuff about building, and then goes blank. And when I navigate to the HelloWorldProgram.exe in the explorer and run it, a windows flashes up and displays "hello world", but then immediately closes. When I do this on Mac OSX there's no problem, and the windows stays up until I decide to close it. Now I know there's a command
system("PAUSE") //I dont know what I need to import to use this. Could you tell me that too?
Which will give me more or less the same effect, but I'd like to know why Windows does it differently from OSX, and what I can do to fix it (bc this annoys the crap out of me).
Looking forward to your replies!
This happens on Windows because this is just the behavior of the Windows console. You'll have to open up the console manually and then running your program through the console you've opened if you don't want the window to close automatically once the program has executed.
You may want to take a look at these:
What is the Best Practice for Combating the Console Closing Issue?
https://superuser.com/questions/186562/how-can-i-keep-the-terminal-open
Don't use system("pause"), it's wrong for a multitude of reasons (read more about it here).
Put cin.get() before return and the window will stay open until you press enter.
If you want to just run your console program, you should open a console, and run it.
Apparently, the OSX version of Eclipse is configured to open a console, and run the program, and not close it. Maybe you can configure the Win version so, too.
You shouldn't meddle with your program to behave differently on another platform, instead wrap it into something that 'adapts' the behaviour.
Probably, you can tell eclipse to use "cmd /c 'yourprogram.exe && pause'", to open a command window and have it execute your program and then pause.
Just add getch(); before return, and add #include <conio.h>.

system() c++ wont run in VirtualBox

I'm trying to compile and run the app, which was created 4 years ago. It was developed for Windows in Embarcadero RAD Studio C++ builder. Now I try to compile and run it in Windows in VirtualBox using the latest version of RAD Studio. I have a system call to another app in my app:
system("dot.exe -Tjpg -o nfa.jpg NFA_graph.txt");
It keeps returning 1 and the file is not created. I also tried
system("Echo %CD% >> z:\log.txt");
and the file is not created. I also tried like this:
FILE *fpipe;
char *command = "Echo %CD% >> z:\log.txt";
char line[256];
if (0 == (fpipe = (FILE*)_popen(command, "r")))
{
perror("popen() failed.");
exit(1);
}
while (fread(line, sizeof line, 1, fpipe))
{
ShowMessage(line);
}
_pclose(fpipe);
And nothing I get. I wonder if the reason of such strange behaviour is that I'm running this all in VirtualBox?
You're not escaping your \ characters. You should use / in file paths, or \\ if you must. In addition, Windows 7 won't let you write to the root directory of a hard drive w/o administrator access.
To determine if a command environment is available, first do this:
if (!system(NULL)) {
// Can't make any system() calls
}
If your command environment is available, then you need to fully specify the path, making sure to escape the \'s like I mentioned above, and don't write anything to a drive's root directory. Also make note that opening files does not default create directories.
No, it's very unlikely. I see few issues with your code: you did not check errno if system() returns 1. It can help you to spot a real problem. Also, all backslashes must be Esc'ed.
I'm suggesting that dot.exe is not in PATH environment variable, that's the reason of the system() failure.