Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Internet (https://www.clubdelphi.com/foros/forumdisplay.php?f=3)
-   -   Obtener el IP en Windows Server 2016 (https://www.clubdelphi.com/foros/showthread.php?t=92446)

rdaniel2000 31-10-2017 20:40:32

Obtener el IP en Windows Server 2016
 
Hola,

Hay una funcion como esta para obtener el IP:

function ObtenerIp :string; //Es el truco 82 de trucomania (editado)
var
wVersionRequested : WORD;
wsaData : TWSAData;
p : PHostEnt;
s : array[0..128] of char;
begin
wVersionRequested := MAKEWORD(1, 1);
WSAStartup(wVersionRequested, wsaData);
GetHostName(@s, 128);
p := GetHostByName(@s);
Result := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
WSACleanUP;
end;

En todos los Windows funciona bien, pero ya en Windows Server 2016 Ya no funciona se genera un excepcion de error.

Alguien ya lo soluciono???

maeyanes 31-10-2017 21:15:57

Hola...

¿Podrías especificar cual es el error que te muestra? Por que así como preguntas, pues como que no das mucha información.


Saludos...

Casimiro Notevi 31-10-2017 21:40:11

Cita:

Empezado por rdaniel2000 (Mensaje 522172)
Hola

No olvides poner los tags al código fuente, ejemplo:



Gracias :)

duilioisola 03-11-2017 09:20:29

Yo utilizo esa funcion en Windows 2016 Server y funciona correctamente.
Deberás decirnos qué excepción genera para tí. (Mensaje completo y sin traducir)

rdaniel2000 03-11-2017 19:25:51

Cita:

Empezado por duilioisola (Mensaje 522219)
Yo utilizo esa funcion en Windows 2016 Server y funciona correctamente.
Deberás decirnos qué excepción genera para tí. (Mensaje completo y sin traducir)

Creo que va a ser problema del Windows que tengo... que Build tienes del Windows Server 2016???

La version que tengo es:

Version 1607 (Compilacion de SO 14393.0) (En español)

Casimiro Notevi 03-11-2017 19:30:17

Cita:

Empezado por rdaniel2000 (Mensaje 522242)
Creo que va a ser problema del Windows que tengo... que Build tienes del Windows Server 2016???
La version que tengo es:
Version 1607 (Compilacion de SO 14393.0) (En español)

Eso no tiene nada que ver, contesta la pregunta que te han hecho :rolleyes:

rdaniel2000 03-11-2017 19:37:31

Cita:

Empezado por maeyanes (Mensaje 522173)
Hola...

¿Podrías especificar cual es el error que te muestra? Por que así como preguntas, pues como que no das mucha información.


Saludos...



Este es el Error que me marca, usando Delphi 7 / Sobre Windows Server 2016 Version 1607 (Compilacion de SO 14393.0)

Access violation at address 004764C1 in module 'Project1.exe'. Read of address 0000000C


CODIGO:

Código Delphi [-]


function ObtenerIp :string; //Es el truco 82 de trucomania (editado)
var
wVersionRequested : WORD;
wsaData : TWSAData;
p : PHostEnt;
s : array[0..128] of char;
begin
try
wVersionRequested := MAKEWORD(1, 1);
WSAStartup(wVersionRequested, wsaData);
GetHostName(@s, 128);
p := GetHostByName(@s);
Result := iNet_ntoa(PInAddr(p^.h_addr_list^)^);    // Aqui es donde se genera la Excepcion
WSACleanUP;
except
 on E: Exception do
   ShowMessage(E.Message);
end;
end;

Casimiro Notevi 03-11-2017 20:48:14

Veo que es de aquí.

Yo uso lo mismo con una pequeña variación y funciona perfectamente:
Código Delphi [-]
function getIPnumber: string;
const
  _MAXSIZE_ = 48;
var
  buffer:Array [0.._MAXSIZE_+1] of char;
  PuntHost: PHostEnt;
  wVersionRequested: WORD;
  wsaData: TWSAData;
begin
  Result := '';
  wVersionRequested := MAKEWORD( 1, 1 );
  WSAStartup( wVersionRequested, wsaData );
  GetHostName( @buffer, _MAXSIZE_ );
  PuntHost := GetHostByName( @buffer );
  Result := iNet_ntoa( PInAddr( PuntHost^.h_addr_list^ )^ );
  WSACleanup;
end;

rdaniel2000 03-11-2017 22:00:33

Cita:

Empezado por Casimiro Notevi (Mensaje 522249)
.
Yo uso lo mismo con una pequeña variación y funciona perfectamente:
Código Delphi [-]
function getIPnumber: string;
const
  _MAXSIZE_ = 48;
var
  buffer:Array [0.._MAXSIZE_+1] of char;
  PuntHost: PHostEnt;
  wVersionRequested: WORD;
  wsaData: TWSAData;
begin
  Result := '';
  wVersionRequested := MAKEWORD( 1, 1 );
  WSAStartup( wVersionRequested, wsaData );
  GetHostName( @buffer, _MAXSIZE_ );
  PuntHost := GetHostByName( @buffer );
  Result := iNet_ntoa( PInAddr( PuntHost^.h_addr_list^ )^ );
  WSACleanup;
end;

Ya use tu codigo y me vuelve a marcar el mismo error de Excepcion...

Por ese motivo, yo creo que debe ser un problema en el Build del Windows Server... ya que cuando lo ejecuto en Windows 7, 10 o 2012 Server funciona muy bien...

Casimiro Notevi 03-11-2017 22:04:32

A ver si alguien lo prueba con la misma versión que tienes tú.

duilioisola 06-11-2017 17:09:51

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;

rdaniel2000 06-11-2017 19:32:23

Cita:

Empezado por duilioisola (Mensaje 522287)
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^)^);
     if (Assigned(p2)) then
        Result := Result + p2
     else
        Result := '';
     WSACleanup;
  except
    on E: Exception do
      Result := E.Message;
  end;
end;


Le agregue a tu funcion el Mensaje de Excepcion para saber si aun continua y desafortunadamente la Excepcion persiste.

rdaniel2000 06-11-2017 21:12:21

Confirmado, Instale Windows Server 2016 Version 1607 (Compilacion de SO 14393.0)

Usando el ISO, sin aplicar ninguna actualizacion de Windows y el Error se presenta al obtener el IP, y ademas de eso muchos otros errores con los componentes de Delphi.

Procedi a Actualizar Windows (KB4035631)

Y con esta actualizacion ya funcionan todas las Funciones anteriores...

Como lo habia dicho, la Instalacion por default de Windows tiene errores que han sido corregidos por Micro$oft.


Saludos,.

Vero190 24-11-2017 21:11:21

Justo lo que necesitaba! Gracias!


La franja horaria es GMT +2. Ahora son las 23:15:58.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi