Convert OEM clipboard text to ANSI?

Developer's Forum, for discussion of bugs, code, and other developmental aspects of DOSBox.

Convert OEM clipboard text to ANSI?

Postby emendelson » 2018-1-17 @ 16:55

I'm not sure this will ever be relevant to games, but for the project I'm putting together for running Windows 3.11 (including games), I'm trying to modify some code so that I can import from the host Windows clipboard in ANSI encoding. My other projects use clipboard-paste code taken from the old dbDOS project that looks like this:

Code: Select all
void PasteClipboard(bool bPressed)
{
    if (!bPressed) return;
    SDL_SysWMinfo wmiInfo;
    SDL_VERSION(&wmiInfo.version);

    if (SDL_GetWMInfo(&wmiInfo) != 1) return;
    if (!::OpenClipboard(wmiInfo.window)) return;
    if (!::IsClipboardFormatAvailable(CF_OEMTEXT)) return; //fix from CF_TEXT to CF_OEMTEXT

    HANDLE hContents = ::GetClipboardData(CF_OEMTEXT); //fix to CF_OEMTEXT
    if (!hContents) return;


    const char* szClipboard = (const char*)::GlobalLock(hContents);
    if (szClipboard)
    {
        // Create a copy of the string, and filter out Linefeed characters (ASCII '10')
        size_t sClipboardLen   = strlen(szClipboard);
        char* szFilteredText   = reinterpret_cast<char*>(alloca(sClipboardLen + 1));
        char* szFilterNextChar = szFilteredText;
        for (size_t i = 0; i < sClipboardLen; ++i)
            if (szClipboard[i] != 0x0A) // Skip linefeeds
            {
                *szFilterNextChar = szClipboard[i];
                ++szFilterNextChar;
            }
        *szFilterNextChar = '\0'; // Cap it.

        strPasteBuffer.append(szFilteredText);
        ::GlobalUnlock(hContents);
    }

    ::CloseClipboard();
}


Wengier Wu gave me the fix from CF_TEXT to CF_OEMTEXT that makes it possible to paste upper-ASCII characters. Now I'm trying to adapt this so that I can test for CF_UNICODETEXT and then convert that unicode text to ANSI format for pasting into Win 3.11.

I've spent a lot of time trying to use WideCharToMultiByte to convert the clipboard contents, but I can't make it work. What's especially frustrating is that I once knew how to do this. A few years ago, I found a lot of code online that I mangled into a working executable that saved the Windows clipboard to a specified codepage, but converted it byte by byte.

What I think I need to do is something like this:

Code: Select all
#include <atlconv.h>   
#include <atlstr.h>     
....
const CStringW unicode = (szClipboard);
const CStringA szFilteredText = CW2A(unicode, CP_ACP);


But that's as far as I can get...
emendelson
Oldbie
 
Posts: 746
Joined: 2010-2-14 @ 02:00

Return to DOSBox Development

Who is online

Users browsing this forum: Google [Bot] and 1 guest