Link to home
Create AccountLog in
Microsoft Development

Microsoft Development

--

Questions

--

Followers

Top Experts

Avatar of porear
porear🇺🇸

Detect laptop docking state, GetCurrentHwProfile and Autohotkey currently attempted
I am seeking a way to detect my laptop's docking state so that I may programmatically reconfigure video settings.  

I have written a script in Autohotkey that does the reconfig, I now just need the trigger.  I would prefer a script within Autohotkey, but anything external that would interface will suffice.  Please help!

http://www.autohotkey.com/

I have tried calling the GetCurrentHwProfile function included in the advapi32 library using the DllCall function in Autohotkey with both str * and UInt * return types.  Script is copied below, no luck.  I suspect either I am not calling properly or am not deconstructing the HW_PROFILE_INFO response properly.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getcurrenthwprofile.asp

I am using the ExtractInteger function code supplied in the Autohotkey help for DllCall to dissect the doxking state from the returned structure.

********************************************************************

;This function copied directly for Autohotkey help for DllCall
;Given as a method for extracting an integer from the return of a DLL call
;that has mixed types, accepted as a string

ExtractInteger(ByRef pSource, pOffset = 0, pIsSigned = false, pSize = 4)
; pSource is a string (buffer) whose memory area contains a raw/binary integer at pOffset.
; The caller should pass true for pSigned to interpret the result as signed vs. unsigned.
; pSize is the size of PSource's integer in bytes (e.g. 4 bytes for a DWORD or Int).
; pSource must be ByRef to avoid corruption during the formal-to-actual copying process
; (since pSource might contain valid data beyond its first binary zero).
{
      SourceAddress := &pSource + pOffset  ; Get address and apply the caller's offset.
      result := 0  ; Init prior to accumulation in the loop.
      Loop %pSize%  ; For each byte in the integer:
      {
            result := result | (*SourceAddress << 8 * (A_Index - 1))  ; Build the integer from its bytes.
            SourceAddress += 1  ; Move on to the next byte.
      }
      if (!pIsSigned OR pSize > 4 OR result < 0x80000000)
            return result  ; Signed vs. unsigned doesn't matter in these cases.
      ; Otherwise, convert the value (now known to be 32-bit) to its signed counterpart:
      return -(0xFFFFFFFF - result + 1)
}

;Allocate space for structure
VarSetCapacity(MyStruct, 200, 0)

;Make call to retrieve HW_PROFILE_INFO structure
DllCall("advapi32\GetCurrentHwProfile", "UInt *", MyStruct)

;Extract dwDockInfo from returned structure's first 4 bytes (double word type)
dwDockInfo := ExtractInteger(MyStruct, 0, 4)

;Looking for hex 2 (docked) or hex 4 (undocked)
MsgBox, Docking state is %dwDockInfo%

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of grayegraye🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of porearporear🇺🇸

ASKER

Hello, graye, thanks for the tip.  I'll give it a shot.

I am a novice programmer, so I may still have issues implementing this, but if so I'll come back with specific questions.  I'll still be trying to call from and format the return in AutoHotkey.

Avatar of grayegraye🇺🇸

What kind of video settings are you trying to change... perhaps we could be of help there as well.

Avatar of porearporear🇺🇸

ASKER

I'd like to be able to dock and undock and manually have the number of screens on the deskopt updated.

I've got an HP nc6230 with a docking station and second monitor.  When docked, I use the laptop screen as the primary screen, and the external as a secondary monitor.  

However, I have to manually tell the desktop it has two screens in Windows XP (Display Properties - Settings).  When undocking, I have to manually set it back to one, else windows on the "phantom" screen are invisible.  

I am using an Autohotkey script to do this setting toggle to save all the clicking through the settings.  I have tried using SysGet calls in Autohotkey to detect how many screens are attached, but once the laptop has been docked, it reports how many monitors are currently set instead of how many are actually there.

Hope this helps, I think your previous suggestion is within my abilities, I should get a chance to try it out tonight.  Thanks!!

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of porearporear🇺🇸

ASKER

Progress.  Although I haven't a clue how to utilize this in Autohotkey, I found a VB sample script at

http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0921.mspx

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colChassis = objWMIService.ExecQuery _
    ("Select * from Win32_SystemEnclosure")
For Each objChassis In colChassis
    For Each strChassisType In objChassis.ChassisTypes
        MsgBox (strChassisType)

that I was able to get running.  It detects my laptop as a type 10.  

I'll have to wait until I get a chance to try this at work, where my docking station resides, to see if the return code changes to 12 (docking station).  The notes I have found are unclear as to whether a chassis can be multiple types (e.g. a notebook and a docking station) since the type used is an array. ??

I'll keep you posted...

Avatar of grayegraye🇺🇸

The ChassisType is an array.... so it will probably switch between 10 when not docked to 10, 12 when docked.   In other words, it will add an additional item (12) onto the existing (10).

The script center also has some good examples (practically all in VBScript, sorry) that will allow you to setup an event...   The event will just sit and wait until there is a change in the Win32_SystemEnclosure class instance.   Inside the event response code, you figure out what the change was, and if it's an addition/deletion of the docking code in ChassisType, then you launch your part of the application.

I've got a few good examples in VB.Net (if that will help any)

Avatar of porearporear🇺🇸

ASKER

Sure, please send any links you might have to the event handling scripts, I need to get more into VB, if I need to resolve this in VB thats no problem.

Tried the above (just realized the script I pasted here is missing the last two Next statements) with the laptop docked, but the return was the same - I only got the 10 code, no 12.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of grayegraye🇺🇸

Well, that's not good....   That means this entire approach that I've suggested won't work for you.

Still want a VB.Net WMI Event example?

Avatar of porearporear🇺🇸

ASKER

Sorry for the lag - work and kids.

Yes, please do send the links to the examples.  I've been surveying the MSDN WMI script sites today looking for another way to resolve my problem, but no inpsiration yet.

Thought maybe detecting switch from battery to AC might have merit, but I still plug in a charger at times without docking.  No other permanent devices attached to the docking station I could look for, either.

SOLUTION
Avatar of grayegraye🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Avatar of porearporear🇺🇸

ASKER

Have had to abandon this effort due to lack of time, but didn't want to leave you hanging.  I really appreciate all your time and suggestions.  If I find a workable solution, I'll let ya know! Thanks again.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

Microsoft Development

Microsoft Development

--

Questions

--

Followers

Top Experts

Most development for the Microsoft platform is done utilizing the technologies supported by the.NET framework. Other development is done using Visual Basic for Applications (VBA) for programs like Access, Excel, Word and Outlook, with PowerShell for scripting, or with SQL for large databases.