我在 SQLGetPrivateProfileString
中使用从 odbcinst.ini
(注册表)读取 DSN 参数。
但是如果我在 DSN 中使用 UID='König'
这样的字符并调用
SQLGetPrivateProfileString(DSN, INI_USERNAME, "", temp, sizeof(temp), ODBC_INI)
变量 temp='K?nig'
odbc自然无法连接到数据源。 如何读取正确的值?
最佳答案
需要调用Unicode版本的函数,即SQLGetPrivateProfileStringW
。
参见 this Microsoft doc
If the application is compiled with the _UNICODE #define the ODBC header file will map undecorated function calls to the Unicode version.
You can recompile an application as a Unicode application in one of two ways:
- Include the Unicode #define contained in the Sqlucode.h header file in the application.
- Compile the application with the compiler's Unicode option. (This option will be different for different compilers.)
参见odbcinst.h:
#ifdef UNICODE
...
#define SQLGetPrivateProfileString SQLGetPrivateProfileStringW
...
其中 SQLGetPrivateProfileStringW
声明为:
int INSTAPI SQLGetPrivateProfileStringW
(
_In_opt_ LPCWSTR lpszSection,
_In_opt_ LPCWSTR lpszEntry,
_In_opt_ LPCWSTR lpszDefault,
_Out_writes_opt_(cchRetBuffer) LPWSTR lpszRetBuffer,
int cchRetBuffer,
_In_opt_ LPCWSTR lpszFilename
);
函数的 Unicode 版本接收 LPCWSTR
和 LPWSTR
,它们是指向 16 位 Unicode 字符的字符串的指针。任何其他函数将接收这些值也需要是 Unicode 版本。
关于c++ - SQLGetPrivateProfileString 错误地读取 Unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55721692/