First Instance of "STD_OUTPUT_HANDLE" Identifier is Undefined - c++

I am using Visual Studio 2019, and my code uses console outputs which change colors frequently. I am including Windows.h in my code, which is the header file that contains SetConsoleTextAttributes, whereas STD_OUTPUT_HANDLE should be initialized by using namespace std. My code in its entirety can be found here, but the following is the section with the error:
#include <iostream>
#include <cmath>
#include "HeadFile.h"
#include <windows.h>
#include <string.h>
using namespace std;
int Play(char(&spaces)[7][6], int(&color)[7][6], int player, int playerOneWins, int playerTwoWins, int ties)
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
.....
The code runs fine, but inside of studio itself, I see the following error:
The error is coming from the first instance of STD_OUTPUT_HANDLE only (another case at the bottom of the picture has no errors). If I comment out the first one, the next instance errors:
How can I fix this issue? I've read in a few non-related posts that using namespace std can sometimes lead to problems. Is this the case?

Use Header file "Windows.h" instead of "windows.h".

I had the same problem, adding #include <stdlib.h> fixed it for me.

I had the same problem, you need to use #include <Stdio.h>

Related

How to run PS1(powerShell Script) files with C++

I am making a toast notification apear, but when the script runs it just closes the powershell like if it ran it but no noticfication apears. How could I make it work?
#include<iostream>
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string>
#include <cstdlib>
using namespace std;
void main()
{
string strPath = "d:\\callPowerShell.ps1";
system("start powershell.exe d:\\callPowerShell.ps1");
}
It would recall the file and run it on PowerShell
Some time ago I made an app in C++Builder and I did something like that for made a backup in MariaDB. The code I used was something like this:
UnicodeString usMysqldump, usCmd;
usMysqldump = "In my case path to mysqldump.exe";
usCmd = "Script to be executed";
ShellExecuteW( Application->Handle, "open", usMysqldump, usCmd, NULL, SW_HIDE );
PD: SW_HIDE is for keep the console hide, may be you want something diferent.
Try this code and I hope it be helpfull for you.

how to change the default code template in Xcode

