我正在使用visual studio 2015,我想编写可以在Unicode项目和Multi-Byte项目中使用的C静态库,我是如何做到的?
例如,我有这个代码:
namespace Reg
{
LONG WINAPI CreateKey(
_In_ HKEY hKey,
_In_ LPCTSTR lpSubKey,
_In_ REGSAM samDesired,
_Out_ PHKEY phkResult
)
{
return RegCreateKeyEx(hKey,
lpSubKey,
0, NULL,
REG_OPTION_NON_VOLATILE,
samDesired,
NULL,
phkResult,
NULL);
}
}
最佳答案 就像Raymond Chen在评论中建议的那样,你可以使用两个独立的重载函数 – 一个用于Ansi,一个用于Unicode:
namespace Reg
{
LONG WINAPI CreateKey(
_In_ HKEY hKey,
_In_ LPCSTR lpSubKey,
_In_ REGSAM samDesired,
_Out_ PHKEY phkResult
)
{
return RegCreateKeyExA(hKey,
lpSubKey,
0, NULL,
REG_OPTION_NON_VOLATILE,
samDesired,
NULL,
phkResult,
NULL);
}
LONG WINAPI CreateKey(
_In_ HKEY hKey,
_In_ LPCWSTR lpSubKey,
_In_ REGSAM samDesired,
_Out_ PHKEY phkResult
)
{
return RegCreateKeyExW(hKey,
lpSubKey,
0, NULL,
REG_OPTION_NON_VOLATILE,
samDesired,
NULL,
phkResult,
NULL);
}
}
或者,就像rubenvb建议的那样,完全忘记Ansi函数,只关注Unicode本身:
namespace Reg
{
LONG WINAPI CreateKey(
_In_ HKEY hKey,
_In_ LPCWSTR lpSubKey,
_In_ REGSAM samDesired,
_Out_ PHKEY phkResult
)
{
return RegCreateKeyExW(hKey,
lpSubKey,
0, NULL,
REG_OPTION_NON_VOLATILE,
samDesired,
NULL,
phkResult,
NULL);
}
}