I'm trying translate a piece of code from C to Delphi, that have as goal fill a struct until max limit allowed previous defined. I tried make the Delphi version more near of C code (even so not be a professional programmer).
But i noted that in my Delphi code the seems struct is filled only with 0 values (result of FillMemory()
) and not is being filled with correct values.
How i can solve it? below i show only the relevant code.
C: (code of reference)
struct Client
{
SOCKET connections[2];
DWORD uhid;
HWND hWnd;
BYTE *pixels;
DWORD pixelsWidth, pixelsHeight;
DWORD screenWidth, screenHeight;
HDC hDcBmp;
HANDLE minEvent;
BOOL fullScreen;
RECT windowedRect;
};
static Client g_clients[256];
static Client *GetClient(void *data, BOOL uhid)
{
for(int i = 0; i < 256; ++i)
{
if(uhid)
{
if(g_clients[i].uhid == (DWORD) data)
return &g_clients[i];
}
else
{
if(g_clients[i].hWnd == (HWND) data)
return &g_clients[i];
}
}
return NULL;
}
BOOL recordClient()
{
Client *client = NULL;
BOOL found = FALSE;
DWORD uhid;
uhid = 27650; // Some value, only as example here
memset(g_clients, 0, sizeof(g_clients));
client = GetClient((void *) uhid, TRUE);
if(client)
return FALSE;
for(int i = 0; i < 256; ++i)
{
if(!g_clients[i].hWnd)
{
found = TRUE;
client = &g_clients[i];
}
}
if(!found)
{
wprintf(TEXT("User %S kicked max %d users\n"), "185.242.4.203", 256);
return FALSE;
}
return TRUE;
}
Delphi:
type
PClient = ^Client;
Client = record
Connections: array [0 .. 1] of TSocket;
uhId,
pixelsWidth,
pixelsHeight,
screenWidth,
screenHeight: Cardinal;
_hWnd: HWND;
Pixels: PByte;
hDcBmp: HDC;
minEvent: THandle;
fullScreen: Boolean;
windowRect: TRect;
end;
var
Clients: array [0 .. 255] of Client;
//...
function GetClient(Data: Pointer; uhId: Boolean): PClient;
var
I: Integer;
begin
Result := nil;
for I := 0 to 255 do
begin
if uhId then
begin
if Clients[I].uhId = Cardinal(Data) then
begin
Result := @Clients[I];
Break;
end;
end
else
begin
if Clients[I]._hWnd = HWND(Data) then
begin
Result := @Clients[I];
Break;
end;
end;
end;
end;
function recordClient: Boolean;
var
_client: PClient;
_uhId: Cardinal;
found: Boolean;
I: Integer;
begin
Result := True;
FillMemory(@Clients, SizeOf(Clients), 0);
_uhId := 27650; // Some value, only as example here
_client := GetClient(@_uhId, True);
if _client <> nil then
begin
Result := False;
Exit;
end;
found := False;
for I := 0 to 255 do
begin
if Clients[I]._hWnd = 0 then
begin
found := True;
_client := @Clients[I];
end;
end;
if not found then
begin
Writeln(Format('Client %s rejected, max allowed is %d clients.' + #13,
['185.242.4.203', 256])); // Only example values
Result := False;
Exit;
end;
end;