I'm a musician and a programmer and would like to create my own program to make music.
I'll start with a console application in C++ before I make a GUI.
I'm quiet new to C/C++ and know how to make a basic console application and have read about the Win32 API.
I was looking into MSDN for multimedia in Win32 applications and I found a lot of functions for MIDI: http://msdn.microsoft.com/en-us/library/dd798495(VS.85).aspx
I can receive how many MIDI devices are plugged in this way:
#include <windows.h>
#include <iostream>
using namespace std;
int main() {
cout << midiInGetNumDevs();
cout << " MIDI devices connected" << endl;
return 0;
}
But now i want to find out how these devices are called, with the midiInGetID function I think and a while loop. Can somebody help me with this? The function requires a HMIDIIN parameter and I don't know how I can get one since almost all the MIDI functions use this parameter.
I know this is not the most obvious topic but it would be great if someone could help me.
Thanks :)
To get information, you loop calling midiInGetDevCaps, with a first parameter varying from 0 included to the result of midiInGetNumDevs excluded. Each call fills a MIDIINCAPS struct (you pass a pointer to the struct when you call the function) with information about the Nth device. To open a device, and fill the HMIDIIN needed for other calls, you call midiInOpen with the device number (again, 0 to N-1 included) as the second parameter.
The same concept applies to output devices, except that the names have Out instead of In (and for the structures OUT instead of IN).
Ok I figured it out. I didn't know midiInGetDevCaps requires a call to the specific properties of it to return the device name.
Here is my code:
#include <windows.h>
#include <iostream>
using namespace std;
int main() {
unsigned int devCount = midiInGetNumDevs();
cout << devCount << " MIDI devices connected:" << endl;
MIDIINCAPS inputCapabilities;
for (unsigned int i = 0; i < devCount; i++) {
midiInGetDevCaps(i, &inputCapabilities, sizeof(inputCapabilities));
cout << "[" << i << "] " << inputCapabilities.szPname << endl;
}
}
And thanks for your help!
Related
I just started learning c++ and am using codeblocks. I'm just wondering if there was a way to set my output to display for only a certain amount of time.
For example, I have the code
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
cout << "You need to learn C++!" << endl;
return 0;
}
Now when I hit build and run, it displays the code and says "Press any key to continue". I want to know if there is a way I can set a timer for that display to go away instead of pressing a key.
Thank you!
Closing the window is not the responsibility of the program to do, it's the user responsibility. Anyway, you could stop the program for the time you want using for stopping the program and for measuring the time you want it to stop. If you double click the .exe file generated after compilation it will automatically close after execution, otherwise you will have to close the window by yourself.
More information here, about the std::thread method: http://www.cplusplus.com/reference/thread/this_thread/sleep_for/
And here about the chrono library:https://en.cppreference.com/w/cpp/chrono/duration
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main(){
cout << "Hello world!" << endl;
cout << "You need to learn C++!" << endl;
this_thread::sleep_for (chrono::seconds(3));
cout<<"Program finished!";
return 0;
}
You could also use chrono::miliseconds or chrono::minutes or chrono::hours
There may also be a platform dependent library for closing the window . You could have one for windows, another for linux, but anyway you shouldn't be worrying about that as a beginner.
I've recently picked up C++ and I am using a Mac with Xcode to start learning.
One of the problems I am having is making it BEEP!!
I know there is a lot of StackOverflow questions for this and the most popular seems to be std::cout << "\007"
#include < iostream >
int main() {
std::cout << "\007";
return 0;
}
Is there something I am missing?
Try cout << '\a' << flush; instead. You can make a system call like system("say beep"); but this is very OS dependent and will not work on all machines.
I'm currently learning c++ and i decided that i want to give it a shot and try to write a simple program that can read the values of other programs that are stored in memory (and later on be able to modify these values).
To test it i wrote a program that does a simple addition, i then run my other program and I'm trying to read in real time the value of the first program using its address in the memory.
The code will help you to understand better what i mean.
The "target" program that i want to read the value from:
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
int main()
{
int userValue=0;
int total=0;
int* t;
SetWindowText(NULL, "Memory");
while(1)
{
cin >> userValue;
total += userValue;
t = &total;
cout << *t << endl;
cout <<"Pointer: " << &t <<" Total: " << &total;
}
getch();
return 0;
}
And the program that reads the value from the first one:
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
DWORD address = 0x28FEF0;
int value = 0;
DWORD pid;
HWND hwnd;
hwnd = FindWindow(NULL,"Memory");
if(!hwnd)
{
cout <<"Window not found!\n";
cin.get();
}
else
{
GetWindowThreadProcessId(hwnd,&pid);
HANDLE phandle = OpenProcess(PROCESS_VM_READ,0,pid);
if(!phandle)
{
cout <<"Could not get handle!\n";
cin.get();
}
else
{
while(1)
{
ReadProcessMemory(phandle,(void*)address,&value,sizeof(value),0);
cout << value << "\n";
Sleep(1000);
}
return 0;
}
}
}
The code from the second one was taken from a forum (after googling) and it works fine if i use it with any other program, but it doesn't want to work at all with my program.
I tried changing the address to both the pointer t and the actual integer total, but nothing worked.
I feel like I've done something wrong in the first program rather than the second one.
I'm using Code:Blocks with GCC compiler.
Thanks in advance for any help!
Oh, and Merry Christmas!
&t = the address of the pointer that points to total
&total = the address of the actual "total value"
In your first program you should output "&total"
In your second program you should use ReadProcessMemory to read the address which is output from the first program.
If it doesn't work, you must run the second program as administrator to get permissions. If you still have trouble, call GetLastError() after each Windows API call, check the return value of ReadProcessMemory() and utilize the last argument of ReadProcessMemory() for error checking.
As the title mentions, I am trying to open Microsoft Word through this program and am running into a little bit of difficulty. Having done some research into processes, I decided to go through the route of working with Process ID's and the Fork function to handle opening another file within my program. The area where I seem to be running into some difficulty are with the exec family functions. Their seems to be a variety of different uses for these functions, but I am having a difficult time wrapping my head around which function I should use and whether I am syntatically laying out my arguments correctly.
My console prints the following out to the screen when I type "msword":
Hello ---, what application would you like to open?
msword
Creating Child Process To Open Microsoft Word
parent process
Opening Microsoft Word
#include <stdio.h>
#include <iostream>
#include <string>
// Routine Headers
#include <sys/types.h>
#include <unistd.h>
using namespace std;
//function that actually processes loading the program, will take the result of searchApplication
void loadApplication(string path)
{
// If the user typs Microsoft Word (msword abbreviation...)
if(path == "msword")
{
cout << "Creating Child Process To Open Microsoft Word\n";
pid_t ProcessID = fork();
if(ProcessID == -1)
{
cout << "Error creating another Process... Exiting\n";
exit(1);
}
// This is the child process
else if (ProcessID == 0)
{
execle("/Applications/Microsoft Office 2011", nullptr);
}
// This is the parent process
else
{
cout << "parent process\n";
}
}
int main()
{
cout << "Hello ---, what application would you like to open?\n";
string input;
cin >> input;
loadApplication(input);
return 0;
}
You don't have to use fork/exec for this. Just pass the open command to system():
#include <cstdlib>
int main() {
system("open /Applications/App\\ Store.app");
return 0;
}
Note that you will need to escape any spaces in the application name (as shown above), and specify the full name (not just the displayed name).
Here's a very closely related question.
I'm working on a (C++) program that more or less revolves around renaming files. I would like to make it so that you can select a file, right-mouse click it and select "Open With" and then select my application.
I got the context menu part figured out, I just don't know how to do the C++ part.
In other words, how to make a program (in C++) that can be opened together with a file (so by context menu or directly opening it) and process this file?
Example:
In my Windows, I associate the ".roberto" extension with "C:\Program Files\MyProgram\MyProgram.exe". So if I open a ".roberto" file, a command prompt pops up, only displaying the name of the selected file.
I hope this is clear, I am not sure how to explain this. I also had some trouble with searching on this question, so please forgive me if this has been asked before.
Thanks.
On Windows platform in MFC-based application this is done automatically by framework in InitInstance() method of your application class:
EnableShellOpen();
RegisterShellFileTypes(TRUE);
IMPORTANT: In general this functionality is framework dependent and OS speicific.
I figured it out!
Using the arguments given to main was the clue. The following program prints one line if opened directly, this line is the path of the program itself, and if opened with the 'Open with...' options it also shows the selected file.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
cout << "Argument count: " << argc << endl << endl;
for (int i = 0; i < argc; i++)
{
cout << argv[i] << endl;
}
cout << endl << endl << endl << endl;
system("pause");
return 0;
}