Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend ofToString to convert Windows data types #4098

Open
DomAmato opened this issue Jul 18, 2015 · 5 comments
Open

Extend ofToString to convert Windows data types #4098

DomAmato opened this issue Jul 18, 2015 · 5 comments

Comments

@DomAmato
Copy link
Contributor

it doesn't happen often, but often enough that the feature would be nice to have, where I need to convert w_chars, LPTSTR, LPTWSTR etc... the functions are already a part of ofSystemUtils

std::string convertWideToNarrow(const wchar_t *s, char dfault = '?',
const std::locale& loc = std::locale())
{
  std::ostringstream stm;

  while( *s != L'\0' ) {
    stm << std::use_facet< std::ctype<wchar_t> >( loc ).narrow( *s++, dfault );
  }
  return stm.str();
}

std::wstring convertNarrowToWide( const std::string& as ){
    // deal with trivial case of empty string
    if( as.empty() )    return std::wstring();

    // determine required length of new string
    size_t reqLength = ::MultiByteToWideChar( CP_UTF8, 0, as.c_str(), (int)as.length(), 0, 0 );

   // construct new string of required length
   std::wstring ret( reqLength, L'\0' );

   // convert old string to new string
    ::MultiByteToWideChar( CP_UTF8, 0, as.c_str(), (int)as.length(), &ret[0], (int)ret.length() );

    // return new string ( compiler should optimize this away )
    return ret;
}

The problem when you do ofToString(LPTSTR...) is you get the pointer not the string itself e.g.

LPTSTR errorText = 0x0336ffa0 L"The media session cannot pause from a stopped state." wchar_t *

string error = "0336FFA0" std::basic_string<char,std::char_traits,std::allocator >

@kylemcdonald
Copy link
Contributor

could you also give some examples of where/when this might be useful? at first glance it looks like a very platform-dependent kind of thing, which OF tries to hide by making things work on different platforms rather than by providing helper functions.

@DomAmato
Copy link
Contributor Author

Right it is platform specific and ideally it would be a feature of ofString so that those data types could just be passed in like any other data type and thus the code only works in the background. It would be useful for anything that deals with HRESULTs as the error messages are returned as LPTSTR and can't be decoded by ofToString. Here is an example of having to decode things for the Media Foundation lib

LPTSTR errorText = NULL;

FormatMessageW(
        // use system message tables to retrieve error text
        FORMAT_MESSAGE_FROM_SYSTEM
        // allocate buffer on local heap for error text
        | FORMAT_MESSAGE_ALLOCATE_BUFFER
        // Important! will fail otherwise, since we're not 
        // (and CANNOT) pass insertion parameters
        | FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,    // unused with FORMAT_MESSAGE_FROM_SYSTEM
        hr,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR)&errorText,  // output 
        0, // minimum size for output buffer
        NULL);   // arguments - see note 
    wstring ws = errorText;
    string error(ws.begin(), ws.end());

    ofLogError("ofxWMFVideoPlayer") << error;

@elliotwoods
Copy link
Contributor

ofToString on a HRESULT would be fantastic!

@kylemcdonald - HRESULT's are the default return for any function on the Windows platform (e.g. COM functions). They give either S_OK or something which denotes in what way the function failed.

@kylemcdonald
Copy link
Contributor

thanks for the example @DomAmato and explanation @elliotwoods

the only precedent i can think of for this sort of thing is the pascal strings used by ofQuickTimeGrabber. in that case, there's a macro defined in ofQtUtils.h, and i'm even not sure it's generally accessible (because people aren't usually writing addons that interface with quicktime directly).

with that in mind, maybe there is somewhere else in OF that could make use of a function like this? and that would be an argument for including it...

i'm sure @arturoc or @ofTheo have more thoughts on the matter, just wanted to see if i could get more info :)

@DomAmato
Copy link
Contributor Author

DomAmato commented Aug 8, 2015

#3068
#1743

Not exactly examples since its actually the inverse though it is an example of issues when dealing with windows data types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants