Problem with something

This is a discussion on Problem with something within the Windows Programming forums, part of the Platform Specific Boards category; Code: #include <windows.h> #include <process.h> #include <wininet.h> #include <commctrl.h> #define FTP_FUNCTIONS_BUFFER_SIZE MAX_PATH+8 HINTERNET hFindFile,bay,say,file; HANDLE simple; HFONT hf; long lf; ...

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Problem with something

    Code:
    #include <windows.h>
    #include <process.h>
    #include <wininet.h>
    #include <commctrl.h>
    #define  FTP_FUNCTIONS_BUFFER_SIZE          MAX_PATH+8
    HINTERNET hFindFile,bay,say,file;
    HANDLE simple;
    HFONT hf;
    long lf;
    HDC hdc;
    DWORD car=0;
    char file_contents[1000000],empty[1000000],file_name[260],cserver[260],cuser[260],cpass[260],cdir[260],file_name2[260],lastopen[260];
    HWND hwnd,dropdown,ava,kustuta,edit,salvesta,nimi,hStatus,conserver,conuser,conpass,condir,conb,a,b,c,d;
    TCHAR szFName[FTP_FUNCTIONS_BUFFER_SIZE];
    void dconnect(void *P);
    void openfile(void *P);
    void dlistfiles(void *P);
    void savefile(void *P);
    void erase(void *P);
    int fan;
    WIN32_FIND_DATA FindFileData;
    char strFileName[260],test[260],tfile[260];
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    char szClassName[ ]="WindowsApp";
    int WINAPI WinMain(HINSTANCE hThisInstance,HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,int nFunsterStil){
        InitCommonControls();
        MSG messages;
        WNDCLASSEX wincl;
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;
        wincl.style = CS_DBLCLKS;
        wincl.cbSize = sizeof (WNDCLASSEX);
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0;
        wincl.cbWndExtra = 0;
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
        if (!RegisterClassEx (&wincl))
            return 0;
        hwnd=CreateWindowEx(0,szClassName,"FTPEditor",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
             CW_USEDEFAULT,644,492,HWND_DESKTOP,NULL,hThisInstance,NULL);
        DWORD dwStyle = GetWindowLong(hwnd, GWL_STYLE);
        dwStyle &= ~(WS_MAXIMIZEBOX|WS_MINIMIZEBOX);
        SetWindowLong(hwnd, GWL_STYLE, dwStyle);
        ShowWindow (hwnd, nFunsterStil);
        while (GetMessage (&messages, NULL, 0, 0)){
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
        return messages.wParam;
    }
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
        switch (message){
            case WM_CREATE:
                hdc = GetDC(NULL);
                lf = -MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);
                ReleaseDC(NULL,hdc);
                hf = CreateFont(lf, 0, 0, 0, FW_MEDIUM, FALSE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
                hStatus=CreateWindowEx(0,STATUSCLASSNAME,NULL,WS_CHILD|WS_VISIBLE,
                        0,0,0,0,hwnd,(HMENU)11445,GetModuleHandle(NULL),NULL);
                SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Ootan käsklusi...");
                a=CreateWindowEx(0,"Static","Server:",WS_CHILD|WS_VISIBLE,10,20,50,30,hwnd,(HMENU)10001,GetModuleHandle(NULL),NULL);
                conserver=CreateWindowEx(0,"Edit","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,65,15,80,25,hwnd,(HMENU)10002,GetModuleHandle(NULL),NULL);
                b=CreateWindowEx(0,"Static","User:",WS_CHILD|WS_VISIBLE,150,20,70,30,hwnd,(HMENU)10003,GetModuleHandle(NULL),NULL);
                conuser=CreateWindowEx(0,"Edit","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,225,15,80,25,hwnd,(HMENU)10004,GetModuleHandle(NULL),NULL);
                c=CreateWindowEx(0,"Static","Pass:",WS_CHILD|WS_VISIBLE,310,20,70,30,hwnd,(HMENU)10005,GetModuleHandle(NULL),NULL);
                conpass=CreateWindowEx(0,"Edit","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL|ES_PASSWORD,385,15,80,25,hwnd,(HMENU)10006,GetModuleHandle(NULL),NULL);
                d=CreateWindowEx(0,"Static","Folder:",WS_CHILD|WS_VISIBLE,470,20,50,30,hwnd,(HMENU)10007,GetModuleHandle(NULL),NULL);
                condir=CreateWindowEx(0,"Edit","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,525,15,80,25,hwnd,(HMENU)10008,GetModuleHandle(NULL),NULL);
                conb=CreateWindowEx(0,"Button","Connect",WS_CHILD|WS_VISIBLE|WS_BORDER|BS_PUSHBUTTON,290,55,60,25,hwnd,(HMENU)10009,GetModuleHandle(NULL),NULL);
                SendMessage(a,WM_SETFONT,(WPARAM)hf,TRUE);
                SendMessage(b,WM_SETFONT,(WPARAM)hf,TRUE);
                SendMessage(c,WM_SETFONT,(WPARAM)hf,TRUE);
                SendMessage(d,WM_SETFONT,(WPARAM)hf,TRUE);
                SendMessage(conb,WM_SETFONT,(WPARAM)hf,TRUE);
                SendMessage(condir,WM_SETFONT,(WPARAM)hf,TRUE);
                SendMessage(conpass,WM_SETFONT,(WPARAM)hf,TRUE);
                SendMessage(conuser,WM_SETFONT,(WPARAM)hf,TRUE);
                SendMessage(conserver,WM_SETFONT,(WPARAM)hf,TRUE);
                break;
            case 11122:{
                  DestroyWindow(a);
                  DestroyWindow(b);
                  DestroyWindow(c);
                  DestroyWindow(d);
                  DestroyWindow(conserver);
                  DestroyWindow(conuser);
                  DestroyWindow(conpass);
                  DestroyWindow(condir);
                  DestroyWindow(conb);
                nimi=CreateWindowEx(0,"Edit","",WS_BORDER|WS_VISIBLE|WS_CHILD|ES_AUTOHSCROLL,
                     60,10,100,25,hwnd,(HMENU)25553,GetModuleHandle(NULL),NULL);
                dropdown=CreateWindowEx(0,"Combobox","",
                         WS_BORDER|WS_VISIBLE|WS_CHILD|LBS_STANDARD|LBS_USETABSTOPS|CBS_AUTOHSCROLL,
                         170,10,150,200,hwnd,(HMENU)25554,GetModuleHandle(NULL),NULL);
                ava=CreateWindowEx(0,"Button","Open",WS_BORDER|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,340,
                    10,60,25,hwnd,(HMENU)25555,GetModuleHandle(NULL),NULL);
                salvesta=CreateWindowEx(0,"Button","Save",WS_BORDER|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,410,
                         10,80,25,hwnd,(HMENU)25556,GetModuleHandle(NULL),NULL);
                kustuta=CreateWindowEx(0,"Button","Delete",WS_BORDER|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,500,
                         10,75,25,hwnd,(HMENU)25557,GetModuleHandle(NULL),NULL);
                edit=CreateWindowEx(0,"Edit","",WS_BORDER|WS_VISIBLE|WS_CHILD|ES_AUTOHSCROLL|ES_AUTOVSCROLL|ES_MULTILINE|WS_HSCROLL|WS_VSCROLL,
                     10,50,624,385,hwnd,(HMENU)25558,GetModuleHandle(NULL),NULL);
                SendMessage(hStatus,WM_SETFONT,(WPARAM)hf,TRUE);
                SendMessage(nimi,WM_SETFONT,(WPARAM)hf,TRUE);
                SendMessage(dropdown,WM_SETFONT,(WPARAM)hf,TRUE);
                SendMessage(ava,WM_SETFONT,(WPARAM)hf,TRUE);
                SendMessage(salvesta,WM_SETFONT,(WPARAM)hf,TRUE);
                SendMessage(kustuta,WM_SETFONT,(WPARAM)hf,TRUE);
                SendMessage(edit,WM_SETFONT,(WPARAM)hf,TRUE);
                _beginthread(dlistfiles,0,NULL);
            }
            case WM_COMMAND:
                switch(wParam){
                    case 25555:
                        _beginthread(openfile,0,NULL);
                        break;
                    case 25556:
                        _beginthread(savefile,0,NULL);
                        break;
                    case 25557:
                        _beginthread(erase,0,NULL);
                        break;
                    case 10009:
                        GetWindowText(conserver,cserver,260);
                        GetWindowText(conuser,cuser,260);
                        GetWindowText(conpass,cpass,260);
                        GetWindowText(condir,cdir,260);
                        _beginthread(dconnect,0,NULL);
                        break;
                }
                break;
            case WM_SIZE:
                SetWindowPos(hwnd,0,0,0,644,492,SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_SHOWWINDOW);
                break;
            case WM_DESTROY:
                PostQuitMessage(0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
        return 0;
    }
    void dconnect(void *P){
        SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Oota... Ühendan");
        bay=InternetOpen(0,INTERNET_OPEN_TYPE_DIRECT,0,0,0);
        say=InternetConnect(bay,cserver,INTERNET_DEFAULT_FTP_PORT,cuser,cpass,INTERNET_SERVICE_FTP,INTERNET_FLAG_TRANSFER_ASCII,0);
        if(say==NULL){
        MessageBox(hwnd,"Couldn't connect!","Error",MB_OK);
        }
        else{
        DestroyWindow(a);
        DestroyWindow(b);
        DestroyWindow(c);
        DestroyWindow(d);
        DestroyWindow(conserver);
        DestroyWindow(conuser);
        DestroyWindow(conpass);
        DestroyWindow(condir);
        DestroyWindow(conb);
        SendMessage(hwnd,11122,0,0);
        }
        SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Waiting for commands");
        return;
    }
    void dlistfiles(void *P){
        WaitForSingleObject(simple,INFINITE);
        SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Wait... Creating file list");
        FtpSetCurrentDirectory(say,cdir);
        if(hFindFile){
        InternetCloseHandle(hFindFile);
        }
        strcpy(strFileName,"");
        hFindFile = FtpFindFirstFile(say, "*.*", &FindFileData, INTERNET_FLAG_RELOAD, 0); 
        if(hFindFile){
        SendMessage(dropdown,CB_RESETCONTENT,0,0);
        if((FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY){
        }
        else{
        strcpy(strFileName,FindFileData.cFileName);
        SendMessage(dropdown,CB_ADDSTRING,0,(LPARAM)strFileName);
        }
        }
        while(InternetFindNextFile(hFindFile, &FindFileData)){
        if((FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY){
        }
        else{
        strcpy(strFileName,FindFileData.cFileName);
        SendMessage(dropdown,CB_ADDSTRING,0,(LPARAM)strFileName);
        }
        }
        SendMessage(dropdown,CB_SETCURSEL,0,0);
        InternetCloseHandle(hFindFile);
        if(file_name){
        fan=(int)SendMessage(dropdown,
        CB_FINDSTRINGEXACT,
        -1,
        (LPARAM)file_name);
        if(fan!=CB_ERR){
        SendMessage(dropdown,CB_SETCURSEL,(WPARAM)fan,0);
        }
        }
        SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Waiting for commands");
        return;
    }
    void openfile(void *P){
        SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Wait.. Opening file");
        GetWindowText(dropdown,file_name,260);
        SetWindowText(nimi,file_name);
        strcpy(file_contents,"");
        car=0;
        SetWindowText(edit,"");
        file=FtpOpenFile(say,file_name,GENERIC_READ,FTP_TRANSFER_TYPE_ASCII,0);
        InternetReadFile(file,file_contents,1000000,&car);
        InternetCloseHandle(file);
        SetWindowText(edit,file_contents);
        strcpy(lastopen,file_name);
        SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Waiting for commands");
        return;
    }
    void savefile(void *P){
        SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Wait.. Saving file");
        GetWindowText(nimi,file_name,1000000);
        GetWindowText(edit,file_contents,1000000);
        car=0;
        FtpDeleteFile(say,file_name);
        file=FtpOpenFile(say,file_name,GENERIC_WRITE,FTP_TRANSFER_TYPE_ASCII,0);
        InternetWriteFile(file,file_contents,strlen(file_contents),&car);
        InternetCloseHandle(file);
        strcpy(lastopen,file_name);
        _beginthread(dlistfiles,0,NULL);
        SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Waiting for commands");
    }
    void erase(void *P){
        if(MessageBox(hwnd,"Are you sure you want to delete this file?","Delete",MB_YESNO)==IDYES){
            SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Wait.. Deleting file");    
            GetWindowText(dropdown,file_name2,260);
            FtpDeleteFile(say,file_name2);
            _beginthread(dlistfiles,0,NULL);
            if(strcmp(lastopen,file_name2)==0){
            SetWindowText(nimi,"");
            SetWindowText(edit,"");
            }
            SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Waiting for commands");
        }
    }
    With this program I can connect to any ftp server and edit files there. But if I load a bigger file and after that a smaller file, the smaller file only overwrites the beginning of bigger file.

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Tried different ways to reset variables etc...

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You need to null terminate the returned string before use:
    Code:
       file=FtpOpenFile(say,file_name,GENERIC_READ,FTP_TR  ANSFER_TYPE_ASCII,0);
        InternetReadFile(file,file_contents,sizeof(file_contents) - 1,&car);
       file_contents[car] = '\0';
    Remember, that all content received over the network must be considered unreliable and potentially hostile.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21