一些 api 需要字符数。
// Why did they choose cch in these functions.
HRESULT StringCchCopyW(
__out LPWSTR pszDest,
__in size_t cchDest,
__in LPCWSTR pszSrc
);
errno_t wcscpy_s(
wchar_t *strDestination,
size_t numberOfElements,
const wchar_t *strSource
);
DWORD WINAPI GetCurrentDirectoryW(
__in DWORD nBufferLength, // Count of Chars
__out LPWSTR lpBuffer
);
有些 apis 需要字节数。
// What do you prefer cch vs cb function.
// Do cch functions almost useful?
HRESULT StringCbCopyW(
__out LPWSTR pszDest,
__in size_t cbDest,
__in LPCWSTR pszSrc
);
BOOL WINAPI ReadFile(
__in HANDLE hFile,
__out LPVOID lpBuffer,
__in DWORD nNumberOfBytesToRead,
__out_opt LPDWORD lpNumberOfBytesRead,
__inout_opt LPOVERLAPPED lpOverlapped
);
// Why did they choose cb in these structures.
// Because there are some apis uses cb, I always should see MSDN.
typedef struct _LSA_UNICODE_STRING {
USHORT Length; // Count of bytes.
USHORT MaximumLength; // Count of bytes.
PWSTR Buffer;
} UNICODE_STRING, *PUNICODE_STRING;
typedef struct _FILE_RENAME_INFO {
BOOL ReplaceIfExists;
HANDLE RootDirectory;
DWORD FileNameLength; // Count of bytes.
WCHAR FileName[1];
} FILE_RENAME_INFO, *PFILE_RENAME_INFO;
在设计函数或数据结构时,如何确定 cb 或 cch?为什么?
要为调用者设计更好的 api,我应该知道什么?