Home > Back-end >  Delphi call DLL C compiler, connection socket port times memory error
Delphi call DLL C compiler, connection socket port times memory error

Time:11-11

Delphi reference link library -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
The function GetPlateInfo (dIP, RST: Pchar) : Boolean; Cdecl; External 'LPRTCPTest. DLL';
Var
Form1: TForm1;
Implementation

{$R *. DFM}

Procedure TForm1. Button1Click (Sender: TObject);
Var
Rstbool: Boolean;
RST: Pchar;
IP: string;
PIP: Pchar;
Buff: Pchar;
The begin
Buff:=stralloc (256);
IP:='192.168.1.100';
PIP:=PChar (IP);
RST:=TestC (pIP);
Rstbool:=GetPlateInfo (pIP, buff);
Edit2. Text:=buff;
end;
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
DLL method
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Extern "C" _declspec (dllexport) bool GetPlateInfo (char * dftip, char * RST)
{
The SOCKET client;
The client=connetServer (" 192.168.1.100 ", 8131, TCP_BLOCK);

Char buff2 []="{\ n"
"" plateInfo \ ": " Beijing AF0236 \ ", \ n
"Color \ ": " yellow "" \ ", \ n
"Timestamp "" \ ", \ "2019-07-26 14:00:00 ", \ n
""" camera_state \ ": 1, \ n"
"} ";
Strcpy (RST buff2);
return true;
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Connectserver method
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
IP SOCKET connetServer (char * and an int port, int block)
{
The SOCKET client;
Struct sockaddr_in addr.
Unsigned long ip_addr=inet_addr (IP);

If (INADDR_NONE==ip_addr)
{
Printf (" input the right IP, eg: 192.168.1.22 \ n ");
Return INVALID_SOCKET;
}
If (initializeWinsockIfNecessary ()==0)
{
Printf (" WSAStartup error!/n ");
Return INVALID_SOCKET;
}
Client=socket (AF_INET SOCK_STREAM, IPPROTO_TCP);
If (INVALID_SOCKET==client)
{
Printf (" create a socket error/n ");
ReleaseWinsockIfNecessary ();
Return INVALID_SOCKET;
}

If (block==TCP_UNBLOCK)
{
MakeSocketNonBlocking (the client);
}
The else
{
MakeSocketBlocking (the client);
}

Addr. Sin_family=AF_INET;
Addr. Sin_port=htons (port);
Addr. Sin_addr. S_addr=ip_addr;
If (connect (client, (struct sockaddr *) & amp; Addr, sizeof (addr))!=0)
{
Printf (" connect error/n ");
CloseSocket (the client);
ReleaseWinsockIfNecessary ();
Return INVALID_SOCKET;
}
Return the client;
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

CodePudding user response:

Stdcall

CodePudding user response:

Parameter passing way consistent with the DLL

CodePudding user response:

Memory allocation and release to pointer
Procedure TForm1. Button1Click (Sender: TObject);
Var
Rstbool: Boolean;
RST: Pchar;
IP: string;
PIP: Pchar;
Buff: Pchar;
The begin
New (buff);
New (pIP).
IP:='192.168.1.100';
PIP:=PChar (IP);
Rstbool:=GetPlateInfo (pIP, buff);
Edit2. Text:=buff;
The Dispose (buff);
The Dispose (pIP);
end;
  • Related