blez
blez

Reputation: 5047

How to extract and append path in C using wchar_t*

I want to extract the path from GetModuleFileNameW and then to append "\hello.dll" to it (Without making "\\\\"). How to do that? (I'm not good with Unicode functions)

Upvotes: 0

Views: 852

Answers (1)

Billy ONeal
Billy ONeal

Reputation: 106589

Assuming you're really working with paths, use the PathAppendW function. Note that you do this by appending "hello.dll" -- the backslash will be added if required by PathAppendW.

Alternately, you could write your own function pretty easily. Here's an example I threw together in 5 minutes in terms of 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;
}

Upvotes: 2

Related Questions