646 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 - Archive

Welcome to the Ask Embedded Wizard archive. This community forum served us well for many years, but we've evolved our support approach!

Your resources:

The Embedded Wizard Online Documentation provides comprehensive documentation, tutorials, examples and ready-to-use software packages.

For dedicated assistance, explore our Embedded Wizard Product Support.

You can still browse the valuable discussions from our community history here.

Embedded Wizard Website | Privacy Policy | Imprint

...