Font Conversion Problem Delphi7 to Delphixe7

Asked

Viewed 218 times

1

I have a certain source code to control microterminals of the Gertec brand that was compiled in Delphi7 and works all ok, when I compile in Delphi XE7 occur some problems.

For those who do not know micro terminal is that.

I use the following function to send text to the display:

procedure TFTerminalSys.EnviaMensagem(Mensagem:String;Linha:Integer);
var
    buf: array [0..255] of BYTE;
    limitechar:Integer;
begin
    LimparLinha(Linha);
    PosicionarCursor(Linha,1);
    Try
    // Verifica se há algum terminal selecionado
    if (ListBox1.ItemIndex = -1) then
    begin
        Application.MessageBox('Selecione um terminal.','Display',48)
    end
    else
    begin
        System.AnsiStrings.StrPCopy(@buf,Copy(Mensagem,1,20));

        // Envia o comando de DisplayString para o terminal
        if mt_dispstr(id_selecionado, buf[0]) < 1 then
        begin
            Application.MessageBox('Erro enviando comando.','ERRO',16);
        end;
    end;
    Except
        Application.MessageBox('Preencha corretamento os campos.','Display',48);
    end;
end;

This function was changed, the source came in Delphi7, the only difference was that I had to change the line

"StrPCopy(@buf,Copy(Mensagem,1,20));"

for

"System.AnsiStrings.StrPCopy(@buf,Copy(Mensagem,1,20));"

This change for some reason Buga my data return, and if I do not change to "System.AnsiStrings.StrPCopy" my message does not go to the terminal correctly, only the first character is sent(however if you run the example that came in Delphi7 without changes everything works correctly).

Finally I do not understand why the function of sending data to the terminal is bugging the return of data. Follow below the function I use to return data from the terminal:

procedure TFTerminalSys.HabilitaResposta(Senha:Boolean = False);
var
    buf : array [0..255] of BYTE;
    estado: DWORD;
begin

    // Verifica se há algum terminal selecionado
    if (ListBox1.ItemIndex = -1) then
    begin
        Application.MessageBox('Selecione um terminal.','Display',48)
    end
    else
    begin
        if (Senha) then
        begin
            estado := 1
        end
        else
        begin
            estado := 0;
        end;

        // Envia o comando de EditString para o terminal
        if mt_seteditstring(Id_selecionado,buf[0],1,estado) < 1 then
        begin
            Application.MessageBox('Erro enviando comando.','ERRO',16);
        end;
    end;
end;

By the way, it also does not work properly after I compiled in Delphi XE7, instead of returning the data correctly, returns what seems to be memory junk. For example: when type '222' in the micro terminal is returned '4þUaE222' (among other strange characters), if it was always a fixed number of garbage I could use a copy and start to pick up the return from when the garbage characters end, but the garbage number is not fixed. What is strange is that the garbage in the return changes if I change the function of sending text to the terminal (first function I pasted there above). Follow the example below :

1st case: Changing the line "StrPCopy(@buf,Copy(Mensagem,1,20));" for "System.AnsiStrings.StrPCopy(@buf,Copy(Mensagem,1,20))" and sending '222' to the terminal

Result: 4þ@aEÓAi2

2nd case: leaving the line "StrPCopy(@buf,Copy(Mensagem,1,20));" unchanged and sending '222' to the terminal

Result: 4þUaEÓ %222

In the second case I get a more stable response, I could use a Copy and cut out only the part without garbage, but remembering that I change the line "StrPCopy(@buf,Copy(Mensagem,1,20));" for "System.AnsiStrings.StrPCopy(@buf,Copy(Mensagem,1,20));" because otherwise the message does not send to the terminal display correctly, only the first character is sent, for example "Welcome" would send only "B"

My doubt is why this memory junk appears, and why if I run the version of Delphi7 works correctly?

  • The Strpcopy version of Sysutils was discontinued. See: http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.SysUtils.StrPCopy It is correct that you use System.AnsiStrings.Strpcopy. Take a test and use the following function, see if it helps you: function StrPCopyCustomizada(const Source: AnsiString; Dest: PAnsiChar): PAnsiChar;&#xA;begin&#xA; Move(PAnsiChar(Source)^, Dest^, Length(Source) + 1); // +1 for the 0 char&#xA; Result := Dest;&#xA;end; .

1 answer

1


The problem is in the function:

System.AnsiStrings.StrPCopy(@buf,Copy(Mensagem,1,20));

This method expects Source (the second parameter) to be of the type AnsiString. But note that you have declared the parameter Mensagem as a String:

procedure TFTerminalSys.EnviaMensagem(Mensagem:String;Linha:Integer);

In newer versions of Delphi the type string became an alias of UnicodeString. Consequently Mensagem ends up being a UnicodeString, causing the indefinite behavior.

To fix, explicitly convert the string to an Ansistring:

System.AnsiStrings.StrPCopy(@buf, AnsiString(Copy(Mensagem,1,20)));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.