Playing sound using MinGW - c++

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.

Related

Trouble attempting to upload file to FTP using wininet [Id returned 1 exit status]

I got this code from another post, but when attempting to compile it shows the following errors:
This is the code:
#include <iostream>
#include <wininet.h>
#pragma comment(lib, "Wininet")
int main() {
HINTERNET hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
HINTERNET hFtpSession = InternetConnect(hInternet, "host", INTERNET_DEFAULT_FTP_PORT, "user", "passwd", INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
FtpPutFile(hFtpSession, "D:\\test.txt", "\\test.txt", FTP_TRANSFER_TYPE_BINARY, 0);
std::cout << "File Uploaded." << std::endl;
InternetCloseHandle(hFtpSession);
InternetCloseHandle(hInternet);
return 0;
}
I'm using windows 7 x86 and gcc 10.3.
gcc does not support #pragma comment(lib, <filename>). You can use gcc's -Werror=unknown-pragmas command-line parameter to verify that.
You will need to use gcc's -l command-line parameter to link to import libraries, like wininet.lib.

IAudioEndpointVolume has no member named GetMasterVolumeLevelScalar

Consider this program:
#include <stdio.h>
#include <windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <math.h>
int main() {
IAudioEndpointVolume *wh;
IMMDevice *ya;
IMMDeviceEnumerator *xr;
CoInitialize(0);
CoCreateInstance(__uuidof(MMDeviceEnumerator), 0, CLSCTX_INPROC_SERVER,
__uuidof(IMMDeviceEnumerator), (void**)&xr);
xr->GetDefaultAudioEndpoint(eRender, eConsole, &ya);
ya->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, 0, (void**)&wh);
float zu;
wh->GetMasterVolumeLevelScalar(&zu);
printf("%d\n", (int) round(100 * zu));
}
I can compile it as C++ with no issue:
x86_64-w64-mingw32-g++ vol.cpp -lole32
However if I try to compile it as C:
x86_64-w64-mingw32-gcc vol.c -lole32
I get errors such as:
error: ‘IAudioEndpointVolume’ has no member named ‘GetMasterVolumeLevelScalar’
This program does not seem to be particularly “C++”, so what is causing the
problem? Also, can I change something so that it compiles as C?
This seems to do it:
#include <stdio.h>
#include <initguid.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <math.h>
int main() {
IAudioEndpointVolume *wh;
IMMDevice *ya;
IMMDeviceEnumerator *xr;
CoInitialize(0);
CoCreateInstance(&CLSID_MMDeviceEnumerator, 0, CLSCTX_INPROC_SERVER,
&IID_IMMDeviceEnumerator, (void**)&xr);
xr->lpVtbl->GetDefaultAudioEndpoint(xr, eRender, eConsole, &ya);
ya->lpVtbl->Activate(ya, &IID_IAudioEndpointVolume, CLSCTX_ALL,
0, (void**)&wh);
float zu;
wh->lpVtbl->GetMasterVolumeLevelScalar(wh, &zu);
printf("%d\n", (int) round(100 * zu));
}
Changes:
#include <initguid.h>
&CLSID_MMDeviceEnumerator instead of __uuidof(MMDeviceEnumerator)
lpVtbl->GetDefaultAudioEndpoint instead of GetDefaultAudioEndpoint
Source

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);

no console output in eclipse

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.

ADsOpenObject bind unsuccessful

I am trying to connect to an AD server from a computer that is outside the domain using ADSI however the bind is unsuccessful. Using Visual c++ 2010 express.
Here is the code snippet:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <wchar.h>
#include <objbase.h>
#include <activeds.h>
#include <AdsHlp.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
IADs *pObject;
HRESULT hr;
hr = ADsOpenObject(L"LDAP://aswathy-server3/cn=users,dc=aswathy,dc=local",
L"administrator",
L"password",
ADS_SECURE_AUTHENTICATION,
IID_IADs,
(void**)&pObject);
if(SUCCEEDED(hr))
{
cout<<"Success";
pObject->Release();
}
else
cout<<"Unsuccessful";
getch();
return 0;
}
I have included adsiid,lib and activeds.lib under project properties -> linker -> input -> additional dependencies.
Does anyone know why bind is not successful?
hr = ADsOpenObject(L"WinNT://aswathy.local/users",
L"administrator",
L"password",
ADS_SECURE_AUTHENTICATION,
IID_IADs,
(void**)&pObject);