我如何支持静态库(.lib)中的Unicode和多字节字符集?

时间:2016-09-27 10:44:07

标签: c++ visual-studio unicode multibyte

我正在使用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);
    }
}

2 个答案:

答案 0 :(得分:1)

你可以像通常用于Win32函数一样:

D:\AndroidSelfTrainingProject\CustomBuildIdDemo>gradle build
To honour the JVM settings for this build a new JVM will be forked. Please consider using the daemon
: https://docs.gradle.org/2.10/userguide/gradle_daemon.html.

To run dex in process, the Gradle daemon needs a larger heap.
It currently has approximately 910 MB.
For faster builds, increase the maximum heap size for the Gradle daemon to more than 2048 MB.
To do this set org.gradle.jvmargs=-Xmx2048M in the project gradle.properties.
For more information see https://docs.gradle.org/current/userguide/build_environment.html

答案 1 :(得分:1)

就像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);
    }
}