Convert unicode string into wchar_t

4.7k Views Asked by At

I'm having a little problem with my application while trying to use WindowsAPI... I'm trying to connect to a handle in a way it works, but if I change the code it doesn't works anymore...

The code that works:

   handle_t porta; // Global var
   COMMTIMEOUTS tempos;  // Global var
   DCB configuracao; // Global var

   wchar_t pcCommPort[]= TEXT("COM1");
   //gate address to be accessed (COM1)

The code I'm trying to do:

   handle_t porta; // Global var
   COMMTIMEOUTS tempos;  // Global var
   DCB configuracao; // Global var

   String GATE = "COM" + Label1->Text;
   wchar_t pcCommPort[]= TEXT(GATE);
   //gate address to be accessed (Any gate)

I've also tried it:

   handle_t porta; // Global var
   COMMTIMEOUTS tempos;  // Global var
   DCB configuracao; // Global var

   wchar_t pcCommPort[]= TEXT("COM" + Label1->Text);
   //gate address to be accessed (Any gate)

And it:

   handle_t porta; // Global var
   COMMTIMEOUTS tempos;  // Global var
   DCB configuracao; // Global var

   String GATE = "COM" + Label1->Text;
   wchar_t pcCommPort[]= GATE;
   //gate address to be accessed (Any gate)

In any way or it says that I haven't mede the correct syntax or the error: Cannot convert unicode string into a wchar_t.

EDIT:

Full source (function):

void abrirporta(){
   wchar_t pcCommPort[]= TEXT("COM1");
   //endereço da porta a ser acessada (COM1)
   //porta foi declarado como HANDLE na seção private da declaração de classe Form
   //HANDLE porta
   porta = CreateFile(pcCommPort,GENERIC_READ+GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
   if(porta == INVALID_HANDLE_VALUE){
        log(7);
        throw Exception("Não foi possível abrir a porta COM.\nPor favor, tente novamente!");
    }

   //Tempo máximo entre o recebimento de 2 bytes (ms)
   tempos.ReadIntervalTimeout = 20;

   //Multiplicador de tempo de recebimento por byte
   tempos.ReadTotalTimeoutMultiplier = 0;
   tempos.ReadTotalTimeoutConstant = 100;
   tempos.WriteTotalTimeoutMultiplier = 0;
   tempos.WriteTotalTimeoutConstant = 100;
   if(!SetCommTimeouts(porta ,&tempos))
   {
      CloseHandle(porta);
      frmPrincipal->spLig->Visible = False;
      frmPrincipal->spStatusInd->Visible = False;
      log(6);
      throw Exception("Erro na configuração de Timeout");
   }

   GetCommState(porta, &configuracao);

   configuracao.BaudRate = 19200;
   configuracao.ByteSize = 8;
   configuracao.Parity = NOPARITY;
   configuracao.StopBits = ONESTOPBIT;

   if(!SetCommState(porta,&configuracao))
   {
        CloseHandle(porta);
        frmPrincipal->spLig->Visible = False;
        frmPrincipal->spStatusInd->Visible = False;
        log(5);
        throw Exception("Erro na Configuração da porta");
   }
    frmPrincipal->spLig->Visible = True;
    frmPrincipal->spStatusInd->Visible = False;
    log(3);
    frmPrincipal->btEnviar->Enabled = true;
    frmPrincipal->swSaida1->Enabled = true;
    log(8);
}

I hope you can help me... Since now thanks XD.

1

There are 1 best solutions below

3
On BEST ANSWER

You are making this more difficult then it needs to be. System::String is an alias for System::UnicodeString, which holds wchar_t data on Windows. You don't need to copy the UnicodeString data into a wchar_t[] buffer. You can use the UnicodeString::c_str() method when you need to pass a String to a function that expects wchar_t* parameters, eg:

void abrirporta()
{
    String pcCommPort = L"COM" + Label1->Text;
    porta = CreateFile(pcCommPort.c_str(), ...);
    ...
}