0

I am trying to convert previous code to VS 2010. The code I am trying to convert is mentioned below. The function addCommand is defined like

addCommand(const ACHAR * cmdGroupName,  const ACHAR * cmdGlobalName, const ACHAR * cmdLocalName, Adesk::Int32 commandFlags, AcRxFunctionPtr FunctionAddr,AcEdUIContext *UIContext=NULL,  int fcode=-1,  HINSTANCE hResourceHandle=NULL,  AcEdCommand** cmdPtrRet=NULL)

The third required argument is of type ACHAR. The function is called in the following way.

char cmdLocRes[65];

// If idLocal is not -1, it's treated as an ID for
// a string stored in the resources.
if (idLocal != -1) {

    // Load strings from the string table and register the command.
    ::LoadString(_hdllInstance, idLocal, cmdLocRes, 64);
    acedRegCmds->addCommand(cmdGroup, cmdInt, cmdLocRes, cmdFlags, cmdProc);

My problem is that the variable cmdLocRes is of type char but the argument needs to be of type ACHAR.

How can I convert the same ?

Aanand
  • 1
  • 2

1 Answers1

0
  1. ACHAR is a typedef (made by Autodesk in file AdAChar.h) of wchar_t. So the question is how to convert a char to wchar_t.
  2. In a wider context this problem is because of the existence of unicode. Linux and Windows programmers normally discuss it without understanding each other. As I do not understand it, too, I cannot explain it. There are threads for the eager beaver: What's "wrong" with C++ wchar_t and wstrings? What are some alternatives to wide characters?
  3. The folling might give you an idea how to convert it.
    // Convert char to wchar_t

    char cmdLocRes[65];

    // Remark: Make sure cmdLocRes contains elements!

    cmdLocRes[0] = 'A';

    cmdLocRes[1] = '\0';

    // Get a wstringstream

    std::wstringstream str;

    // Write the char array to the wstringstream

    str << cmdLocRes;

    // Get a wstring from the wstringstream

    std::wstring wstr = str.str();

    // Get a wchar_t from the wstring

    const wchar_t *chr1 = wstr.c_str();

    const ACHAR *chr2 = wstr.c_str(); // We see that wchar_t == ACHAR!

  4. Better think of using wchar_t cmdLocRes[65] instead of char cmdLocRes[65]!

  5. Sorry for the code style, but this text field is another great example for how not to do it. It took me longer to try to format the code block (and please look at it!!!) than to write the answer. Jesus!!!
Community
  • 1
  • 1