Ver Mensaje Individual
  #11  
Antiguo 06-11-2017
Avatar de duilioisola
[duilioisola] duilioisola is offline
Miembro Premium
 
Registrado: ago 2007
Ubicación: Barcelona, España
Posts: 1.734
Reputación: 20
duilioisola Es un diamante en brutoduilioisola Es un diamante en brutoduilioisola Es un diamante en bruto
Prueba con esta versión:
Notarás que hay una variable mas "P2" de tipo PChar.
Quizas la asignación directa del resultado de inet_ntoa que es PChar a String no le gusta.

Código Delphi [-]
function DameIPLocal: string;
var
  p : PHostEnt;
  s : array[0..128] of char;
  p2 : PChar;
  wVersionRequested : word;
  wsaData : TWSAData;
begin
  // Arranca la librería WinSock
  try
     wVersionRequested := MAKEWORD(1, 1);
     WSAStartup(wVersionRequested, wsaData);

     // Obtiene el nombre del PC
     GetHostName(@s, 128);
     p := GetHostByName(@s);

     // Obtiene la dirección IP y libera la librería WinSock
     p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
     Result := Result + p2;
     WSACleanup;
  except
     Result := '';
  end;
end;

Estuve leyendo que inet_ntoa devuelve null si ha encontrado un error.
Cita:
De la ayuda de Delphi 6:
The Windows Sockets inet_ntoa function converts a network address into a string in dotted format.

char FAR * inet_ntoa (
struct in_addr in
);


Parameters
[in] A structure which represents an Internet host address.

Remarks
This function takes an Internet address structure specified by the in parameter. It returns an ASCII string representing the address in ".'' notation as "a.b.c.d''. Note that the string returned by inet_ntoa resides in memory which is allocated by Windows Sockets. The application should not make any assumptions about the way in which the memory is allocated. The data is guaranteed to be valid until the next Windows Sockets function call within the same thread, but no longer.

Return Values
If no error occurs, inet_ntoa returns a char pointer to a static buffer containing the text address in standard ".'' notation. Otherwise, it returns NULL. The data should be copied before another Windows Sockets call is made.
Código Delphi [-]
function DameIPLocal: string;
var
  p : PHostEnt;
  s : array[0..128] of char;
  p2 : PChar;
  wVersionRequested : word;
  wsaData : TWSAData;
begin
  // Arranca la librería WinSock
  try
     wVersionRequested := MAKEWORD(1, 1);
     WSAStartup(wVersionRequested, wsaData);

     // Obtiene el nombre del PC
     GetHostName(@s, 128);
     p := GetHostByName(@s);

     // Obtiene la dirección IP y libera la librería WinSock
     p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
     if (Assigned(p2)) then
        Result := Result + p2
     else
        Result := '';
     WSACleanup;
  except
     Result := '';
  end;
end;
Responder Con Cita