Windows'GetPrivateProfileXXX函数(用于处理INI文件)有一些关于处理缓冲区长度的奇怪规则。GetPrivateProfileString - 缓冲区长度
GetPrivateProfileString的文档状态:
如果[..]提供的目标缓冲区太小,无法容纳请求字符串,字符串被截断,并后跟一个空字符,并且返回值等于nSize减1。
我读到这,我意识到,这种行为使得它不可能两个场景之间的区别在代码:
- 当值字符串的长度正好等于n大小 - 1
- 当nSize值(即缓冲区)太小。
我想我会尝试:
我有这样的INI文件:
[Bar]
foo=123456
我叫GetPrivateProfileString用这些参数作为测试:
// Test 1. The buffer is big enough for the string (16 character buffer).
BYTE* buffer1 = (BYTE*)calloc(16, 2); // using 2-byte characters ("Unicode")
DWORD result1 = GetPrivateProfileString(L"Bar", L"foo", NULL, buffer, 16, fileName);
// result1 is 6
// buffer1 is { 49, 0, 50, 0, 51, 0, 52, 0, 53, 0, 54, 0, 0, 0, 0, 0, ... , 0, 0 }
// Test 2. The buffer is exactly sufficient to hold the value and the trailing null (7 characters).
BYTE* buffer2 = (BYTE*)calloc(7, 2);
DWORD result2 = GetPrivateProfileString(L"Bar", L"foo", NULL, buffer, 7, fileName);
// result2 is 6. This is equal to 7-1.
// buffer2 is { 49, 0, 50, 0, 51, 0, 52, 0, 53, 0, 54, 0, 0, 0 }
// Test 3. The buffer is insufficient to hold the value and the trailing null (6 characters).
BYTE* buffer3 = (BYTE*)calloc(6, 2);
DWORD result3 = GetPrivateProfileString(L"Bar", L"foo", NULL, buffer, 6, fileName);
// result3 is 5. This is equal to 6-1.
// buffer3 is { 49, 0, 50, 0, 51, 0, 52, 0, 53, 0, 0, 0 }
调用此代码的程序无法确定实际的键值是否确实是5个字符的长度,或甚至6个,如最后两个CAS中的值es结果等于nSize-1。
唯一的解决方案是每当检查结果== nSize-1时,检查函数是否具有较大的缓冲区,但在缓冲区正好是合适的尺码。
没有更好的方法吗?
这应该是C或C++代码吗? –