no console output in eclipse - c++

I am using windows 7 32- bit and eclipse juno for cpp programming . When I tried to run the "Hell World" program, it is showing no console output. The program builds without any error. can anyone suggest a solution?
(Code pasted from comment)
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
cout<<"HELLO WORLD"<<endl;
}

It's an eclipse bug. You need to add:
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
to the start of main.

Related

Playing sound using MinGW

I want to play sounds in my application using mciSendString. For this purpose I found the following simple snippet on the Microsoft website:
#include "stdafx.h"
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "Winmm.lib")
int _tmain(int argc, _TCHAR* argv[])
{
mciSendString("play MyFile wait", NULL, 0, 0);
mciSendString("close MyFile", NULL, 0, 0);
return 0;
}
My problem is that I don't use Visual Studio. Is there a way to get this example compiled via MinGW?
Once I remove the MSVS-isms
#include <windows.h>
int main()
{
mciSendString("play MyFile wait", NULL, 0, 0);
mciSendString("close MyFile", NULL, 0, 0);
return 0;
}
and compile with
g++ -o test.exe "src\\test.o" -lwinmm
as per the linked duplicate, the build is successful.

Attempting to open CD tray

I'm trying to open and close the CD tray of my computer using a piece of code. I have been using MCI commands and have included winmm.lib in the additional dependencies of my project configuration. I've included windows.h and mmsystem.h as well.
The code I'm using is as follows:
mciSendCommand(0, MCI_SET, MCI_SET_DOOR_OPEN, NULL);
mciSendCommand(1, MCI_SET, MCI_SET_DOOR_CLOSED, NULL);
The code builds and runs fine, there's just no CD tray action going on! Can anyone suggest how I need to adapt this?
If you have multiple CD-Drives you should use the following code:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
int _tmain()
{
DWORD dwBytes;
HANDLE hCdRom = CreateFile(_T("\\\\.\\M:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hCdRom == INVALID_HANDLE_VALUE)
{
_tprintf(_T("Error: %x"), GetLastError());
return 1;
}
// Open the door:
DeviceIoControl(hCdRom, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, &dwBytes, NULL);
Sleep(1000);
// Close the door:
DeviceIoControl(hCdRom, IOCTL_STORAGE_LOAD_MEDIA, NULL, 0, NULL, 0, &dwBytes, NULL);
CloseHandle(hCdRom);
}
You are missing some steps, first you need to open the device.
Try this:
#pragma comment( lib, "winmm.lib" )
#include "stdafx.h"
#include <Windows.h>
#include <mmsystem.h>
int _tmain()
{
MCI_OPEN_PARMS mPar = { 0 };
mPar.lpstrDeviceType = reinterpret_cast<LPCWSTR>(MCI_DEVTYPE_CD_AUDIO);
// Open device
mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID, (DWORD)&mPar);
// Open tray
mciSendCommand(mPar.wDeviceID, MCI_SET, MCI_SET_DOOR_OPEN, 0);
// Close tray
mciSendCommand(mPar.wDeviceID, MCI_SET, MCI_SET_DOOR_CLOSED, 0);
// Close device
mciSendCommand(mPar.wDeviceID, MCI_CLOSE, MCI_WAIT, 0);
return 0;
}
Try using DevC++ IDE (WINDOWS ONLY)
Then follow steps:
Step 1:
File > Project > Console Application << Enter
Step 2:
Project Options > Parameters > Linker > write "-lWinmm" in Linker << Enter
Step 3: Open cdtray Copy and paste this small code in your IDE. I recommend DevC++ for this one..
#include<windows.h>
int main(){
mciSendString("set cdaudio door open",0,0,0);
}
Step 4: Close tray, Just change door 'open' to 'closed'
mciSendString("set cdaudio door closed",0,0,0);

How to execute another exe from a C++ program in Windows

I want my C++ program to execute another .exe, in Windows. How would I do this? I am using Visual C++ 2010.
Here is my code
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
unsigned int input;
cout << "Enter 1 to execute program." << endl;
cin >> input;
if(input == 1) /*execute program here*/;
return 0;
}
This is a solution I found when looking for an answer previously.
It stated that you should always avoid using system() because:
It is resource heavy
It defeats security -- you don't know you it's a valid command or does the same thing on every system, you could even start up programs you didn't intend to start up. The danger is that when you directly execute a program, it gets the same privileges as your program -- meaning that if, for example, you are running as system administrator then the malicious program you just inadvertently executed is also running as system administrator.
Anti virus programs hate it, your program could get flagged as a virus.
Instead CreateProcess() can be used. Createprocess() is used to just start up an .exe and creating a new process for it. The application will run independent from the calling application.
#include <Windows.h>
void startup(LPCSTR lpApplicationName)
{
// additional information
STARTUPINFOA si;
PROCESS_INFORMATION pi;
// set the size of the structures
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// start the program up
CreateProcessA
(
lpApplicationName, // the path
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
CREATE_NEW_CONSOLE, // Opens file in a separate console
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure
);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
you can use the system function
int result = system("C:\\Program Files\\Program.exe");
Use the CreateProcess() Function.
See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx for details
You can make a call using system
system("./some_command")
I believe this answer should work with different programs, I tested it with Chrome.
// open program.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "string"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string command = "start chrome https://www.google.com/";
system(command.c_str());
return 0;
}

CreateProcess() launching child applications

