C++ Win32 API include <string> - c++

I'm trying without any luck to include strings in my C++ Win32 API beginner project. The code won't compile if I define a string. What's going on?
Details:
I was working in Dev C++, but now have switched to Code::Blocks using the (default?) "Gnu GCC Compiler".
Here are the code cases I have tried, all similar, with their results:
Compiles successfully:
#include <windows.h>
#include <string.h> //<string> throws "no such file or directory"
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
//...the rest works perfectly, omitted in following examples
Fails:
#include <windows.h>
#include <string.h>
// Error: "string" does not name a type
string myString;
// ...WndProc
Compiles successfully:
#include <windows.h>
#include <string.h>
using namespace std;
// ...WndProc
Fails:
#include <windows.h>
#include <string.h>
using namespace std;
// Error: "string" does not name a type
string myString;
// ...WndProc
Fails:
#include <windows.h>
#include <string.h>
// Error: expected constructor, destructor, or type conversion before "myString"
// Error: expected ',' or ';' before "myString"
std::string myString;
// ...WndProc
I asked this question a few days ago but deleted it because it seemed like a dumb question. However, it wasn't solved and now has come back to haunt me. Thanks in advance.

Does the source file have a .cpp extension? If it's .c, it will compile as C code, which probably excludes the directories containing the standard C++ headers.

#include <string.h> //<string> throws "no such file or directory"
Something is seriously broken with either your compiler installation or your use of it. Whatever comes after this, not being able to include the header for std::string is going to make it very difficult to use one.
You can install the GCC suite without C++ support, maybe that's your problem.

<string.h> contains ANSI C string macros and function declarations (see here) , not the C++ string. To use std::string, you need to do
#include <string>
(no .h)
#include <windows.h>
#include <string>
std::string myString;

string.h has only methods to handle the string. For example, strcpy, strlen etc... (http://opengroup.org/onlinepubs/007908799/xsh/string.h.html)
If you want to use std::string, you should use .
If there is no file, check that file.
Good luck :)

Related

Why does changing the order of including psapi.h gives compilation erros?(Indentifier BOOL is undefined)

I am using Visual Studio Community 2017 to code c++. When I run the following code everything works fine.
#include "pch.h"
#include<Windows.h>
#include<Psapi.h>
#include <iostream>
#include <conio.h>
int main()
{
std::cout << "Really!! How do you do it?";
_getch();
}
But if I change the order of #includes by including psapi.h before Windows.h, compiler goes badass and throws 198 errors at me, which surprisingly(maybe only to me) includes Identifier "BOOL" is undefined.
Why is this happening?
Since Psapi.h's include tree is trivial, I'm going to exemplify. Everything relies on VStudio 2015 (Community) (v14.0.25431.01 Update 3) and Windows Kits 8.1 (? funny, because v10 is there too) files (with default env vars and preprocessor definitions):
BOOL is defined in minwindef.h (#157: typedef int BOOL;)
Psapi.h only includes one file (#27: #include <winapifamily.h>)
winapifamily.h doesn't include any other file
So, when reaching Psapi.h (#87: BOOL WINAPI EnumProcesses (...), the compiler doesn't know anything about BOOL, so it complains.
Windows.h includes minwindef.h (indirectly, via windef.h), and that's why it works when you include it before Psapi.h.
Personally, I think it's a bug in Psapi.h, since it's not self contained, but there might be a good reason (that I'm not aware of) for that. Anyway, if this is indeed a bug, it wouldn't be MS's 1st one :)
#include <Windows.h>
#include <WinSock2.h>
// main present just for rigorosity's sake
int main() {
return 0;
}
to answer the question, I know this is DATED but the issues persist today. You need the following:
#include "stdafx.h"
#include <stdbool.h>
#include <Windows.h>
#include <stdlib.h>
#include <psapi.h>
After stdlib.h was included, the errors were gone.

In what header file the function `_tcscpy_s` is declared?

Visual Studio 2015. I am reading the "Windows via C\C++" book and try to use its code samples. Author writes that the "safe" functions of string have the _s suffix and are declared in the StrSafe.h header. This header are to be the last in the list of includes. In my code I included such headers:
#include <iostream>
#include <exception>
#include <string>
#include <Windows.h>
#include <strsafe.h>
But I have a problem:
// IDE doesn't see the _tcscpy_s function
errno_t result = _tcscpy_s(szBuffer, _countof(szBuffer), TEXT("0123456789"));
I looked for info about the _tcscpy_s function, but I didn't see info about its header file (I expected that it is strsafe.h).
How can I fix it?
Just like any other "Generic Text" string function version, the _tcscpy_s() function is declared in TCHAR.H (as mentioned in the documentation).
Add #include <tchar.h> to your code.

wcscpy_s not working for Win32 Combo Box

I'm trying to create a combo box in Win32 by following this msdn tutorial.
When I implement step 2 and try to compile, I get the following error:
error: 'wcscpy_s' was not declared in this scope
wcscpy_s(A, sizeof(A) / sizeof(TCHAR), (TCHAR*)Planets[k]);
I've included the following header files, hoping to solve this issue:
#include <string.h>
#include <wchar.h>
#include <windows.h>
#include <CommCtrl.h>
#include <math.h>
#include <objbase.h>
Can someone help me understand why I'm getting this error? Thanks in advance.
From cpp-reference
As with all bounds-checked functions, wcscpy_s is only guaranteed to be available if STDC_LIB_EXT1 is defined by the implementation and if the user defines STDC_WANT_LIB_EXT1 to the integer constant 1 before including wchar.h.

VC++ 10.0 express gdi+ GdiPlusPen.h header

When i include GdiPlus.h,Pen class is undefined.
But GdiPlus.h includes GdiPlusPen.h
...
#include "GdiplusImageAttributes.h"
#include "GdiplusMatrix.h"
#include "GdiplusBrush.h"
#include "GdiplusPen.h"
#include "GdiplusStringFormat.h"
#include "GdiplusPath.h"
...
When i include GdiPlusPen.h myself, it works. Can i use it safely?
Question: is this because of my VC++ being express install?
8 days left until activation prompt :(
Anyone having same problem?
Windows XP sp-3, pentium-m centrino.
No, #including GdiplusPen.h directly isn't correct. The gdiplus classes live in a namespace named "Gdiplus". Either use that namespace explicitly (like Gdiplus::Pen) or make it look like this in your .cpp file:
#include <gdiplus.h>
using namespace Gdiplus;

SHGetSpecialFolderPath() Not Declared in This Scope

I am not able to compile my program SHGetSpecialFolderPath() not being declared in the scope of the program, while the correct header is being included (according to MSDN)
http://msdn.microsoft.com/en-us/library/bb762204(v=vs.85).aspx
Here are the headers for my project:
#include <iostream>
#include <iostream>
#include <windows.h>
#include <algorithm>
#include <vector>
#include <fstream>
#include <direct.h>
#include <shlobj.h>
With error:
C:\Users\user\Documents\getAppData\main.cpp|31|error: `SHGetSpecialFolderPath' was not declared in this scope
with shlobj.h being the header with the declaration in it.
Any ideas why the compiler is throwing the error? Here is how I am calling the function:
char appData[MAX_PATH];
SHGetSpecialFolderPath( NULL
,appData
,CSIDL_LOCAL_APPDATA
,1 );
cout << appData << endl;
Thanks!
From the MSDN page:
The Microsoft Internet Explorer 4.0
Desktop Update must be installed for
this function to be available.
With Windows 2000, this function is
superseded by ShGetFolderPath. You can
use this function on earlier systems
by including the redistributable DLL,
ShFolder.dll.
Perhaps this is your problem?