c# - Microchip HID_PnP_Demo, WinForm to WPF -
i've been trying learn wpf since i've been using vb.net , windowsform past 10 years in few small projects.
i'm planning simple interface control house pic18f97j94.
microchip provides example (that's part of mla solution, it's in c# , winform) workaround , finish project want learn wpf , c#.
the problem i'm encountering .handle:
registerdevicenotification(this.handle, pdevicebroadcastheader, device_notify_window_handle);
error cs1061 'mainwindow' not contain definition 'handle' , no extension method 'handle' accepting first argument of type 'mainwindow' found.
the full code block next one:
public mainwindow() { initializecomponent(); progressbar1.tooltip = ("if using board/pim without potentiometer, apply adjustable voltage i/o pin."); anxvoltage_lbl.tooltip = ("if using board/pim without potentiometer, apply adjustable voltage i/o pin."); toggleleds_btn.tooltip = ("sends packet of data usb device."); pushbuttonstate_lbl.tooltip = ("try pressing pushbuttons on usb demo board/pim."); //register wm_devicechange notifications. code uses these messages detect plug , play connection/disconnection events usb devices dev_broadcast_deviceinterface devicebroadcastheader = new dev_broadcast_deviceinterface(); devicebroadcastheader.dbcc_devicetype = dbt_devtyp_deviceinterface; devicebroadcastheader.dbcc_size = (uint)marshal.sizeof(devicebroadcastheader); devicebroadcastheader.dbcc_reserved = 0; //reserved says not use... devicebroadcastheader.dbcc_classguid = interfaceclassguid; //need address of devicebroadcastheader call registerdevicenotification(), //can't use "&devicebroadcastheader". instead, using roundabout means address //making duplicate copy using marshal.structuretoptr(). intptr pdevicebroadcastheader = intptr.zero; //make pointer. pdevicebroadcastheader = marshal.allochglobal(marshal.sizeof(devicebroadcastheader)); //allocate memory new dev_broadcast_deviceinterface structure, , return address marshal.structuretoptr(devicebroadcastheader, pdevicebroadcastheader, false); //copies devicebroadcastheader structure memory allocated @ devicebroadcastheaderwithpointer registerdevicenotification(this.handle, pdevicebroadcastheader, device_notify_window_handle); //now make initial attempt find usb device, if connected pc , enumerated prior launching application. //if connected , present, should open read , write handles device can communicate later. //if not connected, have wait until user plugs device in, , wm_devicechange callback function can process //the message , again search device. if (checkifpresentandgetusbdevicepath()) //check , make sure @ least 1 device matching vid/pid attached { uint errorstatuswrite; uint errorstatusread; //we have proper device path, , can open read , write handles device. writehandletousbdevice = createfile(devicepath, generic_write, file_share_read | file_share_write, intptr.zero, open_existing, 0, intptr.zero); errorstatuswrite = (uint)marshal.getlastwin32error(); readhandletousbdevice = createfile(devicepath, generic_read, file_share_read | file_share_write, intptr.zero, open_existing, 0, intptr.zero); errorstatusread = (uint)marshal.getlastwin32error(); if ((errorstatuswrite == error_success) && (errorstatusread == error_success)) { attachedstate = true; //let rest of pc application know usb device connected, , safe read/write attachedbutbroken = false; statusbox_txtbx.text = "device found, attachedstate = true"; } else //for reason device physically plugged in, 1 or both of read/write handles didn't open successfully... { attachedstate = false; //let rest of application known not read/write device. attachedbutbroken = true; //flag next time wm_devicechange message occurs, can retry re-open read/write pipes if (errorstatuswrite == error_success) writehandletousbdevice.close(); if (errorstatusread == error_success) readhandletousbdevice.close(); } } else //device must not connected (or not programmed correct firmware) { attachedstate = false; attachedbutbroken = false; } if (attachedstate == true) { statusbox_txtbx.text = "device found, attachedstate = true"; } else { statusbox_txtbx.text = "device not found, verify connect/correct firmware"; } readwritethread.runworkerasync(); }
added info: next code block transferred winform example, warning cs0649 in line:
internal char[] devicepath; //tchar array of size
and
internal char[] dbcc_name; //tchar array
code:
public partial class mainwindow : window { //------------------------------------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------begin cut , paste block----------------------------------------------------------------------------------- //constant definitions setupapi.h, aren't allowed include directly since c# internal const uint digcf_present = 0x02; internal const uint digcf_deviceinterface = 0x10; //constants createfile() , other file i/o functions internal const short file_attribute_normal = 0x80; internal const short invalid_handle_value = -1; internal const uint generic_read = 0x80000000; internal const uint generic_write = 0x40000000; internal const uint create_new = 1; internal const uint create_always = 2; internal const uint open_existing = 3; internal const uint file_share_read = 0x00000001; internal const uint file_share_write = 0x00000002; //constant definitions wm_devicechange messages internal const uint wm_devicechange = 0x0219; internal const uint dbt_devicearrival = 0x8000; internal const uint dbt_deviceremovepending = 0x8003; internal const uint dbt_deviceremovecomplete = 0x8004; internal const uint dbt_configchanged = 0x0018; //other constant definitions internal const uint dbt_devtyp_deviceinterface = 0x05; internal const uint device_notify_window_handle = 0x00; internal const uint error_success = 0x00; internal const uint error_no_more_items = 0x00000103; internal const uint spdrp_hardwareid = 0x00000001; enum wm_devicechange_enum : int//public enum wm_devicechange integer { dbt_configchangecanceled = 0x19, dbt_configchanged = 0x18, dbt_customevent = 0x8006, dbt_devicearrival = 0x8000, dbt_devicequeryremove = 0x8001, dbt_devicequeryremovefailed = 0x8002, dbt_deviceremovecomplete = 0x8004, dbt_deviceremovepending = 0x8003, dbt_devicetypespecific = 0x8005, dbt_devnodes_changed = 0x7, dbt_querychangeconfig = 0x17, dbt_userdefined = 0xffff }//end enum //various structure definitions structures code using internal struct sp_device_interface_data { internal uint cbsize; //dword internal guid interfaceclassguid; //guid internal uint flags; //dword internal uint reserved; //ulong_ptr msdn says ulong_ptr "typedef unsigned __int3264 ulong_ptr;" } internal struct sp_device_interface_detail_data { internal uint cbsize; //dword internal char[] devicepath; //tchar array of size } internal struct sp_devinfo_data { internal uint cbsize; //dword internal guid classguid; //guid internal uint devinst; //dword internal uint reserved; //ulong_ptr msdn says ulong_ptr "typedef unsigned __int3264 ulong_ptr;" } internal struct dev_broadcast_deviceinterface { internal uint dbcc_size; //dword internal uint dbcc_devicetype; //dword internal uint dbcc_reserved; //dword internal guid dbcc_classguid; //guid internal char[] dbcc_name; //tchar array } //dll imports. need these access various c style unmanaged functions contained in respective dll files. //-------------------------------------------------------------------------------------------------------------- //returns hdevinfo type device information set. need //hdevinfo in input parameter calling many of other setupdixxx() functions. [dllimport("setupapi.dll", setlasterror = true, charset = charset.unicode)] internal static extern intptr setupdigetclassdevs( ref guid classguid, //lpguid input: need supply class guid. intptr enumerator, //pctstr input: use null here, not important our purposes intptr hwndparent, //hwnd input: use null here, not important our purposes uint flags); //dword input: flags describing kind of filtering use. //gives "psp_device_interface_data" contains interface specific guid (different //from class guid). need interface guid device path. [dllimport("setupapi.dll", setlasterror = true, charset = charset.unicode)] internal static extern bool setupdienumdeviceinterfaces( intptr deviceinfoset, //input: give hdevinfo got setupdigetclassdevs() intptr deviceinfodata, //input (optional) ref guid interfaceclassguid, //input uint memberindex, //input: "index" of device interested in getting path for. ref sp_device_interface_data deviceinterfacedata); //output: function fills in "sp_device_interface_data" structure. //setupdidestroydeviceinfolist() frees memory destroying deviceinfolist [dllimport("setupapi.dll", setlasterror = true, charset = charset.unicode)] internal static extern bool setupdidestroydeviceinfolist( intptr deviceinfoset); //input: give handle device info list deallocate ram. //setupdienumdeviceinfo() fills in "sp_devinfo_data" structure, need setupdigetdeviceregistryproperty() [dllimport("setupapi.dll", setlasterror = true, charset = charset.unicode)] internal static extern bool setupdienumdeviceinfo( intptr deviceinfoset, uint memberindex, ref sp_devinfo_data deviceinterfacedata); //setupdigetdeviceregistryproperty() gives hardware id, use check see if has matching vid/pid [dllimport("setupapi.dll", setlasterror = true, charset = charset.unicode)] internal static extern bool setupdigetdeviceregistryproperty( intptr deviceinfoset, ref sp_devinfo_data deviceinfodata, uint property, ref uint propertyregdatatype, intptr propertybuffer, uint propertybuffersize, ref uint requiredsize); //setupdigetdeviceinterfacedetail() gives device path, needed before createfile() can used. [dllimport("setupapi.dll", setlasterror = true, charset = charset.unicode)] internal static extern bool setupdigetdeviceinterfacedetail( intptr deviceinfoset, //input: wants hdevinfo can obtained setupdigetclassdevs() ref sp_device_interface_data deviceinterfacedata, //input: pointer structure defines device interface. intptr deviceinterfacedetaildata, //output: pointer sp_device_interface_detail_data structure, receive device path. uint deviceinterfacedetaildatasize, //input: number of bytes retrieve. ref uint requiredsize, //output (optional): number of bytes needed hold entire struct intptr deviceinfodata); //output (optional): pointer sp_devinfo_data structure //overload setupdigetdeviceinterfacedetail(). need 1 since can't pass null pointers directly in c#. [dllimport("setupapi.dll", setlasterror = true, charset = charset.unicode)] internal static extern bool setupdigetdeviceinterfacedetail( intptr deviceinfoset, //input: wants hdevinfo can obtained setupdigetclassdevs() ref sp_device_interface_data deviceinterfacedata, //input: pointer structure defines device interface. intptr deviceinterfacedetaildata, //output: pointer sp_device_interface_detail_data structure, contain device path. uint deviceinterfacedetaildatasize, //input: number of bytes retrieve. intptr requiredsize, //output (optional): pointer dword tell number of bytes needed hold entire struct intptr deviceinfodata); //output (optional): pointer sp_devinfo_data structure //need function receiving of wm_devicechange messages. see msdn documentation //description of function does/how use it. note: name remapped "registerdevicenotificationum" //avoid possible build error conflicts. [dllimport("user32.dll", setlasterror = true, charset = charset.unicode)] internal static extern intptr registerdevicenotification( intptr hrecipient, intptr notificationfilter, uint flags); //takes in device path , opens handle device. [dllimport("kernel32.dll", setlasterror = true, charset = charset.unicode)] static extern safefilehandle createfile( string lpfilename, uint dwdesiredaccess, uint dwsharemode, intptr lpsecurityattributes, uint dwcreationdisposition, uint dwflagsandattributes, intptr htemplatefile); //uses handle (created createfile()), , lets write usb data device. [dllimport("kernel32.dll", setlasterror = true, charset = charset.unicode)] static extern bool writefile( safefilehandle hfile, byte[] lpbuffer, uint nnumberofbytestowrite, ref uint lpnumberofbyteswritten, intptr lpoverlapped); //uses handle (created createfile()), , lets read usb data device. [dllimport("kernel32.dll", setlasterror = true, charset = charset.unicode)] static extern bool readfile( safefilehandle hfile, intptr lpbuffer, uint nnumberofbytestoread, ref uint lpnumberofbytesread, intptr lpoverlapped); //--------------- global varibles section ------------------ //usb related variables need have wide scope. bool attachedstate = false; //need keep track of usb device attachment status proper plug , play operation. bool attachedbutbroken = false; safefilehandle writehandletousbdevice = null; safefilehandle readhandletousbdevice = null; string devicepath = null; //need find proper device path before can open file handles. //variables used application/form updates. bool pushbuttonpressed = false; //updated readwritethread, read formupdatetimer tick handler (needs atomic) bool toggleledspending = false; //updated toggleled(s) button click event handler, used readwritethread (needs atomic) uint adcvalue = 0; //updated readwritethread, read formupdatetimer tick handler (needs atomic) uint adcdiff = 0; //globally unique identifier (guid) hid class devices. windows uses guids identify things. guid interfaceclassguid = new guid(0x4d1e55b2, 0xf16f, 0x11cf, 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30); //--------------- end of global varibles ------------------ //-------------------------------------------------------end cut , paste block------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------------------------------------------------------------------------------- windowinterophelper interophelper; private backgroundworker readwritethread = new backgroundworker(); private dispatchertimer timer; private int32 bytescontrol = 0;//convert.toint32("000000000000000000000001", 2);
replace this.handle following:
new windowinterophelper(mywindow).handle
windowinterophelper class in system.windows.interop namespace. , mywindow name of window.
Comments
Post a Comment