I want to launching a sub application from the main application using CreateProcess function with the following steps:
launched a sub .exe program from the main without window for the sub program
wait for rand Sleep
then terminate the sub application first then the main.
In the following my example code for the above but the sub program running with window(in this case NotePad) and i can't terminate the sub program.
#include "stdafx.h"
#include <windows.h>
#include <conio.h>
#include <strsafe.h>
#include <direct.h>
#include <string.h>
int main(int argc, char* argv[])
{
HWND hWnd;
STARTUPINFO sInfo;
PROCESS_INFORMATION pInfo;
ZeroMemory(&sInfo, sizeof(sInfo));
sInfo.cb = sizeof(sInfo);
ZeroMemory(&pInfo, sizeof(pInfo));
if (CreateProcess("C:\\WINDOWS\\System32\\notepad.exe", NULL, NULL, NULL, false, CREATE_NO_WINDOW, NULL, NULL, &sInfo, &pInfo))
{
printf("Sleeping 100ms...\n");
Sleep(100);
DWORD dwExitCode;
GetExitCodeProcess(pInfo.hProcess, &dwExitCode);
CloseHandle(pInfo.hThread);
CloseHandle(pInfo.hProcess);
}
system("pause");
return 0;
}
The reason the notepad window shows is because it's not a console application. MSDN says this about CREATE_NO_WINDOW:
The process is a console application that is being run without a console window. Therefore, the console handle for the application is not set.
This flag is ignored if the application is not a console application, or if it is used with either CREATE_NEW_CONSOLE or DETACHED_PROCESS.
Instead, use the STARTUPINFO you pass in:
sInfo.dwFlags = STARTF_USESHOWWINDOW;
sInfo.wShowWindow = SW_HIDE;
I believe that will affect the last argument to WinMain in Notepad's main function, but I'm not sure.
As for why notepad doesn't close, GetExitCodeProcess doesn't actually end the process, it just retrieves the state. You can use TerminateProcess instead:
TerminateProcess(pInfo.hProcess, 0);

Create window console inside main win32 window

I have a win32 application that need to open a console like the games when tilde is pressed. I tought that the best solution is to use the CreateWindow function. Is this right? How could I make it overlapping the main window and hiding it when tilde is pressed again? Thank you all
The solutions here won't work because newer versions of the Windows SDK define the FILE structure by:
#ifndef _FILE_DEFINED
#define _FILE_DEFINED
typedef struct _iobuf
{
void* _Placeholder;
} FILE;
#endif
When trying to overwrite the stdin/out FILE structures with the = operator, only one pointer will be copied. To copy the whole FILE struct, you have to define the FILE structure before your windows.h include:
#ifndef _FILE_DEFINED
struct _iobuf {
char *_ptr;
int _cnt;
char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char *_tmpfname;
};
typedef struct _iobuf FILE;
#define _FILE_DEFINED
#endif
#include <Windows.h>
If this is somehow not possible for you, you can still define your own FILE structure as FILE_COMPLETE and use this codesnippet:
#include <Windows.h>
#include <io.h>
#include <fcntl.h>
AllocConsole();
SetConsoleTitleA("ConsoleTitle");
typedef struct { char* _ptr; int _cnt; char* _base; int _flag; int _file; int _charbuf; int _bufsiz; char* _tmpfname; } FILE_COMPLETE;
*(FILE_COMPLETE*)stdout = *(FILE_COMPLETE*)_fdopen(_open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT), "w");
*(FILE_COMPLETE*)stderr = *(FILE_COMPLETE*)_fdopen(_open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT), "w");
*(FILE_COMPLETE*)stdin = *(FILE_COMPLETE*)_fdopen(_open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT), "r");
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
setvbuf(stdin, NULL, _IONBF, 0);
It's often tempting to use a console window in your app (using AllocConsole), but it is definitely NOT a standard reusable Windows control. It has a lot of special behaviors and features which make it unique from a typical window.
For this reason, I would agree with your instinct, against using a true 'Console' window. Make your own window with a text editor in it, as you would develop any other UI component like a HUD.
Whether you should use CreateWindow is hard to say: how are you doing the rest of your GUI? DirectX? GDI? Some toolkit? Are you using other standard windows controls?
This is some pretty old code, haven't even really looked over it. Hopefully it's what you need. If you just need a very simple one you can also just make a call to AllocConsole();
void DevConsole::Create(){
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
int consoleHandleR, consoleHandleW ;
long stdioHandle;
FILE *fptr;
AllocConsole();
std::wstring strW = L"Dev Console";
SetConsoleTitle( strW.c_str() );
EnableMenuItem(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE , MF_GRAYED);
DrawMenuBar(GetConsoleWindow());
GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo );
stdioHandle = (long)GetStdHandle( STD_INPUT_HANDLE );
consoleHandleR = _open_osfhandle( stdioHandle, _O_TEXT );
fptr = _fdopen( consoleHandleR, "r" );
*stdin = *fptr;
setvbuf( stdin, NULL, _IONBF, 0 );
stdioHandle = (long) GetStdHandle( STD_OUTPUT_HANDLE );
consoleHandleW = _open_osfhandle( stdioHandle, _O_TEXT );
fptr = _fdopen( consoleHandleW, "w" );
*stdout = *fptr;
setvbuf( stdout, NULL, _IONBF, 0 );
stdioHandle = (long)GetStdHandle( STD_ERROR_HANDLE );
*stderr = *fptr;
setvbuf( stderr, NULL, _IONBF, 0 );
}
I found this little tutorial / code samples useful.
http://www.halcyon.com/~ast/dload/guicon.htm
It creates a console and redirects STD_IN and STD_OUT to the console.
So you can easily use std::cin and std::cout etc streams.