[Vista][C#] HDMI connect event afvangen zonder Window

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • liquid_ice
  • Registratie: Februari 2001
  • Laatst online: 08-09 14:43
Ik wil op een of andere manier een event onderscheppen dat mijn HDMI kabel connect en eentje bij disconnect. Gewoon in een simple (liefst command line) programmatje

M'n idee was om mbv Control.WndProc de events op te vangen en dan te gaan filteren. zoals het mooie voorbeeld op:
MSDN: Control.WndProc Method (System.Windows.Forms)

Maar dit voorbeeld start een Window op van 300 bij 300
code:
1
this.Size = new System.Drawing.Size(300,300);


Ik heb geprobeerd om .Size(0,0); in te vullen, maar dan nog steeds krijg ik een mini Windows met de bekende 3 knoppen.
Ook heb ik this.Visible = false; geprobeerd, maar ook dat leverde niks op.

Klus page: http://klusthuis.blogspot.com


Acties:
  • 0 Henk 'm!

  • RobIII
  • Registratie: December 2001
  • Niet online

RobIII

Admin Devschuur®

^ Romeinse Ⅲ ja!

(overleden)
MSDN: .NET Matters: Handling Messages in Console Apps
Dat was toch wel 3 hele seconden googlen: [google=console application window messages]

[ Voor 26% gewijzigd door RobIII op 30-09-2011 09:16 ]

There are only two hard problems in distributed systems: 2. Exactly-once delivery 1. Guaranteed order of messages 2. Exactly-once delivery.

Je eigen tweaker.me redirect

Over mij


Acties:
  • 0 Henk 'm!

  • liquid_ice
  • Registratie: Februari 2001
  • Laatst online: 08-09 14:43
Als je de juiste termen invult gaat het inderdaad snel, anders kan je een hele tijd bezig zijn en nog niks vinden.

Klus page: http://klusthuis.blogspot.com


Acties:
  • 0 Henk 'm!

  • liquid_ice
  • Registratie: Februari 2001
  • Laatst online: 08-09 14:43
Waarschijnlijk weer zo'n simpele vraag, maar ik kan het echt niet vinden en zit al de hele avond te zoeken.

Met behulp van WndProc heb ik kunnen achterhalen dat het WM_DEVICECHANGE event 2 keer komt als ik de HDMI erin doe, met als WParam DBT_DEVNODES_CHANGED.

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
            protected override void WndProc(ref Message message)
            {
                //you may find these definitions in dbt.h and winuser.h
                const int WM_DEVICECHANGE = 0x0219;
                const int DBT_DEVNODES_CHANGED = 0x007;
                
                if (message.Msg == WM_DEVICECHANGE)
                {
                    if ((int)message.WParam == DBT_DEVNODES_CHANGED)
                    {
                        // handle event
                    }

                }
                base.WndProc(ref message);
            }


Als ik dan kijk op:
msdn.microsoft.com/en-us/library/windows/desktop/aa363211(v=vs.85).aspx
dan zie ik staan:
code:
1
There is no additional information about which device has been added to or removed from the system. Applications that require more information should register for device notification using the RegisterDeviceNotification function.

en die heeft weer een mooi example staan op:
MSDN: Registering for Device Notification

code:
1
2
3
4
5
6
7
8
9
10
11
12
    DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;

    ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
    NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
    NotificationFilter.dbcc_classguid = InterfaceClassGuid;

    *hDeviceNotify = RegisterDeviceNotification( 
        hWnd,                       // events recipient
        &NotificationFilter,        // type of device
        DEVICE_NOTIFY_WINDOW_HANDLE // type of recipient handle
        );


Maar m'n compiler kent DEV_BROADCAST_DEVICEINTERFACE niet. Op verscheidene websites staat Win32.DEV_BROADCAST_DEVICEINTERFACE, maar Win32 is ook niet bekent. Ik heb bij de "Add Reference" gekeken, maar geen Win32, Microsoft.Win32 of System.Win32

Als ik de mscorlib wil kiezen krijg ik de error:

A reference to 'mscorlib' could not be added. This component is automatically referenced by the project system and cannot be referenced directly.

Klus page: http://klusthuis.blogspot.com


Acties:
  • 0 Henk 'm!

  • MSalters
  • Registratie: Juni 2001
  • Laatst online: 13-09 00:05
DEV_BROADCAST_DEVICEINTERFACE is een Windows (SDK) type, geen CLR type. De code waar je naar linkt is dus ook geen C# code, maar C/C++. "C# is expressly unaware of C-based data types used in the Windows API"

Man hopes. Genius creates. Ralph Waldo Emerson
Never worry about theory as long as the machinery does what it's supposed to do. R. A. Heinlein


Acties:
  • 0 Henk 'm!

  • RobIII
  • Registratie: December 2001
  • Niet online

RobIII

Admin Devschuur®

^ Romeinse Ⅲ ja!

(overleden)
Als je met de windows API te maken hebt, kijk dan eens (en zoek dan eens) op pinvoke.net:

http://pinvoke.net/defaul...erDeviceNotification.html
http://pinvoke.net/defaul...CAST_DEVICEINTERFACE.html

Je moet 't niet zoeken in de references; daar ga je alleen .Net namespaces vinden. Je bent nu "om" het .Net framework heen rechtstreeks met de Windows API aan 't babbelen.

[ Voor 3% gewijzigd door RobIII op 30-09-2011 23:49 ]

There are only two hard problems in distributed systems: 2. Exactly-once delivery 1. Guaranteed order of messages 2. Exactly-once delivery.

Je eigen tweaker.me redirect

Over mij


Acties:
  • 0 Henk 'm!

  • liquid_ice
  • Registratie: Februari 2001
  • Laatst online: 08-09 14:43
[quoute]
Je bent nu "om" het .Net framework heen rechtstreeks met de Windows API aan 't babbelen.
[/quote]

Is dat dan überhaupt wel de weg om te gaan dan?

Klus page: http://klusthuis.blogspot.com


Acties:
  • 0 Henk 'm!

  • RobIII
  • Registratie: December 2001
  • Niet online

RobIII

Admin Devschuur®

^ Romeinse Ⅲ ja!

(overleden)
liquid_ice schreef op zaterdag 01 oktober 2011 @ 12:55:

Is dat dan überhaupt wel de weg om te gaan dan?
Er zit nou eenmaal niet alles in 't framework.

There are only two hard problems in distributed systems: 2. Exactly-once delivery 1. Guaranteed order of messages 2. Exactly-once delivery.

Je eigen tweaker.me redirect

Over mij


Acties:
  • 0 Henk 'm!

  • liquid_ice
  • Registratie: Februari 2001
  • Laatst online: 08-09 14:43
Ik heb de RegisterDeviceNotification toegevoegd, maar ik krijg niet het resultaat wat ik had verwacht.

Ik heb een simpele message form class:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private class MessageWindow : Form
{
    int msgCount = 0;

    protected override void WndProc(ref Message message)
    {
        if (message.Msg == Win32.WM_DEVICECHANGE)
        {
            if ((int)message.WParam == Win32.DBT_DEVNODES_CHANGED)
            {
                msgCount++;
            }

        }
        base.WndProc(ref message);
    }
}


die maak en registreer ik zo:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
_window = new MessageWindow();
_windowHandle = _window.Handle;
mre.Set();
Application.Run();
                                                        
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter = new DEV_BROADCAST_DEVICEINTERFACE();

NotificationFilter.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_name = "";
NotificationFilter.dbcc_size = Marshal.SizeOf(NotificationFilter);
IntPtr buffer = Marshal.AllocHGlobal(Marshal.SizeOf(NotificationFilter));
Marshal.StructureToPtr(NotificationFilter, buffer, true);

RegisterDeviceNotification(_windowHandle,                               // events recipient
                                       buffer,                                     // type of device
                                       Win32.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES); // type of recipient handle


Ik had nou verwacht om meer / uitgebreidere events te krijgen in m'n wndproc functie, maar ik kreeg daar gewoon wat ik eerst al kreeg.

nou vraag ik me af of ik een andere functie dan de wndproc moet overloaden, of dat ik nog iets speciaals moet doen met het aanmaken van m'n MessageWindow?

[ Voor 10% gewijzigd door liquid_ice op 05-10-2011 13:48 ]

Klus page: http://klusthuis.blogspot.com

Pagina: 1