Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2008-02

Convert C++ Array[1] to FPC

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Sooky Boo

Please help I am unsure how to port from C++ to fpc this code. The function this must be passed to says that the structure pointer or size is invalid.

----C++----
typedef struct _SSLPROTOCOL {
    DWORD dwProtocol;
    DWORD dwVersion;
    DWORD dwFlags;
} SSLPROTOCOL, FAR *LPSSLPROTOCOL;
typedef struct _SSLPROTOCOLS {
    DWORD dwCount;
    SSLPROTOCOL ProtocolList[1];   // array of 'count' structures
} SSLPROTOCOLS, FAR *LPSSLPROTOCOLS;

----My Attempt FPC Delphi Mode----

type  SSLPROTOCOL = record
    dwProtocol : DWORD;
    dwVersion  : DWORD;
    dwFlags    : DWORD;
end;
_SSLPROTOCOL = SSLPROTOCOL;
LPSSLPROTOCOL = ^SSLPROTOCOL;
type SSLPROTOCOLS = record
     dwCount :DWORD;
     ProtocolList : array[0..0] of SSLPROTOCOL;   // array of 'count'
structures
end;
 _SSLPROTOCOLS = SSLPROTOCOLS;
 LPSSLPROTOCOLS = ^SSLPROTOCOLS;
 PSSLPROTOCOLS = ^SSLPROTOCOLS;
---- ----
Thanks in advance.
If my port seems correct are there any ideas what could be the problem?

Reply by email

ik
to Sooky Boo
{$PACKRECORDS C}
...
 type
    LPSSLPROTOCOL = ^SSLPROTOCOL;
    SSLPROTOCOL = record
     dwProtocol : DWORD;
     dwVersion  : DWORD;
     dwFlags    : DWORD;
 end;
 _SSLPROTOCOL = SSLPROTOCOL;
