298 views
in System Integration by

Hello

How can I convert XString to char and const char *?

For example;

Situation No. 1

var string comname = "\\\\.\\COM"+(string)Application::Device.SelectedPorts; //string to convert

native( comname)
{
const char* COMPORT = comname; //there i have put my string and convert to const char *


                hComm = CreateFileA(COMPORT,//port name
                                     ...)              
}
--------------------------------------------------------------------------------------------------
Situation No. 2

var string text = "Hello"; //string to convert

native(tetx)
{
char lpBuffor_write[33] = text; //there i have put my string and convert to char

                    WriteFile(
                    hComm,           // open file handle
                    lpBuffor_write,      // start of data to write
                    strlen(lpBuffor_write),  // number of bytes to write
                    ...)
                        
                    CloseHandle(hComm);//Closing the Serial Port
}
--------------------------------------------------------------------------------------------------

I would like to ask for help because I have been struggling with this for a long time.

Best regards,

Kyo

1 Answer

+1 vote
by
 
Best answer

Hello Kyo,

such conversion is handled by following RTE functions:

EwStringToUtf8()

EwStringToAnsi()

For example:

var string comname = "\\\\.\\COM"+(string)Application::Device.SelectedPorts; //string to convert

native( comname)
{
  char COMPORT[ MAX_PATH ];

  EwStringToUtf8( comname, COMPORT, sizeof( COMPORT )); 

  hComm = CreateFileA( COMPORT, ... );
}

Another approach: From your example I deduce that you are working on Win32 API. In such case you can use the wide-char version of the API and pass the Embedded Wizard Strings directly to it. For example:

var string comname = "\\\\.\\COM"+(string)Application::Device.SelectedPorts; //string to convert

native( comname)
{
  /* Use the wide-char version of the function (with W in its name) */
  hComm = CreateFileW( comname, ... );
}

Best regards

Paul Banach

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...