Jump to content
Iznogoud

Finding Unique hardware ID and lock Windows OS

Recommended Posts

Hi,

Does anyone knows why this command doesn't work?

Run(@ComSpec & " /c " & "devcon.exe hwids * | findstr  /R ""1050&PID_0114&REV_0340&MI_01"" || (rundll32.exe user32.dll, LockWorkStation)", "", "")

This is the command in commandline:

devcon.exe hwids * | findstr  /R "1050&PID_0114&REV_0340&MI_01" && (echo USB Device is connected) || (rundll32.exe user32.dll, LockWorkStation)

This works, but i want to convert this to AutoIT, but it doesn't work.

 

In short what i am trying to accomplish is that a script checks every 1 sec. if there is an USB device plugged in and if not ( so it is removed ) it will lock Windows OS.

Can someone help me with this?

Share this post


Link to post
Share on other sites
1 minute ago, JLogan3o13 said:

@Iznogoud if you change the /c to /k it will keep the command line window open - what is the error telling you?

I changed the code into

Run(@ComSpec & " /k " & "devcon.exe hwids * | findstr  /R ""1050&PID_0114&REV_0340&MI_01"" || (rundll32.exe user32.dll, LockWorkStation)", "", "")

But the same happens as /c

Share this post


Link to post
Share on other sites

@Iznogoud again, "it doesn't work" doesn't help us much. My guess is a quotes issue, but you need to help us with what error you are seeing, or what the behavior is when you run the script? Help us help you ;)

 

Share this post


Link to post
Share on other sites
1 minute ago, JLogan3o13 said:

@Iznogoud again, "it doesn't work" doesn't help us much. My guess is a quotes issue, but you need to help us with what error you are seeing, or what the behavior is when you run the script? Help us help you ;)

 

It is hard to tell you what the output is, because i don't see anything. Script runs ">Exit code: 0    Time: 0.3422" and that is it.

I think it is a problem then i am using findstr inside a @ComSpec or maybe the & characters, but i don't know.

I don't see any command window either.

Share this post


Link to post
Share on other sites

Now it seems to work.... Or i am stupid or something with my variables are wrong, but entering the path to devcon.exe does helps alot.

Run(@ComSpec & " /c " & "C:\Test\devcon.exe hwids * | findstr  /R ""1050&PID_0114&REV_0340&MI_01"" || (rundll32.exe user32.dll, LockWorkStation)")

The only problem now is, it shows a command prompt window.

When i use this code:

Run(@ComSpec & " /c " & "C:\Test\devcon.exe hwids * | findstr  /R ""1050&PID_0114&REV_0340&MI_01"" || (rundll32.exe user32.dll, LockWorkStation)", @SW_HIDE)

It shows a Exit code: 0 and a time, but nothing happens.

 

Edit
Maybe it is not the best way to check if the hardware ID is available, maybe using WMI ? This because it runs every sec. to check if the USB device is connected.

Edited by Iznogoud

Share this post


Link to post
Share on other sites

Well i had a problem shord time ago. 

 

This is probably same as me. 

 

The dos windows have some encoding issue. You probably run into one of them.

 

 

What happen if you manage the scite encoding like this : 

You write your code, Then in SCITE you change that setting : 

File/Encoding/Code Page Property

 

Then you run the code again 

If you got that issue it will now work.

Edited by caramen

My video tutorials : HERE ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

What are you expecting for an output? Because from just looking at the code, all it does is pipe the HWIDs to the Findstr program to look for one in particular, then locks the computer. There's no actual display output if nothing is found.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
16 minutes ago, BrewManNH said:

What are you expecting for an output? Because from just looking at the code, all it does is pipe the HWIDs to the Findstr program to look for one in particular, then locks the computer. There's no actual display output if nothing is found.

What i want to accomplish is it to check if there is a HWID in the system, if yes, do nothing, if no then lock the computer / Windows. I needs to run in the background.

Share this post


Link to post
Share on other sites

So what about the encoding ?


My video tutorials : HERE ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
47 minutes ago, caramen said:

Well i had a problem shord time ago. 

 

This is probably same as me. 

 

The dos windows have some encoding issue. You probably run into one of them.

 

 

What happen if you manage the scite encoding like this : 

You write your code, Then in SCITE you change that setting : 

