Board index » cppbuilder » Re: Pascal to C++

Re: Pascal to C++


2005-09-28 09:08:09 PM
cppbuilder85
Hello Andrue,
the purpose of the code is to open a communications port on launching an
application. The full code is as follows.
Procedure TFMain.FormCreate (Sender : TObject);
Begin
If HComm <= 0 Then
Begin
CloseHandle (HComm); HComm := 0
End;
HComm := CreateFile (PChar (PName), GENERIC_READ Or GENERIC_WRITE, 0, Nil,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
If HComm = Invalid_Handle_Value
Then ShowMessage ('Unable to open comm port');
End;
I am familiar with basic Pascal and C++ syntax but I cannot recall the
purpose of a semicolon in the middle of a line of Pascal code. I tried
translating the first section of code as follows but without success.
void __fastcall TForm1::FormCreate(TObject *Sender)
{
if (HComm <= 0)
{
CloseHandle (HComm);
HComm = 0;
}
"Andrue Cope [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
>I wish to translate the following line of Pascal code to C++.
>
>CloseHandle (HComm); HComm := 0

CloseHandle (HComm); HComm=0;

..but I think you need to tell us what the code is actually supposed to
be doing. At the moment it's just generic code that is calling a
function and passing a variable into it then setting that variable to
zero. A basic knowledge of C++ syntax would suffice to translate that.

I assume you're actually after specific information for some library or
standard API.

--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html
 
 

Re:Re: Pascal to C++

On Wed, 28 Sep 2005 14:08:09 +0100, Maurice wrote:
Quote
I am familiar with basic Pascal and C++ syntax but I cannot recall the
purpose of a semicolon in the middle of a line of Pascal code.
It's a statement terminator.
--
liz
 

Re:Re: Pascal to C++

Maurice wrote:
Quote
I am familiar with basic Pascal and C++ syntax but I cannot recall
the purpose of a semicolon in the middle of a line of Pascal code.
It isn't in the middle. Pascal source is free form as is C++. A
semi-colon does for Pascal what it does for C++. Of course most
programmers keep one statement per line but that's only a convention as
it is with C++. There are a few odd rules about when a semi-colon is
and isn't needed (not before 'else' for one thing) but as you saw from
my translation it's mostly used the same way. The original code you
posted is just two Pascal statements and my translation is two C++
statements.
Quote
if (HComm <= 0)
{
CloseHandle (HComm);
HComm = 0;

}
Syntactically that's fine. Unfortunately you've failed to provide a
complete code example and also failed to tell us what the error message
is. If you're going to get useful help from these newsgroups you need
to realise that we can't see you or your computer and what you don't
tell us we don't know and often can't guess.
In this instance I would hazard a guess that 'HComm' doesn't have the
correct type but I don't know because you haven't provided the
declaration for me to see.
if( HComm==INVALID_HANDLE_VALUE )
ShowMessage ("Unable to open comm port");
Quote
HComm := CreateFile (PChar (PName), GENERIC_READ Or GENERIC_WRITE, 0,
Nil,

OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
In most cases API related code will translate with almost no changes at
all:
HComm=CreateFile( PName, GENERIC_READ|GENERIC_WRITE,0,NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
..but again it depends on what type 'HComm' is. It should be declared
as:
HANDLE HComm;
as the help for CreateFile() would indicate.
One final point about this translation is that I have simply omitted
PChar(). The C++ equivalent of PChar() is a cast to 'char *' but I
vaguely recall that PChar() is more than just a cast and can convert
from Pascal 'string' type to 'char *'.
--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html
 

{smallsort}

Re:Re: Pascal to C++

"Maurice" < XXXX@XXXXX.COM >wrote in message
Quote
I am familiar with basic Pascal and C++ syntax but I cannot
recall the purpose of a semicolon in the middle of a line of
Pascal code.
It serves the exact same purpose that it does in C++ - to separate multiple
statements that are on a single line rather than on their own individual
lines.
Quote
I tried translating the first section of code as follows but without
success.
A literal translation would be as follows:
void __fastcall TFMain::FormCreate(TObject *Sender)
{
if( HComm <= 0 )
{
CloseHandle(HComm);
HComm = 0;
}
HComm = CreateFile(PName.c_str(), GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if( HComm == INVALID_HANDLE_VALUE )
ShowMessage("Unable to open comm port");
}
However, the OnCreate event is a Delphi diom that can produce illegal
behavior in C++ as it can be triggered before the constructor. You should
use the actual constructor instead:
void __fastcall TFMain::TFMain(TComponent *Owner)
: TForm(Owner)
{
HComm = CreateFile(PName.c_str(), GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if( HComm == INVALID_HANDLE_VALUE )
ShowMessage("Unable to open comm port");
}
Gambit
 

Re:Re: Pascal to C++

"Andrue Cope [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
One final point about this translation is that I have simply omitted
PChar(). The C++ equivalent of PChar() is a cast to 'char *'
If PName is declared as a 'string' in Pascal, then it is an AnsiString in
C++, and thus you would use AnsiString's c_str() method to accomplish the
same thing that PChar() is doing:
HComm = CreateFile(PName.c_str(), ...);
Gambit
 

Re:Re: Pascal to C++

Remy Lebeau (TeamB) wrote:
Quote
It serves the exact same purpose that it does in C++ - to separate multiple
statements that are on a single line rather than on their own individual
lines.

Hmm, that's not the purpose of semicolon in C++.
.a
 

Re:Re: Pascal to C++

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >writes:
Quote
"Maurice" < XXXX@XXXXX.COM >wrote in message
news:433a9551$ XXXX@XXXXX.COM ...

>I am familiar with basic Pascal and C++ syntax but I cannot
>recall the purpose of a semicolon in the middle of a line of
>Pascal code.

It serves the exact same purpose that it does in C++ - to separate
multiple statements that are on a single line rather than on their
own individual lines.
Seperation is not what the semicolon does in C++. It is a statement
terminator. The main difference between a seperator and terminator is
that you only need a seperator between two things. But in C++, even a
statement by itself needs a termination after it. Sometimes you can
get away with no semicolon in Pascal where it is still required in C++.
--
Chris (TeamB);
 

Re:Re: Pascal to C++

Hello Remy
thank you for the translations, my code now compiles. I left it in the
OnCreate event for the moment and it seems to work. I will change it as you
suggest later. Thank you also to Andrue and Liz.
Maurice
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Maurice" < XXXX@XXXXX.COM >wrote in message
news:433a9551$ XXXX@XXXXX.COM ...

>I am familiar with basic Pascal and C++ syntax but I cannot
>recall the purpose of a semicolon in the middle of a line of
>Pascal code.

It serves the exact same purpose that it does in C++ - to separate
multiple
statements that are on a single line rather than on their own individual
lines.

>I tried translating the first section of code as follows but without
success.

A literal translation would be as follows:

void __fastcall TFMain::FormCreate(TObject *Sender)
{
if( HComm <= 0 )
{
CloseHandle(HComm);
HComm = 0;
}

HComm = CreateFile(PName.c_str(), GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if( HComm == INVALID_HANDLE_VALUE )
ShowMessage("Unable to open comm port");
}

However, the OnCreate event is a Delphi diom that can produce illegal
behavior in C++ as it can be triggered before the constructor. You should
use the actual constructor instead:

void __fastcall TFMain::TFMain(TComponent *Owner)
: TForm(Owner)
{
HComm = CreateFile(PName.c_str(), GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if( HComm == INVALID_HANDLE_VALUE )
ShowMessage("Unable to open comm port");
}


Gambit


 

Re:Re: Pascal to C++

Hi
I'm trying to convert a Pascal (Delphi) program for BCB 5
procedure EnumNodes( Node : TTreeNode;
Enum : EnumNodesProc;
pData : pointer;
bPre : boolean
);
begin
....
end;
EnumNodesProc = procedure( Node : TTreeNode; pData : pointer );
Can someone show me what the equivalent code in C++ would be for the above.
Thanks in advance.
Ralph
 

Re:Re: Pascal to C++

"Ralph Gamble" < XXXX@XXXXX.COM >wrote in message
Quote
Can someone show me what the equivalent code in C++ would be for the
above.
typedef void __fastcall (*EnumNodesProc)(TTreeNode *Node, void *pData);
void __fastcall EnumNodes(TTreeNode *Node, EnumNodesProc Enum, void
*pData, bool bPre)
{
//...
}
Gambit
 

Re:Re: Pascal to C++

Thanks alot Remy
you are fast!
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Ralph Gamble" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>Can someone show me what the equivalent code in C++ would be for the
above.

typedef void __fastcall (*EnumNodesProc)(TTreeNode *Node, void *pData);

void __fastcall EnumNodes(TTreeNode *Node, EnumNodesProc Enum, void
*pData, bool bPre)
{
//...
}


Gambit