Search code examples
cwinapicharwchar-t

How can a string parameter be of type byte * and of type wchar_t * at the same time?


I am currently looking into the WinHttp API and have come across the WinHttpConnect() function which is described here. The parameter description is quite worrying for me. The aforementioned page first gives the function prototype as

WINHTTPAPI HINTERNET WinHttpConnect(
  [in] HINTERNET     hSession,
  [in] LPCWSTR       pswzServerName,
  [in] INTERNET_PORT nServerPort,
  [in] DWORD         dwReserved
);

and continues with the parameter description. The second input parameter is explained as follows (leaving away the last two irrelevant sentences):

[in] pswzServerName
Pointer to a null-terminated string that contains the host name of an HTTP server. Alternately, the string can contain the IP address of the site in ASCII, for example, 10.0.1.45. [...]

That's the part I am struggling with. In C, a string consists either of wide characters (as the parameter type LPCWSTR implies) or of bytes (as the wording "IP address [...] in ASCII" implies). It is not clear to me how the function can distinguish whether the bytes at the memory location the parameter points to are bytes making up a narrow string or are wide characters making up a wide string.

Can somebody shed some light on this?


Solution

  • ASCII can refer to two things.

    It can refer to the ASCII encoding, in which case it does imply bytes and thus char *.

    But it can also refer to the ASCII character set, meaning the symbols that can be encoded using ASCII.

    The second is closer to what is meant here. "the IP address of the site in ASCII" means "the text representation of the IP address of the site". In other words, you are to expected to provide the text form (e.g. L"10.0.1.45") of the address rather than its packed form ("\x0A\x00\x01\x2D") or its numerical form (0x0A00012D).