File/Encoding/Code Page Property

 

Then you run the code again 

If you got that issue it will now work.

It was already on "File/Encoding/Code Page Property"

Share this post


Link to post
Share on other sites

got it working

move devcon.exe to your script folder, compile your script as a CUI x86, run as an admin

script should contain 

Run(@comspec & " /c devcon.exe hwids * > test.txt")

Use autoit filereadline  to search for the string in test.txt

use autoit _WinAPI_LockWorkStation ( )

Share this post


Link to post
Share on other sites
1 hour ago, Nine said:

got it working

move devcon.exe to your script folder, compile your script as a CUI x86, run as an admin

script should contain 

Run(@comspec & " /c devcon.exe hwids * > test.txt")

Use autoit filereadline  to search for the string in test.txt

use autoit _WinAPI_LockWorkStation ( )

Sending it to a file every second and doing a filereadline is more resourcefull then it should be.

If i search for more info on getting hardware ID's, i think there should be a better solution, but then again, i am not that good with it yet to tell how to get it to work.

Share this post


Link to post
Share on other sites

How about piping all of the devcon output to an array and then searching inside the array? That is what I do... I can't remember where I found this code though, so I can't credit the original author.

#include <File.au3>

Global $path_Devcon = @ScriptDir & '\devcon.exe'

$find = _GetFromDevcon('"' & $path_Devcon & '" findall *') ; the findall * means we will see "non present devices"

_arraydisplay($find)

; --------------------------------------------------
;~ Function Name:   _GetFromDevcon(path and string to use for devcon)
;~ Description:     Makes an array of devices according to the devcon string
; --------------------------------------------------
Func _GetFromDevcon($strRun)
    Local $htreere = 'HTREE\'
    Local $legacyre = 'ROOT\LEGACY'
    Local $vssre = 'STORAGE\VOLUMESNAPSHOT'
    Local $swre = 'SW\'
    Local $data, $dvcn = Run("cmd.exe", @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
    StdinWrite($dvcn, $strRun & @CRLF)
    StdinWrite($dvcn)
    While True
        $data &= StdoutRead($dvcn)
        If @error Then ExitLoop
        Sleep(25)
    WEnd
    Local $aData = StringSplit($data, @CRLF, 3)

    Local $outdata[1]
    For $i = 0 To UBound($aData, 1) - 1
        $chkstring = $aData[$i]
        If Not StringInStr($chkstring, $legacyre) Then ;
            If Not StringInStr($chkstring, $htreere) Then
                If Not StringInStr($chkstring, $vssre) Then
                    If Not StringInStr($chkstring, $swre) Then
                        _ArrayAdd($outdata, $aData[$i])
                    EndIf
                EndIf
            EndIf
        EndIf
    Next
    Return $outdata
EndFunc   ;==>_GetFromDevcon

Also have a look at what Kylomas helped me with a long time back.

 

 

Share this post


Link to post
Share on other sites

Alright, that should be closer of what you want : tested with a string in my devcon

Read my previous post, for compiling and stuff....

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Constants.au3>
#include <WinAPISys.au3>

Opt ("MustDeclareVars", 1)

   Local $data
   Local $dvcn = Run(@comspec & ' /c devcon.exe hwids * | findstr "PCI\VEN_8086&DEV_0C01&SUBSYS_85341043&REV_06"', _ 
          @ScriptDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)

    While True
        $data &= StdoutRead($dvcn)
        If @error Then ExitLoop
        Sleep(25)
    WEnd


  ;  Msgbox (0,"",$data)
    

  if $data <> "" then _WinAPI_LockWorkStation()

 

Share this post


Link to post
Share on other sites
1 hour ago, ModemJunki said:

I wonder if we can do this with WMI. I don't have a Yubikey so I don't know where the key enumerates in the WMI data.

It looks like it was done before with older Yubikeys. https://forum.yubico.com/viewtopice4bc.html?f=35&t=260

 

Regardless if it is a yubikey, you can even do it with a USB mouse or USB Storage device. Everything has its unique ID's.

Share this post


Link to post
Share on other sites
2 minutes ago, Iznogoud said:

Regardless if it is a yubikey, you can even do it with a USB mouse or USB Storage device. Everything has its unique ID's.

What to select FROM in the query (for a Yubikey), is what I don't know. The only reason I think of WMI is to eliminate need for devcon executable.

Share this post


Link to post
Share on other sites
1 minute ago, ModemJunki said:

What to select FROM in the query (for a Yubikey), is what I don't know. The only reason I think of WMI is to eliminate need for devcon executable.

If you can use the hardware ID, then it doesn't matter if i am correct?

To be using devcon.exe is not a problem. I could embed that with the fileinstall function.

Share this post


Link to post
Share on other sites
2 hours ago, Nine said:

Alright, that should be closer of what you want : tested with a string in my devcon

Read my previous post, for compiling and stuff....

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Constants.au3>
#include <WinAPISys.au3>

Opt ("MustDeclareVars", 1)

   Local $data
   Local $dvcn = Run(@comspec & ' /c devcon.exe hwids * | findstr "PCI\VEN_8086&DEV_0C01&SUBSYS_85341043&REV_06"', _ 
          @ScriptDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)

    While True
        $data &= StdoutRead($dvcn)
        If @error Then ExitLoop
        Sleep(25)
    WEnd


  ;  Msgbox (0,"",$data)
    

  if $data <> "" then _WinAPI_LockWorkStation()

 

I used your code and  changed the Hardware ID like this:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Constants.au3>
#include <WinAPISys.au3>

Opt ("MustDeclareVars", 1)

   Local $data
   Local $dvcn = Run(@comspec & ' /c devcon.exe hwids * | findstr "HID\VID_1050&PID_0114&REV_0340&MI_01"', _
          @ScriptDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)

    While True
        $data &= StdoutRead($dvcn)
        If @error Then ExitLoop
        Sleep(25)
    WEnd


    Msgbox (0,"",$data)


  ;if $data <> "" then _WinAPI_LockWorkStation()

The messagebox is showing the Example.png image

So if i am correct it should NOT lock my workstation, but it does.... Why is that?

<> "" this means, if i am correct is nothing then lockworkstation?

 

Example.PNG

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • By Kelvin44
      WiFi Disconnects Automatically Windows 10 / 8 / 7 Laptop. This video guide very help to me
    • By PeterVerbeek
      Hi Guys,
      I've noticed that the SoundGetWaveVolume UDF in an older topic isn't working on Windows Vista, 7, 8, 8.1 and 10. It's probably a Windows XP UDF. The UDF below does work. It successfully returns the volume set by SoundSetWaveVolume. It might need checking by the AutoIt community for some synthax finetuning.
      ; #FUNCTION# ==================================================================================================================== ; Name...........: _SoundGetWaveVolume ; Description....: Returns app volume of script, Windows Vista, 7, 8, 10 only ; Syntax.........: _SoundGetWaveVolume([$iValueOnError = -1]) ; Parameters.....: $iValueOnError - Value to return when an error occurs ; Return values..: App volume of script or $iValueOnError at an error ; Error values...: @error = 1 - Unable to create Struct ; @error = 2 - Dll file not found ; @error = 3 - Wrong call so not on Windows Vista, 7, 8 or 10 ; @error = 4 - Internal error, array not returned ; @error = 5 - Volume wasn't received ; @error = 6 - Volume couldn't read ; Author.........: Peter Verbeek ; Modified.......: ; =============================================================================================================================== Func _SoundGetWaveVolume($iValueOnError = -1) Local $LPDWORD,$aMMRESULT,$iVolume $LPDWORD = DllStructCreate("dword") If @error <> 0 Then SetError(1) ; 1 = unable to create Struct Return $iValueOnError EndIf ; get app volume of this script $aMMRESULT = DllCall("winmm.dll","uint","waveOutGetVolume","ptr",0,"long_ptr",DllStructGetPtr($LPDWORD)) Switch @error Case 1 SetError(2) ; 2 = dll file not found Return $iValueOnError Case 2,3,4,5 SetError(3) ; 3 = wrong call so not on Windows Vista, 7, 8 or 10 Return $iValueOnError EndSwitch If not IsArray($aMMRESULT) Then SetError(4) ; 4 = internal error, array not returned Return $iValueOnError EndIf If $aMMRESULT[0] <> 0 Then SetError(5) ; 5 = volume wasn't received Return $iValueOnError EndIf $iVolume = DllStructGetData($LPDWORD,1) If @error <> 0 Then SetError(6) ; 6 = volume couldn't read Return $iValueOnError EndIf Return Round(100*$iVolume/4294967295) ; return in range 0 to 100 as SoundSetWaveVolume() EndFunc  
    • By Drac89
      How do i pass the IE handle to an active windows authentication prompt when the IE shows it is " waiting " , right now it is nt detecting the windows security prompt
      I used the below to wait for it to load until the 60ms timeout but still it isnt working  :
       
      #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
      #AutoIt3Wrapper_Outfile=ps_test.exe
      #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
      #include <Constants.au3>
      #include <IE_v1.au3>
      If $CmdLine[0] < 2 Then
         MsgBox($MB_OK, "Usage", "ps_WAN <username> <password>")
      Else
         wan($CmdLine[1], $CmdLine[2])
      EndIf
      Func wan($email, $password)
       $oIE = _IECreate()
      $ieo=_IENavigate($oIE, "https://cccc")
      _IELoadWait($ieo,0,60)
       Local $hWnd = WinExists("Windows Security")
      if $hWnd = 0 Then
         MsgBox($MB_OK, "Usage", "timeout")
         EndIf
      ControlFocus("Windows Security", "", "[CLASS:DirectUIHWND]")
      ControlFocus("Windows Security", "", "[CLASS:Edit; INSTANCE:1]")
       

    • By BigMac121263
      I know that this theme was discussed several times but I read all These threats with no helping result.
      I have two pieces of TEMPer1F USB Thermometer.
      It is a Standard Windows HID device with no add. Driver needed.
      With the device I got an App reading from it and showing Graphs and much more and which can handle several devices.
      With the Software came also a RDingUSB.dll file handling the communication with the device.
      I can call the dll without any error. But the Problem is how to Access the device using the DLL and Access more than one of same VID/PID
      by Serial or anything else useful.
      I have also sources in c# doing the Job, but no experience in C# to successfuly port it to AutoIT.
       
      So my question: Can anyone help me to realize the simple pull of the temperature value from the devices based on C# source?
      The rest to write it to MySQL DB and repeat it etc. is no Problem for me.
      ***************************************
      The C# part with the DLL functions class:
      ***************************************
      using System;
      using System.Runtime.InteropServices;
      namespace TEMPer
      {
        internal class RDing
        {
          [DllImport("RDingUSB.dll")]
          public static extern IntPtr OpenUSBDevice(int VID, int PID);
          [DllImport("RDingUSB.dll")]
          public static extern IntPtr CloseUSBDevice(IntPtr hDevice);
          [DllImport("RDingUSB.dll")]
          public static extern bool WriteUSB(IntPtr hDevice, byte[] pBuffer, uint dwBytesToWrite, ref ulong lpNumberOfBytesWritten);
          [DllImport("RDingUSB.dll")]
          public static extern bool ReadUSB(IntPtr hDevice, byte[] pBuffer, uint dwBytesToRead, ref ulong lpNumberOfBytesRead);
          [DllImport("RDingUSB.dll")]
          public static extern ushort GetInputLength(IntPtr hDevice);
          [DllImport("RDingUSB.dll")]
          public static extern ushort GetOutputLength(IntPtr hDevice);
          [DllImport("RDingUSB.dll")]
          public static extern uint GetErrorMsg(ref string[] lpErrorMsg, uint dwErrorMsgSize);
        }
      }
      *********************************
      The C# code snippet working with the functions of the class above is in the attachment
      this is the interesting part where the data is sent and pulled which I
      do not understand to transcode to AutoIT
      Important my devices are the type2 (TEMPer1F) :
      *********************************
      So in the end I Need: Call DLL, read all devices in array
      Loop the Array until ubound(Array) and for each device in Array read the temperature value and prepare the data to
      decimal 6,1 (SQL Format) and °C
      So how to pull the value from the devices?
      Maybe somwbody finds the important part in the C#  (the code above is for different models, mine is TEMPer1F
      Or maybe someone has solved this and has maybe a uDF for this
       
       
      MainForm.cs
    • By VITSUSA
      I am using window XP in my desktop, so I want to convert basic disk to a dynamic disk, so how it is possible?
×
×
  • Create New...