c - 如何使用 wchar_t* 在 C 中提取和附加路径

标签 c winapi unicode

我想从 GetModuleFileNameW 中提取路径,然后将 "\hello.dll" 附加到它(不使 "\\\\")。怎么做? (我不擅长 Unicode 函数)

最佳答案

假设您确实正在使用路径,请使用 PathAppendW功能。请注意,您可以通过附加 "hello.dll" 来完成此操作 - 如果 PathAppendW 需要,将添加反斜杠。

或者,您可以非常轻松地编写自己的函数。这是我在 5 分钟内用 std::wstrings

拼凑而成的示例
std::wstring PathAppend(const std::wstring& lhs, const std::wstring& rhs)
{
    if (lhs.empty())
    {
        return rhs;
    }
    else if (rhs.empty())
    {
        return lhs;
    }
    std::wstring result(lhs);
    if (*lhs.rbegin() == L'\\')
    {
        result.append(rhs.begin() + (rhs[0] == L'\\'), rhs.end());
    }
    else
    {
        if (rhs[0] != L'\\')
        {
            result.push_back(L'\\');
        }
        result.append(rhs);
    }
    return result;
}

关于c - 如何使用 wchar_t* 在 C 中提取和附加路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7264482/

相关文章:

c++ - 准确设置 winsock 选择超时

c - 将控制台所有权返回给父进程

c++ - C++ 中类型错误的参数

Python 列表返回一个 unicode 列表

python - unicodedata.unidata_version 打印错误的 unicode 版本?

c - 使用数组作为指向字符串的指针来确定 int、double 的数据类型

c - 通过arm c内联汇编器操作内存中的数组

Python:使用 UTF-8 编码读取德语/西类牙语 CSV 文件

c - 通过引用传递变量的错误

c - 合并帧代码未在 opencv 中提供输出