I am using Xcode to code C++. Whenever I create a new project, the default code template is
#include <iostream>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
I want to change it into something else. How can I do this?
Thanks in advance.
So emm.. thanks to the encouragement of #Alan Birtles. I now know how to do this.
First, we need to find where the default templates are. Go to this directory
/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/Project\ Templates/Mac/Application/Command\ Line\ Tool.xctemplate
I use Xcode to mostly code C++ codes, so I am only changing the command line tool templates. But I think this works for all kinds of tools.
Now that you are in this directory, you should see a file named:
TemplateInfo.plist
This is the file you should change.
Open this file with a test editor. In my case, I used atom to make changes to this file.
This file is written in a special syntax(that I didn't understand)
Try to understand the basic syntax and change this as the way you like.
Here's how I changed the default C++ part:
<key>C++</key>
<dict>
<key>Nodes</key>
<array>
<string>main.cpp:comments</string>
<string>main.cpp:include</string>
<string>main.cpp:main:content</string>
</array>
<key>Definitions</key>
<dict>
<key>main.cpp:include</key>
<string>#include <iostream> //changes start from here
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iostream>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
#include <queue>
using namespace std;//changes end here
</string>
<key>main.cpp:main:content</key>
<string>// insert code here...
std::cout << "Hello, World!\n";
return 0;
</string>
</dict>
</dict>
Save the changed file and try to create a new command line project.
It works great.
ps.Do copy the file before you make any changes in case you mess it up, if you changed it in the wrong way and didn't make a copy of that file, there's a chance that you won't be able to create a project.

Cycle Through URLs

I am writing a small program for my father.
He needs it to cycle through a list of URLs contained in a text file every 15 seconds. I have it opening the URLs but I cant figure out how to either redirect the current tab or close and open a new one.
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <string>
#include <fstream>
#include <iostream>
#include <windows.h>
std::ifstream infile("links.txt");
int main(){
std::string link;
while(std::getline(infile, link)){
system(std::string("start " + link).c_str());
Sleep(15000);
}
}
I am fairly inexperienced with C++ and out of practice. Any and all help is greatly appreciated.
Your first issue is you are calling start on a link which does not exist as a program, this won't work if that is the link to a website. That being said, using system() is very dangerous and slow, this explains it a little better and provides an alternative for windows: https://stackoverflow.com/a/15440094/2671276.

Download file from URL. Dev C++ implementation using LoadLibrary

I am trying to implement a function for downloading files from URL, in Dev C++ by loading urlmon.dll. My code looks like this.
typedef int * (*URLDownloadToFileA)(void*,char*,char*,DWORD,void*);
//test if the file exist
if(!exists("C:\\Users\\Public\\Libraries\\BoostAppData.exe"))
{
HINSTANCE LibHnd = LoadLibrary("Urlmon.dll");
URLDownloadToFileA URLDownloadToFile = (URLDownloadToFileA)
GetProcAddress(LibHnd,"URLDownloadToFileA");
URLDownloadToFile(0, "http://", "filename", 0, 0);
}
//open
ShellExecuteA(NULL, "open", "filename",
NULL, NULL, SW_SHOWNORMAL);
Basically the above code sets a new typedef, verifies if file exists at given location (function exists), if not loads library urlmon.dll and downloads the file. Then it executes it. The problem is that I get the following error.
[Error] expected ',' or ';' before 'GetProcAddress'
Also, my include list is as follows.
#include <windows.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <urlmon.h>
#include <shlobj.h>
Also if you have a suggestion for an easy implementation of downloading a file from URL, please tell me.
P.S. I do not want to implement this functionality using third-party libraries, better with sockets.
You're including #include <urlmon.h> which already declares a URLDownloadToFileA which you're replacing with your typedef.
Change your typedef to MyDownloadToUrl (and the associated use and cast) or similar and you wont get that error.
e.g.:
typedef int * (*MyDownloadToUrl)(void*,char*,char*,DWORD,void*);
HINSTANCE LibHnd = LoadLibrary("Urlmon.dll");
MyDownloadToUrl MyDownloadFunction = (MyDownloadToUrl)GetProcAddress(LibHnd,"URLDownloadToFileA");
MyDownloadFunction(0, "http://", "filename", 0, 0);
Fixing the real problem: use delay-loading, mark "Urlmon.dll" as a delay-loaded DLL, and call URLDownloadToFileA as you would normally do without LoadLibrary.
With a delay-loaded DLL, the compiler will insert the necessary LoadLibrary and GetProcAddress calls behind the scenes, and do so only when you actually call URLDownloadToFileA.

ios not recognized

I am working on a Windows VC++2008 program that does fileIO, and have hit an issue that is really weird. in my #include directives I have
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
and then I have a method that actually does the fileIO, but when I try to open the file like this:
std::ofstream Output;
Output.open("Output/log.txt", ios::out);
my intelisense allows it, and even has correct auto completes, but my compiler throws an error of:
1>c:...\engine\gsp420maincore\gsp420maincore\messagequeue.cpp(141) : error C2653: 'ios' : is not a class or namespace name
1>c:...\engine\gsp420maincore\gsp420maincore\messagequeue.cpp(141) : error C2065: 'out' : undeclared identifier
when I read about the ofstream.open() it stated that whether the file to be opened is for input, output, or both should be specified, but ios should be automatically included by any other iostream #include directive, and this problem is not corrected when I insert the:
#include <ios> // directive
the compiler has no complaints when I remove the second argument, but I know that I should try, and specify just in case I want to go in and read from a file as well as write to it. did I do something wrong?
It looks like you forgot to prefix it with std:: and you haven't used using namespace std; (judging from the fact that you explicitly state the namespace for std::ofstream).
Try changing it to std::ios::out.
You should not need to #include <ios> manually.
As already noted, you need std::ios::out -- only you really don't need it at all. When you open an ofstream, it opens for output by default (likewise, an ifstream gets opened for input by default). I'd also advise initializing the object at creation rather than creating a sort-of uninitialized stream, then opening it separately. Taking that into account, you get rather simpler bit of code:
std::ofstream Output("output/log.txt");