Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to *safely* return a TCHAR* from a function?
    primarykey
    data
    text
    <p>I've created a function that will convert all the event notification codes to strings. Pretty simple stuff really.</p> <p>I've got a bunch of consts like</p> <pre><code>const _bstr_t DIRECTSHOW_MSG_EC_ACTIVATE("A video window is being activated or deactivated."); const _bstr_t DIRECTSHOW_MSG_EC_BUFFERING_DATA("The graph is buffering data, or has stopped buffering data."); const _bstr_t DIRECTSHOW_MSG_EC_BUILT("Send by the Video Control when a graph has been built. Not forwarded to applications."); .... etc.... </code></pre> <p>and my function</p> <pre><code>TCHAR* GetDirectShowMessageDisplayText( int messageNumber ) { switch( messageNumber ) { case EC_ACTIVATE: return DIRECTSHOW_MSG_EC_ACTIVATE; case EC_BUFFERING_DATA: return DIRECTSHOW_MSG_EC_BUFFERING_DATA; case EC_BUILT: return DIRECTSHOW_MSG_EC_BUILT; ... etc ... </code></pre> <p>No big deal. Took me 5 minutes to throw together.</p> <p>... but I simply don't trust that I've got all the possible values, so I want to have a default to return something like "Unexpected notification code (7410)" if no matches are found.</p> <p>Unfortunately, I can't think of anyway to return a valid pointer, without forcing the caller to delete the string's memory ... which is not only nasty, but also conflicts with the simplicity of the other return values.</p> <p>So I can't think of any way to do this without changing the return value to a parameter where the user passes in a buffer and a string length. Which would make my function look like </p> <pre><code>BOOL GetDirectShowMessageDisplayText( int messageNumber, TCHAR* outBuffer, int bufferLength ) { ... etc ... </code></pre> <p>I really don't want to do that. There must be a better way.</p> <p>Is there?</p> <p>I'm coming back to C++ after a 10 year hiatus, so if it's something obvious, don't discount that I've overlooked it for a reason.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload