Menu

Problems compiling a windows tutorial

2003-03-11
2012-09-26
  • Jon Edwards

    Jon Edwards - 2003-03-11

    Hi everyone I am working my way through some windows tutorials from www.winprog.org  I am using Dev Cpp 4.9.7.7 (latest critical update) and am having a problem when I compile this tutorial.

    Its quite a long piece of code but I will include it here in the hope that someone out there may be able to help the problem occurs at line 86 and produces the error "illegal conversion from void to int"

    Source code:

    #include <windows.h>

    #include "resource.h"

    BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
        switch(Message)
        {
            case WM_INITDIALOG:
                // This is where we set up the dialog box, and initialise any default values
               
                SetDlgItemText(hwnd, IDC_TEXT, "This is a string");
                SetDlgItemInt(hwnd, IDC_NUMBER, 5, FALSE);
            break;
            case WM_COMMAND:
                switch(LOWORD(wParam))
                {
                    case IDC_ADD:
                    {
                         // When somebody presses the ADD button first we get the
                         // number of times to add the given string that was entered
                         // in the Number Of box
                        
                         BOOL bSuccess;
                         int ntimes = GetDlgItemInt(hwnd, IDC_NUMBER, &bSuccess, FALSE);
                         if(bSuccess)
                         {
                             // Then we get the string they entered
                             // First we need to find how long it is so that we can
                             // allocate some memory
                             int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT));
                             if(len>0)
                             {
                                 // Now we allocate and get the string into our buffer
                                
                                 int i;
                                 char* buf;
                                
                                 buf = (char*)GlobalAlloc(GPTR, len + 1);
                                 GetDlgItemText(hwnd, IDC_TEXT, buf, len + 1);
                                
                                 // Now we add the string to the listbox however many times
                                 // the user asked us to
                                
                                 for(i = 0;i < ntimes;i++)
                                 {
                                     int index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);
                                    
                                     // Here we are associating the value ntimes with the item
                                     // just for the heck of it, we'll use it to display later.
                                     // Normally you would put some more useful data here, such
                                     // as a pointer.
                                     SendDlgItemMessage(hwnd, IDC_LIST, LB_SETITEMDATA, (WPARAM) index, (LPARAM) ntimes);
                                 }
                                
                                 // Don't forget to free the memory!
                                 GlobalFree((HANDLE)buf);
                             }
                             else
                             {
                                 MessageBox(hwnd, "You didn't enter anything!", "Warning", MB_OK);
                             }
                         }
                         else
                         {
                             MessageBox(hwnd, "Couldn't translate that number :(", "Warning", MB_OK);
                         }
                   
                    }
                    break;
                    case IDC_REMOVE:
                    {
                        // When the user clicks the remove button, we first get the number
                        // of selected items
                       
                        HWND hList = GetDlgItem(hwnd, IDC_LIST);
                        int count = SendMessage(hList, LB_GETSELCOUNT, 0, 0);
                        if(count != LB_ERR)
                        {
                            if(count != 0)
                            {
                                // And then allocate room to store the list of selected items.
                               
                                int i;
                                
                                int *buf = GlobalAlloc(GPTR, sizeof(int)*count);// Error occurs here
                                SendMessage(hList, LB_GETSELITEMS, (WPARAM)count, (LPARAM)buf);
                               
                                // Now we loop through the list and remove each item
                                // that was selected
                               
                                // WARNING!!!
                                // We loop backwards because if we removed items
                                // from top to bottom it would change the indexes of the other
                                // items!!!
                               
                                for(i = count-1;i >= 0;i--)
                                {
                                    SendMessage(hList, LB_DELETESTRING, (WPARAM)buf[i], 0);
                                }
                                GlobalFree(buf);
                            }
                            else
                            {
                                MessageBox(hwnd, "No items selected.", "Warning", MB_OK);
                            }
                        }
                        else
                        {
                            MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK);
                        }
                    }
                    break;
                    case IDC_CLEAR:
                        SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT,0 ,0);
                    break;
                    case IDC_LIST:
                        switch(HIWORD(wParam))
                        {
                            case LBN_SELCHANGE:
                            {
                                // Get the number of items selected
                               
                                HWND hList = GetDlgItem(hwnd, IDC_LIST);
                                int count = SendMessage(hList, LB_GETSELCOUNT, 0, 0);
                                if(count != LB_ERR)
                                {
                                    // We only want to continue if one and only one item is
                                    // selected
                                   
                                    if(count == 1)
                                    {
                                        // Since we know ahead of time we're only getting one
                                        // index, theres no need to allocate an array.
                                       
                                        int index;
                                        int err = SendMessage(hList, LB_GETSELITEMS, (WPARAM)1, (LPARAM)&index);
                                        if(err != LB_ERR)
                                        {
                                       
                                            // Get the data we associated with the item above
                                            // (the number of times it was added)
                                           
                                            int data = SendMessage(hList, LB_GETITEMDATA, (WPARAM)index, 0);
                                           
                                            SetDlgItemInt(hwnd, IDC_SHOWCOUNT, data, FALSE);
                                        }
                                        else
                                        {
                                            MessageBox(hwnd, "Error getting selected item :(", "Warning", MB_OK);
                                        }
                                    }
                                    else
                                    {
                                        // No items selected, or more than one
                                        // Either way we aren't going to process this.
                                        SetDlgItemText(hwnd, IDC_SHOWCOUNT, "-");
                                    }
                                }
                                else
                                {
                                    MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK);
                                }
                            }
                            break;
                        }
                    break;
                }
            break;
            case WM_CLOSE:
                EndDialog(hwnd, 0);
            break;
            default:
                return FALSE;
        }
        return TRUE;
    }

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
    }   
       

    Compiler output:

    Compiler: Default compiler
    Building Makefile: "C:\Documents and Settings\Jon Edwards.HOME-MAIN-PC\My Documents\Dev-Cpp Projects and Tutorials\Windows Programs from the forger tutorials\Control Tutorial 1\Makefile.win"
    Executing  make clean
    rm -f "Control 1.o"  "CTL 1.exe"g++.exe -c "Control 1.cpp" -o "Control 1.o" -I"C:/Dev-Cpp/include"  -I"C:/Dev-Cpp/include/c++"  -I"C:/Dev-Cpp/include/akrip"  -I"C:/Dev-Cpp/include/c++/mingw32"  -I"C:/Dev-Cpp/include/c++/mingw32/bits"  -I"C:/Dev-Cpp/include/c++/backward"  -I"C:/Dev-Cpp/include/c++/bits"  -I"C:/Dev-Cpp/include/c++/ext"  -I"C:/Dev-Cpp/include/FL"  -I"C:/Dev-Cpp/include/GL"  -I"C:/Dev-Cpp/include/sys"  Control 1.cpp: In function `BOOL DlgProc(HWND__*, unsigned int, unsigned int,    long int)':Control 1.cpp:86: invalid conversion from `void*' to `int*'make.exe: *** ["Control] Error 1Execution terminated

    any help here would be greatly appreciated

    Thankyou in advance

    Jon

     
    • Jon Edwards

      Jon Edwards - 2003-03-11

      error occurs at the line

      int *buf = GlobalAlloc(GPTR, sizeof(int)*count);

       
    • Curtis Sutter

      Curtis Sutter - 2003-03-13

      Try
      int *buf = (int*)GlobalAlloc(GPTR, sizeof(int)*count);

      Since GlobalAlloc() returns a void you need to typecast it...  I maybe wrong since I am not near a Dev compiler right now to test it.

      Curtis

       
      • Jon Edwards

        Jon Edwards - 2003-03-17

        Thanks Curtis for that it compiles fine now but it wont run at all -sigh- :-(

         
    • Curtis Sutter

      Curtis Sutter - 2003-03-17

      E-mail your code, I'll look into it...  curtis8@hotmail.com

      Curtis

       
    • Jon Edwards

      Jon Edwards - 2003-04-05

      Hiya Curtis any joy yet

      Jon

       
    • Tobias Jakobi

      Tobias Jakobi - 2003-04-08

      when using typecasting in C++ you should rather use the new C++ style casting than the old bracket-casting C code.

      instead of
      int* int_ptr = (int*)get_a_void_pointer();

      try casting with one of these:
      static_cast, dynamic_cast, const_cast, reinterpret_cast

      syntax is:
      int* int_ptr = X_cast<int*>(get_a_void_pointer());

      the only thing you have to figure out is when to use what type of cast.

      cya
      liquid

       
    • Jon Edwards

      Jon Edwards - 2003-04-19

      Hi Liquid I'll certainly give that a try

      but I'm thinking that maybe the compiled exe isn't calling the resource, it seems to run for a few seconds and then quits out

       
    • Curtis Sutter

      Curtis Sutter - 2003-04-21

      If I got your mail Jon, hotmail trashed it.  If you haven't already found a solution, try again.  Try with a really obvious Subject so I don't delete it by accident...

      Curtis

       
    • Jon Edwards

      Jon Edwards - 2003-04-21

      Hi Curtis how about if I sent the separate files as a zipped or similar attachment? I know its risky but pretty much everything gets macafee'd to death with the hotmail system, or, alternatively I could upload the job lot to one of my websites and add a link on that.

      which would you prefer? because I would dearly love to get to the bottom of this

      Thanks again

      Jon -"I will eventually work out windows programming"- Edwards :-)

       
    • Curtis Sutter

      Curtis Sutter - 2003-04-22

      Either or, I use Outlook Express...

      Curtis

       
      • Jon Edwards

        Jon Edwards - 2003-04-23

        Ok I've uploaded a zip of all the files to the following location:

        www.microvision.org.uk/programs.htm

        and right click on the link there, as I said, it seems to compile fine but when I try to run the exe it runs for maybe 1 second and then stops. The actual tutorial is located at winprog.org (no www)

        all the best

        Jon Edwards

         
    • Jon Edwards

      Jon Edwards - 2003-04-26

      Sorry My Mistake !!

      either http://www.winprog.org    or

               http://winprog.org work

      btw: anyone interested can pick up the source codes at the link in the previous post

      any help would be appreciated

      Jon Edwards

      edjon2000@connectfree.co.uk

       
    • Curtis Sutter

      Curtis Sutter - 2003-04-26

      I have sent you a solution via e-mail Jon, complies here, but I don't see a thing...  Maybe you will have better luck :P

      Curtis

       
    • Jon Edwards

      Jon Edwards - 2003-04-26

      about to try it

      it compiles fine it just don't run

      I'll let you know

      Jon Edwards
      edjon2000@connectfree.co.uk

       
    • Jon Edwards

      Jon Edwards - 2003-04-26

      Alas no

      Now won't compile
      reports error

      "10 C:\Documents and Settings\Jon Edwards.HOME-MAIN-PC\My Documents\Dev-Cpp Projects and Tutorials\Windows Programs from the forger tutorials\Control Tutorial 1\CTL_1_private.rc:4
      empty file name in #include"

      Oh heck!!

      Jon Edwards

       
    • Curtis Sutter

      Curtis Sutter - 2003-04-26

      I had that one as well... do not fear.  Just re-create your project withthe new files, all will be well...

      Curtis

       
    • B Bishwa

      B Bishwa - 2003-06-03

      I downloaded the zip, extracted all files.
      copied control 1.rc, control 1.cpp, and resource.h to a blank directory,
      did not like  space in name (personal dislike only) so removed spaces, created a new blank project (C++). added the three files (naturally removed the default main.cpp bunged in by devcpp) . and hey presto compiles and works.
      A bit puzzled..  (just to confirm .. adds string to array specified times .. removes selected string etc etc)

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.