Finding External IP Address

Hello, everyone.

I have some code I copied from another article somewhere for a C++ program that will hit whatismyip.com's automation file to find out my external IP.

What I need to know is WHY/HOW to get this to compile properly in Visual Studio 2010 Ultimate without setting the Project Properties: Character Set to "Multi-byte" or "Not Set."

I know there is another post around here about it, but I didn't really understand the explanation, and that post is closed. Besides, it wasn't specifically regarding my code.

Anyway, when I compile this code, I get the "char to LPCWSTR conversion" error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <windows.h> 
#include <wininet.h> 
#include <iostream>
 
#pragma comment(lib, "wininet")
 
int main(int argc, char* argv[])
{
    HINTERNET hInternet, hFile;
    DWORD rSize;
    char buffer[47];

    hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    hFile = InternetOpenUrl(hInternet, "http://automation.whatismyip.com/n09230945.asp", NULL, 0, INTERNET_FLAG_RELOAD, 0);
    InternetReadFile(hFile, &buffer, sizeof(buffer), &rSize);
    buffer[rSize] = '\0';

    InternetCloseHandle(hFile);
    InternetCloseHandle(hInternet);

    std::cout << "Your IP Address: " << buffer << "\n";
    system("pause");
    return 0;
}


Note: Setting the Character Set to Multi-byte does allow VS to compile, but I want to know exactly what I can do to this code to bypass that altogether. I know it has to do with TCHARS, but that's it.

Thanks for any help!
Use InternetOpenUrlA or add a L in front of url strîng if you use UNICODE.
First solution works always.
I edited my code to the following and set VS back to Unicode. It compiles just fine, but it now displays a memory address, I guess, instead of the IP Address.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <windows.h> 
#include <wininet.h> 
#include <iostream>
 
#pragma comment(lib, "wininet")
 
int main(int argc, TCHAR* argv[])
{
    HINTERNET hInternet, hFile;
    DWORD rSize;
    TCHAR buffer[47];

    hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    hFile = InternetOpenUrl(hInternet, TEXT("http://automation.whatismyip.com/n09230945.asp"), NULL, 0, INTERNET_FLAG_RELOAD, 0);
    InternetReadFile(hFile, &buffer, sizeof(buffer), &rSize);
    buffer[rSize] = '\0';

    InternetCloseHandle(hFile);
    InternetCloseHandle(hInternet);

    std::cout << "Your IP Address: " << buffer << "\n";
    system("pause");
    return 0;
}


Any other ideas? I know I'm on the right track.
Use InternetOpenUrlA or add a L in front of url strîng if you use UNICODE.
First solution works always.


Can you please give me an example? I'm not sure where the L would go. Where would I put the InternetOpenUrlA?

I am new to C++, and I know nothing of Windows programming yet.
Oh okay. I had to change back the TCHARs to char for InternetOpenUrlA to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <windows.h> 
#include <wininet.h> 
#include <iostream>
 
#pragma comment(lib, "wininet")
 
int main(int argc, char* argv[])
{
    HINTERNET hInternet, hFile;
    DWORD rSize;
    char buffer[47];

    hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    hFile = InternetOpenUrlA(hInternet, "http://automation.whatismyip.com/n09230945.asp", NULL, 0, INTERNET_FLAG_RELOAD, 0);
    InternetReadFile(hFile, &buffer, sizeof(buffer), &rSize);
    buffer[rSize] = '\0';

    InternetCloseHandle(hFile);
    InternetCloseHandle(hInternet);

    std::cout << "Your IP Address: " << buffer << "\n";
    system("pause");
    return 0;
}


Thanks so much for the assistance! Boy, what a difference the 'A' can make. Haha!

Edit: The L in front of the url works, too. Thanks so much! Haha, why is that so simple?
Last edited on
Enclosing url string in TEXT() macro will work too, unicode or not, without 'A'.
Include tchar.h header.

Do not alter InternetReadFile as in your attempt, that always takes char pointer.
Last edited on
Topic archived. No new replies allowed.