"URLDownloadToFile" not doing anything at all - c++

I'm making a program where it checks if the user has the required JRE installed so they are able to run jar files. And, if they don't have it, then it downloads and runs the file for them. However, I keep getting the same error no matter what type of method I find online and apply. It keeps telling me that its unable to find or open the exe. And when I check the download location, it doesn't download anything at all.
Code:
HRESULT hr = URLDownloadToFile(NULL, _T("https://download.oracle.com/otn/java/jdk/8u291-b10/d7fc238d0cbf4b0dac67be84580cfb4b/jre-8u291-windows-x64.exe"), _T( "c:\\jre-8u291-windows-x64.exe"), 0, NULL);
cout << "Download Complete" << endl;
cout << "Now installing" << endl;
system("start c:\\jre-8u291-windows-x64.exe");
Includes:
#include <iostream>
#include <atlstr.h>
#include <Windows.h>
#include <urlmon.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "urlmon.lib")
using namespace std;
Output:

Related

Electric Light Bulb symbol (Unicode) output to terminalby C++

I'm trying to output the symbol of Electric Light Bulb with code U+1F4A1 to Windows Terminal (experiment with Unicode). I can't realize how to do that. I tried to use wchar_t, wcout, to change console output code page, and with no result. Who made it. please tell how to do that.
#include <uchar.h>
#include <iostream>
#include <cstdlib>
#include <clocale>
#include "Windows.h"
#include <io.h>
#include <fcntl.h>
int main() {
SetConsoleCP(12000);
SetConsoleOutputCP(12000);
/*Alternative*/
system("chcp 65001");
std::cout << u8"\u1F4A1" << std::endl;
return 0;
}

"endl" causes "C1001" error

My code is a basic HelloWorld but fails to compile when I use cout<<endl.
I'm using Microsoft visual studio fresh download and created a console application for my first test project.
// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>
using namespace std;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
return 0;
}
It generates a
"C1001" at line 1.
Replacing "endl" with ""\n"" works though.
You don't need the precompiled header #include <stdafx.h> so you can safely get rid of it. Also get rid of using namespace std; because it pollutes the global namespace. Try something like this. There's no reason it shouldn't work.
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
return 0;
}
In Visual Studio you can disable use of the precompiled header in the project settings.
I do not see what the problem is. Both options compile and execute for me.
RexTester cppOnline
// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>
using namespace std;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
cout << "lets see: " << s << "\n";
return 0;
}
So idk what was causing the error but it was fixed after pasting imports to the "stdafx.h" header file and then delete them...

i am using voce my programme compiles succesfully but i am getting error occured initializing vm can not find native libraries

this is my code i am using vs2013. i have set dependencies and all the linkers but still my code compiles correctly but when cosole opens it says error initializing vm, unable to find native libraries
#include <iostream>
#include <voce.h>
#include <Windows.h>
#include <string>
#pragma comment(lib, "jvm.lib")
#pragma comment(lib, "jawt.lib")
using namespace std;
int main()
{
init("C:\\voce-0.9.1\\lib\\", false, true, "C:\\voce - 0.9.1\\lib\\gram", "digits.gram");`
string s;
// Speech recognition in C++
while (voce::getRecognizerQueueSize() > 0)
{
s = voce::popRecognizedString();
cout << "You said: " << s << endl;
}
return 0;
}

Error loading .wav file C++

I tried to load a .wav file(afschieten.wav), but I cant load it. I followed the tutorial of lazyfoo: http://lazyfoo.net/SDL_tutorials/lesson11/
The code:
I got this on top of file:
#include <stdlib.h>
#include "GL/glut.h"
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <time.h>
#include <iostream>
the function I call from main:
void load_files(){
//Load sound effect
fire = Mix_LoadWAV( "afschieten.wav" );
if(fire != NULL)
std::cout << "Sound effect afschieten.wav loaded succesfully" << std::endl;
else
std::cout << "Sound effect afschieten.wav failed loading" << std::endl;
}//load_files
What am I doing wrong? I get no errors when compiling. It says sound effect afschieten.wav failed loading.
Check the path of the wav file: for proper loading, you should put it in the same directory of the executable, or use an absolute path.

How do you make a program sleep in C++ on Win 32?

How does one "pause" a program in C++ on Win 32, and what libraries must be included?
#include <windows.h>
Sleep(number of milliseconds);
Or if you want to pause your program while waiting for another program, use WaitForSingleObject.
In C++11, you can do this with standard library facilities:
#include <chrono>
#include <thread>
std::this_thread::sleep_for(std::chrono::milliseconds(x));
If you are using boost, you can use the thread::sleep function:
#include <boost/thread/thread.hpp>
boost::system_time time = boost::get_system_time();
time += boost::posix_time::seconds(1);
boost::thread::sleep(time);
Otherwise, you are going to have to use the win32 api:
#include <windows.h>
Sleep(1000);
And, apparently, C++0x includes this:
#include <thread>
std::this_thread::sleep_for(chrono::seconds(1));
Please note that the code above was tested on Code::Blocks 12.11 and Visual Studio 2012
on Windows 7.
For forcing your programme stop or wait, you have several options :
sleep(unsigned int)
The value has to be a positive integer in millisecond.
That means that if you want your programme wait for 2 second, enter 2000.
Here's an example :
#include <iostream> //for using cout
#include <stdlib.h> //for using the function sleep
using namespace std; //for using cout
int main(void)
{
cout << "test" << endl;
sleep(5000); //make the programme waiting for 5 secondes
cout << "test" << endl;
sleep(2000); // wait for 2 secondes before closing
return 0;
}
If you wait too long, that probably means the parameter is in second. So change it like that :
sleep(5);
For those who get error message or problem using sleep try to replace it by _sleep or Sleep especially on Code::Bloks.
And if you still getting probleme, try to add of one this library on the biggining of the code.
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
system("PAUSE")
A simple "Hello world" programme on windows console application would probably close before you can see anything. That the case where you can use system("Pause").
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
system("PAUSE");
return 0;
}
If you get the message "error: 'system' was not declared in this scope" just add
the following line at the biggining of the code :
#include <cstdlib>
cin.ignore()
The same result can be reached by using cin.ignore() :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.ignore();
return 0;
}
cin.get()
example :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.get();
return 0;
}
getch()
Just don't forget to add the library conio.h :
#include <iostream>
#include <conio.h> //for using the function getch()
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
getch();
return 0;
}
You can have message telling you to use _getch() insted of getch
If you wish for the program to stay responsive while "paused", you need to use a timer event.
It depends on what type of program you are writing.
A console app can just call Sleep. A GUI app probably does not want to do this, as all the menus and widgets will go insensitive, and the app won't redraw itself during this period. Instead you need to do something like set yourself up a timer with a callback when it expires.
Dont use a sleep function in your GUI if it is not provided by the framework you are working with. This could create referencing problems to data (specially in a thread that is not the main thread). This could freeze you GUI. Its not just a question of sleeping for a short time , use waitmutexes if you need to do that.