Joan P.S
Joan P.S

Reputation: 1573

Usage of InternetGetConnectedStateEx

I'm trying to use InternetGetConnectedStateEx but I'm not able to retrieve lpszConnectionName. If I initialise it as 0 I don't get any value, but if I initialised with _T("hol"); I get an access violation

    DWORD dwFlags;
    LPTSTR lpszConnectionName = _T("hol");
    DWORD dwNameLen = 3;

    if (InternetGetConnectedStateEx(&dwFlags, lpszConnectionName, dwNameLen, NULL))
    {
        printf("Connected to internet");
    }
    else {
        printf("not connected");
    }

Thanks!

Upvotes: 1

Views: 476

Answers (1)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262939

The documentation says (emphasis mine):

lpszConnectionName [out]

Pointer to a string value that receives the connection name.

That is an output parameter, that will be filled with the connection name if there is one active. Specifying the contents of this parameter yourself is a mistake, having it point to a string literal doubly so (since modifying it will trigger undefined behavior).

The appropriate way to invoke the function would be something like:

DWORD dwFlags;
TCHAR lpszConnectionName[512];

if (InternetGetConnectedStateEx(&dwFlags, lpszConnectionName,
    _countof(lpszConnectionName), NULL)) {
    // ...
} else {
    // ...
}

Upvotes: 3

Related Questions