c++ - SQLGetPrivateProfileString 错误读取 Unicode 字符

标签 c++ c odbc

我在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.

this one

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 版本接收 LPCWSTRLPWSTR,它们是指向 16 位 Unicode 字符字符串的指针。无论其他函数如何接收这些值,也都需要是 Unicode 版本。

关于c++ - SQLGetPrivateProfileString 错误读取 Unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55721692/

相关文章:

java - 编程中实例的含义是什么?

c++ - 构建 Qt Slog 时的错误信息计算

c++ - 将字符串拆分为单个单词并将它们放入 vector 中

c++ - "NUL"字符 ("\0") 字节是分开的还是分配给带有字符串的字符数组?

r - 如何在 OSX 10.10.2 上将 R 与 PostgreSQL 连接?

mysql - 将 ODBC Canonical 日期转换为二进制,或使用 ODBC Canonical 函数

c++ - constexpr + 多维数组失败

c - scanf 之后 fgets 不起作用

c - 通过将字符串类型转换为c中的int指针来获取字符串的长度

sql - 我如何连接到 ODBC Oracle 数据库?