Sooky Boo wrote 44 lines
Please help I am unsure how to port from C++ to fpc this code. The function this must be passed to says that the structure pointer or size is invalid. ----C++---- typedef struct _SSLPROTOCOL { DWORD dwProtocol; DWORD dwVersion; DWORD dwFlags; } SSLPROTOCOL, FAR *LPSSLPROTOCOL; typedef struct _SSLPROTOCOLS { DWORD dwCount; SSLPROTOCOL ProtocolList[1]; // array of 'count' structures } SSLPROTOCOLS, FAR *LPSSLPROTOCOLS; ----My Attempt FPC Delphi Mode---- type SSLPROTOCOL = record dwProtocol : DWORD; dwVersion : DWORD; dwFlags : DWORD; end; _SSLPROTOCOL = SSLPROTOCOL; LPSSLPROTOCOL = ^SSLPROTOCOL; type SSLPROTOCOLS = record dwCount :DWORD; ProtocolList : array[0..0] of SSLPROTOCOL; // array of 'count' structures end; _SSLPROTOCOLS = SSLPROTOCOLS; LPSSLPROTOCOLS = ^SSLPROTOCOLS; PSSLPROTOCOLS = ^SSLPROTOCOLS; ---- ---- Thanks in advance. If my port seems correct are there any ideas what could be the problem? _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Sooky Boo
to ik
ik wrote 67 lines
{$PACKRECORDS C} ... type LPSSLPROTOCOL = ^SSLPROTOCOL; SSLPROTOCOL = record dwProtocol : DWORD; dwVersion : DWORD; dwFlags : DWORD; end; _SSLPROTOCOL = SSLPROTOCOL; On Sat, Feb 23, 2008 at 5:59 AM, Sooky Boo wrote: > > > Please help I am unsure how to port from C++ to fpc this code. > The function this must be passed to says that the structure pointer or size > is invalid. > > ----C++---- > typedef struct _SSLPROTOCOL { > DWORD dwProtocol; > DWORD dwVersion; > DWORD dwFlags; > } SSLPROTOCOL, FAR *LPSSLPROTOCOL; > > typedef struct _SSLPROTOCOLS { > DWORD dwCount; > SSLPROTOCOL ProtocolList[1]; // array of 'count' structures > } SSLPROTOCOLS, FAR *LPSSLPROTOCOLS; > > ----My Attempt FPC Delphi Mode---- > > type SSLPROTOCOL = record > dwProtocol : DWORD; > dwVersion : DWORD; > dwFlags : DWORD; > end; > _SSLPROTOCOL = SSLPROTOCOL; > LPSSLPROTOCOL = ^SSLPROTOCOL; > > type SSLPROTOCOLS = record > dwCount :DWORD; > ProtocolList : array[0..0] of SSLPROTOCOL; // array of 'count' > structures > end; > _SSLPROTOCOLS = SSLPROTOCOLS; > LPSSLPROTOCOLS = ^SSLPROTOCOLS; > PSSLPROTOCOLS = ^SSLPROTOCOLS; > ---- ---- > Thanks in advance. > If my port seems correct are there any ideas what could be the problem? > _______________________________________________ > fpc-devel maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-devel > > -- http://ik.homelinux.org/ _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Unfortunately still doesn't work :(

Reply by email

Bram Kuijvenhoven
to Sooky Boo
Sooky Boo wrote 25 lines
On 2/23/08, ik wrote: > {$PACKRECORDS C} > > On Sat, Feb 23, 2008 at 5:59 AM, Sooky Boo wrote: >> >> Please help I am unsure how to port from C++ to fpc this code. >> The function this must be passed to says that the structure pointer or > size >> is invalid. >> >> ----C++---- >> >> typedef struct _SSLPROTOCOLS { >> DWORD dwCount; >> SSLPROTOCOL ProtocolList[1]; // array of 'count' structures >> } SSLPROTOCOLS, FAR *LPSSLPROTOCOLS; >> >> ----My Attempt FPC Delphi Mode---- >> >> type SSLPROTOCOLS = record >> dwCount :DWORD; >> ProtocolList : array[0..0] of SSLPROTOCOL; // array of 'count' structures >> end; Unfortunately still doesn't work :(

The actual size of the SSLPROTOCOLS struct/record should match the value of dwCount. Allocating such a record probably should go like

LPSSLPROTOCOLS(GetMem(sizeOf(SSLPROTOCOLS) + (dwCount - 1) * sizeOf(SSLPROTOCOL)))

Sometimes a dummy record is to be appended at the end of the array, similar to the null character at the end of a null-terminated string. In that case it is possible that this 'sentinel' record is not counted in dwCount, but you should still allocate the space of course. Please refer to the documentation of the header file (I hope there is).

I hope this helps.

Regards,

Bram

Reply by email

Sooky Boo
to Bram Kuijvenhoven

I put the function in a for loop and passed I as the size paramater until it succeeded,

the size of the structure it expects is 40.

40 is equal to  GetMem(sizeOf(SSLPROTOCOLS) + (2) * sizeOf(SSLPROTOCOL));

so

protocols := LPSSLPROTOCOLS(GetMem(sizeOf(SSLPROTOCOLS) + (2) *
sizeOf(SSLPROTOCOL)));

but the size of protocols^ is always 16 no matter what I put in GetMem. (16 is the size of SSLPROTOCOL on its own)
How do I now GetMem for protocols^.ProtocolList.

//protocols^.ProtocolList := (GetMem((2) * sizeOf(SSLPROTOCOL)));//Error:
Incompatible types: got "Pointer" expected "Array[0..0] Of SSLPROTOCOL"

Thanks

On Sun, Feb 24, 2008 at 12:31 PM, Bram Kuijvenhoven <

Show quoted text 52 lines
wrote: Sooky Boo wrote: > On 2/23/08, ik wrote: >> {$PACKRECORDS C} >> >> On Sat, Feb 23, 2008 at 5:59 AM, Sooky Boo wrote: >>> >>> Please help I am unsure how to port from C++ to fpc this code. >>> The function this must be passed to says that the structure pointer or >> size >>> is invalid. >>> >>> ----C++---- >>> >>> typedef struct _SSLPROTOCOLS { >>> DWORD dwCount; >>> SSLPROTOCOL ProtocolList[1]; // array of 'count' structures >>> } SSLPROTOCOLS, FAR *LPSSLPROTOCOLS; >>> >>> ----My Attempt FPC Delphi Mode---- >>> >>> type SSLPROTOCOLS = record >>> dwCount :DWORD; >>> ProtocolList : array[0..0] of SSLPROTOCOL; // array of 'count' structures >>> end; > > Unfortunately still doesn't work :( The actual size of the SSLPROTOCOLS struct/record should match the value of dwCount. Allocating such a record probably should go like LPSSLPROTOCOLS(GetMem(sizeOf(SSLPROTOCOLS) + (dwCount - 1) * sizeOf(SSLPROTOCOL))) Sometimes a dummy record is to be appended at the end of the array, similar to the null character at the end of a null-terminated string. In that case it is possible that this 'sentinel' record is not counted in dwCount, but you should still allocate the space of course. Please refer to the documentation of the header file (I hope there is). I hope this helps. Regards, Bram _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Bram Kuijvenhoven
to Sooky Boo
Sooky Boo wrote
I put the function in a for loop and passed I as the size paramater until it succeeded, the size of the structure it expects is 40.

BTW what is the value of dwCount?

40 is equal to GetMem(sizeOf(SSLPROTOCOLS) + (2) * sizeOf(SSLPROTOCOL));

Indeed.

Show quoted text 7 lines
so protocols := LPSSLPROTOCOLS(GetMem(sizeOf(SSLPROTOCOLS) + (2) * sizeOf(SSLPROTOCOL))); but the size of protocols^ is always 16 no matter what I put in GetMem. (16 is the size of SSLPROTOCOL on its own)

Well, SSLPROTOCOL has size 12 and SSLPROTOCOLS has size 16. (Note that the SSLPROTOCOLS record type holds one DWORD and one SSLPROTOCOL.)

The point is that neither C nor Pascal has a way to declare a record/struct of varying size. The compiler itself is not aware of the fact that SSLPROTOCOLS.ProtocolList is actually an array of of dwCount items.

But you can still use a trick: allocate a block of memory using GetMem, then cast the pointer to the memory block to LPSSLPROTOCOLS ( = ^SSLPROTOCOLS). This allows you to interpret the first SizeOf(SSLPROTOCOLS) bytes of the memory block as if it is a SSLPROTOCOLS record. Moreover, when range checking is off, you can use the syntax

protocols^.ProtocolList[i]

to access any i-th element (as long as you allocated a memory block that is large enough).

Show quoted text 5 lines
How do I now GetMem for protocols^.ProtocolList. //protocols^.ProtocolList := (GetMem((2) * sizeOf(SSLPROTOCOL)));//Error: Incompatible types: got "Pointer" expected "Array[0..0] Of SSLPROTOCOL"

The compiler error tells you precisely what is wrong here. An 'array[0..0] of anyThing' is not a pointer type in Pascal (and neither is the 'anyThing[1]' type in C). Instead an 'array[0..0] of anyThing' holds room for (and has the same size as) one 'anyThing'.

You can't do

  var i:integer;
  ...
  i := GetMem(SizeOf(integer));

but you can do

  var pi:^integer; // pointer to integer
  ...
  pi := ^integer(GetMem(SizeOf(integer)));

Bram

Show quoted text 65 lines
On Sun, Feb 24, 2008 at 12:31 PM, Bram Kuijvenhoven < > wrote: Sooky Boo wrote: > On 2/23/08, ik < > wrote: >> {$PACKRECORDS C} >> >> On Sat, Feb 23, 2008 at 5:59 AM, Sooky Boo < > wrote: >>> >>> Please help I am unsure how to port from C++ to fpc this code. >>> The function this must be passed to says that the structure pointer or >> size >>> is invalid. >>> >>> ----C++---- >>> >>> typedef struct _SSLPROTOCOLS { >>> DWORD dwCount; >>> SSLPROTOCOL ProtocolList[1]; // array of 'count' structures >>> } SSLPROTOCOLS, FAR *LPSSLPROTOCOLS; >>> >>> ----My Attempt FPC Delphi Mode---- >>> >>> type SSLPROTOCOLS = record >>> dwCount :DWORD; >>> ProtocolList : array[0..0] of SSLPROTOCOL; // array of 'count' structures >>> end; > > Unfortunately still doesn't work :( The actual size of the SSLPROTOCOLS struct/record should match the value of dwCount. Allocating such a record probably should go like LPSSLPROTOCOLS(GetMem(sizeOf(SSLPROTOCOLS) + (dwCount - 1) * sizeOf(SSLPROTOCOL))) Sometimes a dummy record is to be appended at the end of the array, similar to the null character at the end of a null-terminated string. In that case it is possible that this 'sentinel' record is not counted in dwCount, but you should still allocate the space of course. Please refer to the documentation of the header file (I hope there is). I hope this helps. Regards, Bram _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel ------------------------------------------------------------------------ _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Sooky Boo
to Bram Kuijvenhoven

Thanks for all the help already working :), realized after last email, getmem allocates enough memory regardless of what sizeof() says.

Hopefully this will help someone in future with the same type of question.

After the function returns (and populates SSLPROTOCOLS) dwCount is the amount of elements in the array.

Thanks for the useful tricks.

On Sun, Feb 24, 2008 at 5:55 PM, Bram Kuijvenhoven <

Show quoted text 138 lines
wrote: Sooky Boo wrote: > I put the function in a for loop and passed I as the size paramater > until it succeeded, > > the size of the structure it expects is 40. BTW what is the value of dwCount? > 40 is equal to GetMem(sizeOf(SSLPROTOCOLS) + (2) * sizeOf(SSLPROTOCOL)); Indeed. > so > > protocols := LPSSLPROTOCOLS(GetMem(sizeOf(SSLPROTOCOLS) + (2) * > sizeOf(SSLPROTOCOL))); > > but the size of protocols^ is always 16 no matter what I put in GetMem. > (16 is the size of SSLPROTOCOL on its own) Well, SSLPROTOCOL has size 12 and SSLPROTOCOLS has size 16. (Note that the SSLPROTOCOLS record type holds one DWORD and one SSLPROTOCOL.) The point is that neither C nor Pascal has a way to declare a record/struct of varying size. The compiler itself is not aware of the fact that SSLPROTOCOLS.ProtocolList is actually an array of of dwCount items. But you can still use a trick: allocate a block of memory using GetMem, then cast the pointer to the memory block to LPSSLPROTOCOLS ( = ^SSLPROTOCOLS). This allows you to interpret the first SizeOf(SSLPROTOCOLS) bytes of the memory block as if it is a SSLPROTOCOLS record. Moreover, when range checking is off, you can use the syntax protocols^.ProtocolList[i] to access any i-th element (as long as you allocated a memory block that is large enough). > How do I now GetMem for protocols^.ProtocolList. > > //protocols^.ProtocolList := (GetMem((2) * > sizeOf(SSLPROTOCOL)));//Error: Incompatible types: got "Pointer" > expected "Array[0..0] Of SSLPROTOCOL" The compiler error tells you precisely what is wrong here. An 'array[0..0] of anyThing' is not a pointer type in Pascal (and neither is the 'anyThing[1]' type in C). Instead an 'array[0..0] of anyThing' holds room for (and has the same size as) one 'anyThing'. You can't do var i:integer; ... i := GetMem(SizeOf(integer)); but you can do var pi:^integer; // pointer to integer ... pi := ^integer(GetMem(SizeOf(integer))); Bram > On Sun, Feb 24, 2008 at 12:31 PM, Bram Kuijvenhoven > < > > wrote: > > Sooky Boo wrote: > > On 2/23/08, ik < > wrote: > >> {$PACKRECORDS C} > >> > >> On Sat, Feb 23, 2008 at 5:59 AM, Sooky Boo < > > wrote: > >>> > >>> Please help I am unsure how to port from C++ to fpc this code. > >>> The function this must be passed to says that the structure > pointer or > >> size > >>> is invalid. > >>> > >>> ----C++---- > >>> > >>> typedef struct _SSLPROTOCOLS { > >>> DWORD dwCount; > >>> SSLPROTOCOL ProtocolList[1]; // array of 'count' structures > >>> } SSLPROTOCOLS, FAR *LPSSLPROTOCOLS; > >>> > >>> ----My Attempt FPC Delphi Mode---- > >>> > >>> type SSLPROTOCOLS = record > >>> dwCount :DWORD; > >>> ProtocolList : array[0..0] of SSLPROTOCOL; // array of 'count' > structures > >>> end; > > > > Unfortunately still doesn't work :( > > The actual size of the SSLPROTOCOLS struct/record should match the > value of dwCount. Allocating such a record probably should go like > > LPSSLPROTOCOLS(GetMem(sizeOf(SSLPROTOCOLS) + (dwCount - 1) * > sizeOf(SSLPROTOCOL))) > > Sometimes a dummy record is to be appended at the end of the array, > similar to the null character at the end of a null-terminated > string. In that case it is possible that this 'sentinel' record is > not counted in dwCount, but you should still allocate the space of > course. Please refer to the documentation of the header file (I hope > there is). > > I hope this helps. > > Regards, > > Bram > > _______________________________________________ > fpc-devel maillist - > > http://lists.freepascal.org/mailman/listinfo/fpc-devel > > > > ------------------------------------------------------------------------ > > _______________________________________________ > fpc-devel maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-devel _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email