A piece of code to check the Internet connection:

#include <Wininet.h> #pragma comment(lib, "wininet.lib") bool CheckConnection(const std::string InternetLink) { if (!InternetCheckConnectionW(InternetLink.c_str, FLAG_ICC_FORCE_CONNECTION, 0)) return 0; return 1; } 

I get an error: there is no suitable conversion function from "const std :: string" in "LPCWSTR

Then I wanted to call her like this:

 is(!CheckConnection("http://google.com") {...} 
  • You are making something up. The code given to you cannot give such an error. No need to invent, bring the real code. - AnT
  • @AnT, maybe. The questioner uses the W variant of the function, which accepts not char* , but wchar_t* . - ߊߚߤߘ
  • @Arhad: The questioner is missing () after c_str . This already eliminates such an error. And this suggests that the code that he compiles is not the code he brought here. Two return successors in a row also hint that there is some kind of BNOP, and not a real code. - AnT
  • @AnT, Example of checking the Internet connection took from the left board) for the test to check. - GooliveR
  • @GooliveR: This is great. But you compiled not the code that resulted here. The code that you gave here, in principle, cannot generate such an error. It is recommended to give the real code here, not “fantasies based on motives” - AnT

3 answers 3

LPCWSTR is a Unicode string. In C ++, its counterpart is std::wstring (note the prefix w ).

The std::string is a single-byte character string, and you can convert it to a maximum in LPCSTR .

Accordingly, you do not need to use Unicode InternetCheckConnectionW() , but single-byte character InternetCheckConnectionA() .

  • Thanks helped - GooliveR

To avoid having to do extra string conversions, you can use this simple macro:

 #if defined(UNICODE) #define _tstring wstring #else #define _tstring string #endif 

Now, in the project, replace all std :: string with std :: _ tstring . And you no longer have to convert strings out of the blue. There are other surprises in STL for winapi (for example to_string / to_wstring). This solution is suitable for them.

The same goes for A / W, @AnT is right: you don’t need to explicitly call InternetCheckConnection A or InternetCheckConnection W , unless you have specific requirements (and judging by the code, they are not). Call InternetCheckConnection without postfixes.

And do not forget about the text assistant in windows - macro TEXT or _T . The correct CheckConnection call is:

 is(!CheckConnection(TEXT("http://google.com")) {...} 
  • one
    I will know now, thanks for the advice. I will use in the future. - GooliveR

look here: Converting std :: string to std :: wstring

and it is better to pass to the function probably not const std :: string, but const std :: string &