Convert a System String into a WCHAR or OLECHAR

I am not stating that this is the only - or even best - way to convert; however I spent a lot of time trying to find a better, simpler way to read in a string and convert it so I could open an API that needed an OLECHAR pointer - i.e., the real work - If you have a better suggesion, please let me know. sam@gilcrist.com. Until then, at least this works.

Because I am not crazy about the new Net pointers, this code needs to be compiled with the /clr:oldSyntax command line.

To get to the command line, go to Project->Properties->C/C++->CommandLine and type in the string under Additional Options.

Click this link to download the c file.

Return to www.gilcrist.com


    /**************************************************************************************
    * author  : R. Sam Gilcrist
    * date    : 11 Jan 2006
    * purpose : Convert a System::String into a WCHAR or OLECHAR
    **************************************************************************************/


      #include "stdafx.h"
      #include 
      using namespace System;

      int main()
      {
        String* str = "012wxyz789";
        Char arr[]= str->ToCharArray(0,str->Length);
        WCHAR wArr[1024];
        int i = 0;
        for(i =0;i Length;i++){
           wArr[i] = arr[i];
        }
        wArr[i]=0;
        return 0;
      }