Menu

problem handling WM_DEVICECHANGE

Dr deo
2009-03-21
2012-09-26
  • Dr deo

    Dr deo - 2009-03-21

    am trying to write an application that detects the insertion and removal of a removable device such as a flash drive.

    i am trying to do this by handling WM_DEVICECHANGE message. To my suprise the code failed to compile and because "PDEV_BROADCAST_HDR" not declared... and other similar errors yet i #include"windows.h" .Am lost....


    code snipet//this is the part i think has the problems. can send entire source if needed

    case WM_DEVICECHANGE:
    {
    PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR) lParam;
    switch (wParam)
    {
    case DBT_DEVICEARRIVAL:
    MessageBox(hwnd, "A device has been inserted.", "USB Notice", MB_OK);
    break;

                       case DBT_DEVICEREMOVECOMPLETE:
                          MessageBox(hwnd, "A device has been removed.", "USB Notice", MB_OK);
                             break;
                   }
           }
           break;
    

    compile log

    Compiler: Default compiler
    Building Makefile: "C:\Dev-Cpp\flash defender\Makefile.win"
    Executing make...
    make.exe -f "C:\Dev-Cpp\flash defender\Makefile.win" all
    g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

    main.cpp: In function LRESULT WindowProcedure(HWND__*, UINT, WPARAM, LPARAM)': main.cpp:92: error:PDEV_BROADCAST_HDR' undeclared (first use this function)

    main.cpp:92: error: (Each undeclared identifier is reported only once for each function it appears in.)
    main.cpp:92: error: expected ;' before "pHdr" main.cpp:95: error:DBT_DEVICEARRIVAL' undeclared (first use this function)
    main.cpp:98: error: `DBT_DEVICEREMOVECOMPLETE' undeclared (first use this function)

    make.exe: *** [main.o] Error 1

    Execution terminated

    other information you may find useful

    operating system : windows xp sp2
    compiling using devc++ 4.9.9.2

     
    • cpns

      cpns - 2009-03-22

      > paste me a link to read up more on the above two quotes.

      I think I already did: http://msdn.microsoft.com/en-us/library/aa383745(VS.85).aspx

      All you need to do is:

      inclide <windows.h>

      include <stdlib.h>

      int main()
      {
      printf( "_WIN32_WINNT: 0x%X\n", _WIN32_WINNT )
      printf( "WINVER: 0x%X\n", WINVER ) ;
      printf( "_WIN32_IE: 0x%X\n", _WIN32_IE) ;
      system( "pause" ) ;
      }

      If you compile the above code in Dev-C++ (which uses MinGW/GCC as its default toolchain in case you were not aware), you will get a different result than in say VC++ 2008. That is reasonable, MinGW/GCC 3.4.2 is older.

      If you take a look in teh windows headers such as <windows.h> and a number of ithers, you will see these macros are ised in conditional compilation to restrict or allow APIs that were introdices in successive versions of Windows.

      Clifford

       
    • cpns

      cpns - 2009-03-22

      Note that in the documentation http://msdn.microsoft.com/en-us/library/aa363246(VS.85).aspx it states that it required Win2K or later, and the header is <dbt.h>.

      Windows' headers use version macros to specify the minimum version of Windows you want your code to work with, so that any interface that requires a later version causes the code to fail compilation. The MinGW Win32 API uses a default version much earlier than Microsoft's current tools (Win95 I believe).

      Over time Microsoft have mandated various macros to specify version, and it is as well perhaps to set all of them appropriately, however if you want to know what macro guards this particular definition just take a look in the header. The version macros are detailed here: http://msdn.microsoft.com/en-us/library/aa383745(VS.85).aspx

      Now, having said all that, I just took a look at my VC++ 2008 Express Edition installation and there is no <dbt.h> header, and I text searched all available headers for PDEV_BROADCAST_HDR and it does not exist, so you may find that MinGW does not have it either. You may be able to fix that since I found teh file here: http://doc.ddart.net/msdn/header/include/dbt.h.html which looks like a blatent copyright breach to me - I'll leave that to your conscience. Regardless of the unlikeness of action being taken against you, I suggest that if this is for anything other than personal use, you avoid it. Especially if you intend to release code open-source. A legal solution is to create the header from information contained in the documentation.

      Clifford

       
    • Dr deo

      Dr deo - 2009-03-22

      thanks :)
      it now compiles ok.

                some answers and some questions
              -------------------------------------------------
      

      i came across this struct in the win32 help file under DEV_BROADCAST_HDR. I got he file from this link

      http://www.carabez.com/downloads.html


      typedef struct _DEV_BROADCAST_HDR {
      ULONG dbch_size;
      ULONG dbch_devicetype;
      ULONG dbch_reserved;
      } DEV_BROADCAST_HDR;
      typedef DEV_BROADCAST_HDR *PDEV_BROADCAST_HDR;


      so i think that explains the PDEV_BROADCAST_HDR part.
      Also the MinGW did not complain when i included <dbt.h> so it implies it must have found this header.

      However you mentioned some things that got me more inquisitive.......

      >Windows' headers use version macros to specify the minimum version of Windows you want your code to work with, so that any interface that requires a later version causes the code to fail compilation.

      >The MinGW Win32 API uses a default version much earlier than Microsoft's current tools

      paste me a link to read up more on the above two quotes. Fortunately in this case they were not the source of the problem but they might be next time. I would rather be prepared.

      Once again thanks in advance...

                                                                     dr Deo
      
       

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.