@@ -113,40 +113,52 @@ private static int OnEventReceived(
113
113
{
114
114
var thisPtr = GCHandle . FromIntPtr ( context ) ;
115
115
var thisObj = thisPtr . Target as WindowsHidDeviceListener ;
116
- thisObj ? . _log . LogInformation ( "ConfigMgr callback received." ) ;
117
116
118
- CM_NOTIFY_EVENT_DATA eventData = Marshal . PtrToStructure < CM_NOTIFY_EVENT_DATA > ( eventDataPtr ) ;
119
- Debug . Assert ( eventData . ClassGuid == CmInterfaceGuid . Hid ) ;
120
- Debug . Assert ( eventData . FilterType == CM_NOTIFY_FILTER_TYPE . DEVINTERFACE ) ;
117
+ try
118
+ {
121
119
122
- int stringOffset = 24 ; // Magic number from C land
123
- int stringSize = eventDataSize - stringOffset ;
124
- byte [ ] buffer = new byte [ eventDataSize ] ;
120
+ CM_NOTIFY_EVENT_DATA eventData = Marshal . PtrToStructure < CM_NOTIFY_EVENT_DATA > ( eventDataPtr ) ;
121
+ Debug . Assert ( eventData . ClassGuid == CmInterfaceGuid . Hid ) ;
122
+ Debug . Assert ( eventData . FilterType == CM_NOTIFY_FILTER_TYPE . DEVINTERFACE ) ;
125
123
126
- Marshal . Copy ( eventDataPtr + stringOffset , buffer , 0 , stringSize ) ;
124
+ int stringOffset = 24 ; // Magic number from C land
125
+ int stringSize = eventDataSize - stringOffset ;
126
+ byte [ ] buffer = new byte [ eventDataSize ] ;
127
127
128
- switch ( action )
129
- {
130
- case CM_NOTIFY_ACTION . DEVICEINTERFACEARRIVAL :
131
- {
132
- string instancePath = System . Text . Encoding . Unicode . GetString ( buffer ) ;
133
- var cmDevice = CmDevice . FromDevicePath ( instancePath ) ;
134
- if ( cmDevice == null )
128
+ Marshal . Copy ( eventDataPtr + stringOffset , buffer , 0 , stringSize ) ;
129
+
130
+ switch ( action )
131
+ {
132
+ case CM_NOTIFY_ACTION . DEVICEINTERFACEARRIVAL :
135
133
{
136
- thisObj ? . _log . LogWarning ( "Failed to create CmDevice from instance path {InstancePath}." , instancePath ) ;
137
- return 0 ; // We can't do anything without a CmDevice. Exit gracefully.
134
+ string instancePath = System . Text . Encoding . Unicode . GetString ( buffer ) ;
135
+ var cmDevice = CmDevice . FromDevicePath ( instancePath ) ;
136
+ if ( cmDevice == null )
137
+ {
138
+ thisObj ? . _log . LogWarning (
139
+ "Failed to create CmDevice from instance path {InstancePath}." , instancePath ) ;
140
+
141
+ return 0 ; // We can't do anything without a CmDevice. Exit gracefully.
142
+ }
143
+
144
+ var device = new WindowsHidDevice ( cmDevice ) ;
145
+ thisObj ? . OnArrived ( device ) ;
146
+ break ;
138
147
}
139
-
140
- var device = new WindowsHidDevice ( cmDevice ) ;
141
- thisObj ? . OnArrived ( device ) ;
148
+ case CM_NOTIFY_ACTION . DEVICEINTERFACEREMOVAL :
149
+ thisObj ? . OnRemoved ( NullDevice . Instance ) ;
142
150
break ;
143
- }
144
- case CM_NOTIFY_ACTION . DEVICEINTERFACEREMOVAL :
145
- thisObj ? . OnRemoved ( NullDevice . Instance ) ;
146
- break ;
147
- }
151
+ }
148
152
149
- return 0 ;
153
+ return 0 ;
154
+ }
155
+ catch ( Exception ex )
156
+ {
157
+ // We must not let exceptions escape from this callback. There's nowhere for them to go, and
158
+ // it will likely crash the process.
159
+ thisObj ? . _log . LogDebug ( $ "Exception in OnEventReceived: { ex } ") ;
160
+ return 0 ;
161
+ }
150
162
}
151
163
}
152
164
}
0 commit comments