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/

相关文章:

sql-server - 通过 ODBC 的 Microsoft SQL Server 统计

sql-server - 使用 Ruby、ODBC 和 FreeTDS 从 Mac 连接到 MS SQL Server 2005

mysql - SQL Server 2008 链接 MySQL 服务器慢

c++ - 实现 "weakness table"(通常在游戏中)的最简单方法是什么?

c++ - 理解指针的问题

c++ - 将 UNIX makefile 测试参数转换为 msbuild for vs2010, C++

c - C中的动态 double 组

c - 在 C 中“动态”使用变量?

c++ - 在编译时确定这是否是只读的

c - 指向连续二维数组的指针