<div>Thanks for all the help already working :), realized after last email, getmem allocates enough memory regardless of what sizeof() says.<br><br>Hopefully this will help someone in future with the same type of question.<br>
<br>After the function returns (and populates SSLPROTOCOLS) dwCount is the amount of elements in the array.<br><br>Thanks for the useful tricks.</div>
<div> </div>
<div class="gmail_quote">On Sun, Feb 24, 2008 at 5:55 PM, Bram Kuijvenhoven <<a href="mailto:mailinglists.bram@kuijvenhoven.net">mailinglists.bram@kuijvenhoven.net</a>> wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">
<div class="Ih2E3d">Sooky Boo wrote:<br>> I put the function in a for loop and passed I as the size paramater<br>> until it succeeded,<br>><br>> the size of the structure it expects is 40.<br><br></div>BTW what is the value of dwCount?<br>
<div class="Ih2E3d"><br>> 40 is equal to GetMem(sizeOf(SSLPROTOCOLS) + (2) * sizeOf(SSLPROTOCOL));<br><br></div>Indeed.<br>
<div class="Ih2E3d"><br>> so<br>><br>> protocols := LPSSLPROTOCOLS(GetMem(sizeOf(SSLPROTOCOLS) + (2) *<br>> sizeOf(SSLPROTOCOL)));<br>><br>> but the size of protocols^ is always 16 no matter what I put in GetMem.<br>
> (16 is the size of SSLPROTOCOL on its own)<br><br></div>Well, SSLPROTOCOL has size 12 and SSLPROTOCOLS has size 16. (Note that the SSLPROTOCOLS record type holds one DWORD and one SSLPROTOCOL.)<br><br>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.<br>
<br>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<br>
<br> protocols^.ProtocolList[i]<br><br>to access any i-th element (as long as you allocated a memory block that is large enough).<br>
<div class="Ih2E3d"><br>> How do I now GetMem for protocols^.ProtocolList.<br>><br>> //protocols^.ProtocolList := (GetMem((2) *<br>> sizeOf(SSLPROTOCOL)));//Error: Incompatible types: got "Pointer"<br>
> expected "Array[0..0] Of SSLPROTOCOL"<br><br></div>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'.<br>
<br>You can't do<br><br> var i:integer;<br> ...<br> i := GetMem(SizeOf(integer));<br><br>but you can do<br><br> var pi:^integer; // pointer to integer<br> ...<br> pi := ^integer(GetMem(SizeOf(integer)));<br><br>Bram<br>
<div class="Ih2E3d"><br>> On Sun, Feb 24, 2008 at 12:31 PM, Bram Kuijvenhoven<br>> <<a href="mailto:mailinglists.bram@kuijvenhoven.net">mailinglists.bram@kuijvenhoven.net</a><br></div>
<div class="Ih2E3d">> <mailto:<a href="mailto:mailinglists.bram@kuijvenhoven.net">mailinglists.bram@kuijvenhoven.net</a>>> wrote:<br>><br>> Sooky Boo wrote:<br></div>
<div class="Ih2E3d">> > On 2/23/08, ik <<a href="mailto:idokan@gmail.com">idokan@gmail.com</a> <mailto:<a href="mailto:idokan@gmail.com">idokan@gmail.com</a>>> wrote:<br>> >> {$PACKRECORDS C}<br>
> >><br>> >> On Sat, Feb 23, 2008 at 5:59 AM, Sooky Boo <<a href="mailto:sookyboo@gmail.com">sookyboo@gmail.com</a><br></div>
<div>
<div></div>
<div class="Wj3C7c">> <mailto:<a href="mailto:sookyboo@gmail.com">sookyboo@gmail.com</a>>> wrote:<br>> >>><br>> >>> Please help I am unsure how to port from C++ to fpc this code.<br>
> >>> The function this must be passed to says that the structure<br>> pointer or<br>> >> size<br>> >>> is invalid.<br>> >>><br>> >>> ----C++----<br>
> >>><br>> >>> typedef struct _SSLPROTOCOLS {<br>> >>> DWORD dwCount;<br>> >>> SSLPROTOCOL ProtocolList[1]; // array of 'count' structures<br>> >>> } SSLPROTOCOLS, FAR *LPSSLPROTOCOLS;<br>
> >>><br>> >>> ----My Attempt FPC Delphi Mode----<br>> >>><br>> >>> type SSLPROTOCOLS = record<br>> >>> dwCount :DWORD;<br>> >>> ProtocolList : array[0..0] of SSLPROTOCOL; // array of 'count'<br>
> structures<br>> >>> end;<br>> ><br>> > Unfortunately still doesn't work :(<br>><br>> The actual size of the SSLPROTOCOLS struct/record should match the<br>> value of dwCount. Allocating such a record probably should go like<br>
><br>> LPSSLPROTOCOLS(GetMem(sizeOf(SSLPROTOCOLS) + (dwCount - 1) *<br>> sizeOf(SSLPROTOCOL)))<br>><br>> Sometimes a dummy record is to be appended at the end of the array,<br>> similar to the null character at the end of a null-terminated<br>
> string. In that case it is possible that this 'sentinel' record is<br>> not counted in dwCount, but you should still allocate the space of<br>> course. Please refer to the documentation of the header file (I hope<br>
> there is).<br>><br>> I hope this helps.<br>><br>> Regards,<br>><br>> Bram<br>><br>> _______________________________________________<br>> fpc-devel maillist - <a href="mailto:fpc-devel@lists.freepascal.org">fpc-devel@lists.freepascal.org</a><br>
</div></div>> <mailto:<a href="mailto:fpc-devel@lists.freepascal.org">fpc-devel@lists.freepascal.org</a>><br>
<div class="Ih2E3d">> <a href="http://lists.freepascal.org/mailman/listinfo/fpc-devel" target="_blank">http://lists.freepascal.org/mailman/listinfo/fpc-devel</a><br>><br>><br>><br></div>> ------------------------------------------------------------------------<br>
<div>
<div></div>
<div class="Wj3C7c">><br>> _______________________________________________<br>> fpc-devel maillist - <a href="mailto:fpc-devel@lists.freepascal.org">fpc-devel@lists.freepascal.org</a><br>> <a href="http://lists.freepascal.org/mailman/listinfo/fpc-devel" target="_blank">http://lists.freepascal.org/mailman/listinfo/fpc-devel</a><br>
<br><br>_______________________________________________<br>fpc-devel maillist - <a href="mailto:fpc-devel@lists.freepascal.org">fpc-devel@lists.freepascal.org</a><br><a href="http://lists.freepascal.org/mailman/listinfo/fpc-devel" target="_blank">http://lists.freepascal.org/mailman/listinfo/fpc-devel</a><br>
</div></div></blockquote></div><br>