Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

COM Object Reference [AutoHotkey v1.1+]


  • Please log in to reply
233 replies to this topic
jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

COM Object: WMPlayer.OCX

Purpose: Play Media Files; Embed WMP control in GUI

System Requirements: Windows Media Player 9 Series or later

Documentation Link: Player Object

Code Example:

FileSelectFile, file, 2, %A_MyDocuments%, Select WMP Compatable File:
if Errorlevel
   ExitApp

wmp := ComObjCreate("WMPlayer.OCX")
ComObjConnect(wmp, "wmp_") ;// show player status in TrayTip
wmp.url := file ;// play file
 
Message("Pause")
wmp.controls.pause

Message("Unpause")
wmp.controls.play

Message("Speed Up")
wmp.settings.rate := 1.5

Message("Return to Normal Speed")
wmp.settings.rate := 1 

Message("Load File in WMP App & Close Script")
wmp.close
wmp.openPlayer(file)

wmp := ""  ;// release/disconnect object

wmp_StatusChange(wmp) {
   TrayTip, , % "Status: " wmp.status
             .  "`nDuration: " wmp.currentMedia.durationString
} 
Message(str) {
   ToolTip, Press CTRL to %str%
   KeyWait, CTRL, D
   KeyWait, CTRL 
   ToolTip
}

Embed player in a GUI:

FileSelectFile, file, 2, %A_MyDocuments%, Select WMP Compatable File:
if Errorlevel
   ExitApp

Gui, +LastFound +Resize
Gui, Add, ActiveX, x0 y0 w500 h300 vWMP, WMPLayer.OCX
WMP.Url := file
Gui, Show, w500 h300 Center, Player GUI
SetTimer, Position, 1000
return

GuiClose:
   ExitApp

GuiSize:
   GuiControl, Move, WMP, w%A_GuiWidth% h%A_GuiHeight%
   return
  
Position:
   Gui, Show, NA, % WMP.CurrentMedia.name " - " Format(WMP.Controls.currentPosition) " / " Format(WMP.CurrentMedia.duration)
   return

Format(m1) {
   static units := "sec,min,hr"
   Loop Parse, units, csv
      RegExMatch(m1/60, "(.*)(\.\d*)", m), %A_LoopField% := Pad(Round(m2*60))
   return (hr? hr ":":"") min ":" sec
}
Pad(p) {
   return (p<10? "0":"") p
}


Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
Very nice :p

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
[*:2uwlkwop]COM Object: Ahk.ComSurrogate.32/64
[*:2uwlkwop]Purpose: Create COM objects of a specified bitness
[*:2uwlkwop]System Requirements: ComDispatch.ahk (with the included ComRemote.ahk library)
[*:2uwlkwop]Description:This is a simple COM script that proxies the creation of 32-bit only objects on 64-bit scripts and viceversa.
;
; File encoding:  UTF-8
; Author: fincs
;
; COM 32bit<->64bit object creation surrogate
;

[color=#107095]#NoEnv[/color]
[color=#107095]SendMode[/color] [color=#107095]Input[/color]
[color=#107095]SetWorkingDir[/color], %[color=brown]A_ScriptDir[/color]%

[color=#107095]#Persistent[/color]
;#NoTrayIcon
[color=#107095]#SingleInstance[/color] Off
[color=#107095]#Include[/color] ComRemote.ahk

; CLSID and APPID for this script: don't reuse, please!
[color=#107095]if[/color] [color=brown]A_PtrSize[/color] = 4
{
	CLSID_ThisScript := [color=#666666]"{EB42F5B6-31A0-4B9D-8C6D-757FE4136B2C}"[/color]
	APPID_ThisScript := [color=#666666]"Ahk.ComSurrogate.32"[/color]
}[color=#107095]else[/color] [color=#107095]if[/color] [color=brown]A_PtrSize[/color] = 8
{
	CLSID_ThisScript := [color=#666666]"{CC2D93BB-3818-40E6-9405-804512FF23DC}"[/color]
	APPID_ThisScript := [color=#666666]"Ahk.ComSurrogate.64"[/color]
}[color=#107095]else[/color]
{
	[color=#107095]MsgBox[/color], 16,, What kind of weird AHK build are you running?!
	[color=#107095]ExitApp[/color]
}

[color=#107095]ComObjError[/color](false)
test := [color=#107095]ComObjActive[/color](APPID_ThisScript)
[color=#107095]if[/color] [color=#107095]IsObject[/color](test)
	[color=#107095]ExitApp[/color] ; already running
[color=#107095]ComObjError[/color](true)

; Register our CLSID and APPID
[color=#107095]OnExit[/color], IDCleanup
RegisterIDs(CLSID_ThisScript, APPID_ThisScript)

; Create a dispatch object for exposing
myObj := ComDispatch([color=#666666]""[/color], [color=#666666]"ObjCreate=Wrap_ObjCreate, Close=Wrap_Close"[/color])
; Expose it
[color=#107095]if[/color] !(hRemote := ComRemote(myObj, CLSID_ThisScript))
{
	[color=#107095]MsgBox[/color], 16, %[color=brown]A_ScriptName[/color]%, Can't remote the [color=#107095]object[/color]!
	[color=#107095]ExitApp[/color]
}

; End of auto-run section
[color=#107095]return[/color]

IDCleanup:
; Remove the CLSID and APPID from the registry
RevokeIDs(CLSID_ThisScript, APPID_ThisScript)
[color=#107095]ExitApp[/color]

RegisterIDs(CLSID, APPID)
{
	[color=#107095]RegWrite[/color], REG_SZ, HKCU, Software\Classes\%APPID%,, %APPID%
	[color=#107095]RegWrite[/color], REG_SZ, HKCU, Software\Classes\%APPID%\CLSID,, %CLSID%
	[color=#107095]RegWrite[/color], REG_SZ, HKCU, Software\Classes\CLSID\%CLSID%,, %APPID%
}

RevokeIDs(CLSID, APPID)
{
	[color=#107095]RegDelete[/color], HKCU, Software\Classes\%APPID%
	[color=#107095]RegDelete[/color], HKCU, Software\Classes\CLSID\%CLSID%
}

Str2GUID(ByRef var, str)
{
	[color=#107095]VarSetCapacity[/color](var, 16)
	[color=#107095]DllCall[/color]([color=#666666]"ole32\CLSIDFromString"[/color], [color=#666666]"wstr"[/color], str, [color=#666666]"ptr"[/color], &var)
	[color=#107095]return[/color] &var
}

; --------------------------------------------

Wrap_ObjCreate(this, ProgIDorCLSID)
{
	[color=#107095]return[/color] [color=#107095]ComObjCreate[/color](ProgIDorCLSID)
}

Wrap_Close(this)
{
	[color=#107095]Critical[/color]
	[color=#107095]SetTimer[/color], _Close, -0
}

_Close:
[color=#107095]ExitApp[/color]
You need to have it running in the background using the AHK_L build of the appropiate bitness (running both AHK_L builds at the same time is supported), then you can use these functions to create objects:
ComCreateObj32(ProgIDorCLSID)
{
	[color=#107095]static[/color] _sur := [color=brown]A_PtrSize[/color] = 8 ? [color=#107095]ComObjActive[/color]([color=#666666]"Ahk.ComSurrogate.32"[/color]) : [color=#666666]""[/color]
	[color=#107095]return[/color] [color=brown]A_PtrSize[/color] = 8 ? _sur.ObjCreate(ProgIDorCLSID) : [color=#107095]ComObjCreate[/color](ProgIDorCLSID)
}

ComCreateObj64(ProgIDorCLSID)
{
	[color=#107095]static[/color] _sur := [color=brown]A_PtrSize[/color] = 4 ? [color=#107095]ComObjActive[/color]([color=#666666]"Ahk.ComSurrogate.64"[/color]) : [color=#666666]""[/color]
	[color=#107095]return[/color] [color=brown]A_PtrSize[/color] = 4 ? _sur.ObjCreate(ProgIDorCLSID) : [color=#107095]ComObjCreate[/color](ProgIDorCLSID)
}

Example: ScriptControl on 64-bit AHK_L:
oSC := ComCreateObj32([color=#666666]"ScriptControl"[/color]), oSC.Language := [color=#666666]"VBScript"[/color]
oSC.ExecuteStatement([color=#666666]"MsgBox "[/color][color=#666666]"Hello World!"[/color][color=#666666]""[/color])


tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: Apr 19 2019 05:49 PM
  • Joined: 21 Dec 2007
fincs I beleive once the others wrap their heads around this that this will be a real break thru

Thanks

I am buying a new laptop today which will have a 64bit OS and i7 processor
Never lose.
WIN or LEARN.

amnesiac
  • Members
  • 124 posts
  • Last active: May 01 2014 03:04 AM
  • Joined: 07 Nov 2010
[*:zf913s7a]COM Object:WIA.CommonDialog/WIA.DeviceManager/WIA.ImageFile/WIA.ImageProcess/WIA.Rational/WIA.Vector [*:zf913s7a]Purpose: acquire images on digital cameras, scanners, or Web cameras, and to rotate, scale, and annotate your image files. [*:zf913s7a]System Requirements:require Windows XP Service Pack 1 (SP1) or later, and Windows Image Acquisition Automation Library v2.0 Tool [*:zf913s7a]Documentation Link:http://msdn.microsof...ibrary/ms630827 [*:zf913s7a]Other Links:[*:zf913s7a]Basic Code Example:
Display Detailed Image Information
The following example shows how to display detailed information about one of the sample pictures from Microsoft Windows XP.
Img := ComObjCreate("WIA.ImageFile")

Img.LoadFile("C:\test.jpg")

s := "Width = " Img.Width "`n"
    .    "Height = " Img.Height "`n"
    .    "Depth = " Img.PixelDepth "`n"
    .    "HorizontalResolution = " Img.HorizontalResolution "`n"
    .    "VerticalResolution = " Img.VerticalResolution "`n"
    .    "FrameCount = " Img.FrameCount "`n"

If Img.IsIndexedPixelFormat
{
    s := s "Pixel data contains palette indexes" "`n"
}

If Img.IsAlphaPixelFormat
{
    s := s "Pixel data has alpha information" "`n"
}

If Img.IsExtendedPixelFormat
{
    s := s "Pixel data has extended color information (16 bit/channel)" "`n"
}

If Img.IsAnimated
{
    s := s "Image is animated" "`n"
}

If Img.Properties.Exists("40091")
{
    v := Img.Properties("40091").Value
    s := s "Title = " v.String "`n"
}

If Img.Properties.Exists("40092")
{
    v := Img.Properties("40092").Value
    s := s "Comment = " v.String "`n"
}

If Img.Properties.Exists("40093")
{
    v := Img.Properties("40093").Value
    s := s "Author = " v.String "`n"
}

If Img.Properties.Exists("40094")
{
    v := Img.Properties("40094").Value
    s := s "Keywords = " v.String "`n"
}

If Img.Properties.Exists("40095")
{
    v := Img.Properties("40095").Value
    s := s "Subject = " v.String "`n"
}

MsgBox, % s
return
Take a Picture
The following example shows how to signal a camera to take a picture if the device selected is a camera.
objCommonDialog := ComObjCreate("WIA.CommonDialog")

dev := objCommonDialog.ShowSelectDevice

If (dev.Type = CameraDeviceType)
    itm := dev.ExecuteCommand(wiaCommandTakePicture)
return


shajul_nli
  • Guests
  • Last active:
  • Joined: --

[list][*]COM Object:WIA.CommonDialog/WIA.DeviceManager/WIA.ImageFile/WIA.ImageProcess/WIA.Rational/WIA.Vector


Pretty cool. Thanks for sharing.

amnesiac
  • Members
  • 124 posts
  • Last active: May 01 2014 03:04 AM
  • Joined: 07 Nov 2010
Note Windows Image Acquisition (WIA) has been replaced with WPD Automation Object Model. The WPD Application Programming Interface is supported in Windows 7, Windows Vista, and Windows XP operating systems, but the WPD Automation Object Model is only supported in Windows 7. See WPD Automation Object Model.

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
@amnesiac - nice examples!

However, your system requirements through me off. I was thinking you had to install Windows Image Acquisition Automation Library v2.0 Tool. Correct me if I'm wrong, but this ActiveX control should already be installed on any XP SP1+ operating system, correct? If that is the case, I might recommend something like this:

[*:j4gclpr0]System Requirements: Windows XP Service Pack 1 (SP1) or later
(ActiveX Control Link, if it's missing from your OS: Windows Image Acquisition Automation Library v2.0 Tool)


Additionally, could you remove the "(VS.85).aspx" form your MSDN links? That way you will get a hyperlink:

Documentation Link: <!-- m -->http://msdn.microsof...ibrary/ms630827<!-- m -->

See WPD Automation Object Model.



amnesiac
  • Members
  • 124 posts
  • Last active: May 01 2014 03:04 AM
  • Joined: 07 Nov 2010
I'm very sorry for my error: one is because of my poor English, and another is that I am not familiar with the formats or settings of the forum.

It is required to use this object model. According to the help on the download page of Microsoft's website for Windows Image Acquisition Automation Library v2.0 Tool or the help of corresponding MSDN, it is asked to install the program above all (I am using XP SP3. It has proved it out after it has been installed).

Would you like to lend me a hand? I beg you to get the content in the right form and make a new reply (if quoted in the first page, please quote this one).

sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
My first time doing this, so hopefully I got it right...


[*:3v1bmesu]COM Object: Outlook.Application
[*:3v1bmesu]Purpose: Create various new items in Outlook
[*:3v1bmesu]System Requirements: Windows 2000 or higher
[*:3v1bmesu]Documentation Link: Outlook 2003 Visual Basic Reference
[*:3v1bmesu]Other Links: CreateItem Method, GetInspector Property, OlItemType constants
[*:3v1bmesu]Basic Code Example: The entire process of creating a new Outlook item can be executed in a single line using one of the OlItemType constants shown in the below code (and in the above link) with the CreateItem method. This method is valuable if you have problems opening new items using the command line switches.
/*
olAppointmentItem   1 
olContactItem   2 
olDistributionListItem   7 
olJournalItem   4 
olMailItem   0 
olNoteItem   5 
olPostItem   6 
olTaskItem   3 
*/

; will create a new mail item
ComObjActive("Outlook.Application").CreateItem[[color=red]0[/color]].GetInspector.Display()

; will create a new note item
ComObjActive("Outlook.Application").CreateItem[[color=red]5[/color]].GetInspector.Display()

And here's a sample ListBox GUI that will allow you to select and create a new instance of any of the applicable Outlook items:

Gui, Add, Text, xm ym w325 Section, Select the Outlook item you wish to create:
Gui, Add, ListBox, r6 xs wp AltSubmit gItemSelection vitem,  Mail||Appointment|Contact|Task|Journal|Note|Post|Distribution List
Gui, Add, Button, xs+295 gCreateItem, OK
Gui, Show, AutoSize Center, Create New Outlook Item
return

ItemSelection:
CreateItem:
if	(A_ThisLabel="ItemSelection" && A_GuiEvent<>"DoubleClick")
	return
Gui, Submit
ComObjActive("Outlook.Application").CreateItem[item-1].GetInspector.Display()
Gui, Destroy
return

GuiCancel:
GuiClose:
Gui, Destroy
return


jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
@sinkfaze - thanks for the example :)

I'm very sorry for my error

You're post is good, I was just being picky. Could you remove the "(VS.85).aspx" from you Documentation Link? It will show up as a hyperlink then.

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
Hey, guys
Why does this code won't work? (the msgbox never appears)
#Persistent

winmgmts := ComObjGet("winmgmts:")
deleteSink := ComObjCreate("WbemScripting.SWbemSink")
ComObjConnect(deleteSink,"ProcessDelete_")
interval := 1
winmgmts.ExecNotificationQueryAsync(deleteSink, "SELECT * FROM __InstanceDeletionEvent WITHIN " . interval . " WHERE TargetInstance ISA 'Win32_Process'")

ProcessDelete_OnObjectReady(objWbemObject,objWbemAsyncContext) 
{
msgbox Process Closed!
}

What did i missed?
The original code is here (it works with ahk base):
COM_Init()

; Get WMI service object.
winmgmts := COM_GetObject("winmgmts:")

; Create sink objects for receiving event noficiations.
COM_ConnectObject(deleteSink := COM_CreateObject("WbemScripting.SWbemSink"), "ProcessDelete_")

; Set event polling interval, in seconds.
interval := 1

; Register for process deletion notifications:
COM_Invoke(winmgmts, "ExecNotificationQueryAsync", "+" deleteSink
	, "Select * from __InstanceDeletionEvent"
	. " within " interval
	. " where TargetInstance isa 'Win32_Process'")

ProcessDelete_OnObjectReady(prm)
{
obj := COM_DispGetParam(prm, 0, 9)
proc := COM_Invoke(obj, "TargetInstance")
COM_Release(obj)
;http://msdn.microsoft.com/en-us/library/aa394372.aspx
proc_cmd := COM_Invoke(proc, "CommandLine")
proc_name := COM_Invoke(proc, "Name")
proc_exe := COM_Invoke(proc, "ExecutablePath")
proc_pID := COM_Invoke(proc, "ParentProcessID")
proc_ID := COM_Invoke(proc, "Handle")

COM_Release(proc)
}


Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
can someone help me plz?

a4u
  • Guests
  • Last active:
  • Joined: --

Why does this code won't work? (the msgbox never appears)

A msgbox never appears if I run the AHK Basic code either...

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
because there is no msgbox in original code lol
you can see how its working from this one (use COM.ahk)
#Persistent
#NoEnv
#Include COM.ahk

COM_Init()

; Get WMI service object.
winmgmts := COM_GetObject("winmgmts:")

; Create sink objects for receiving event noficiations.
COM_ConnectObject(deleteSink := COM_CreateObject("WbemScripting.SWbemSink"), "ProcessDelete_")

; Set event polling interval, in seconds.
interval := 1

; Register for process deletion notifications:
COM_Invoke(winmgmts, "ExecNotificationQueryAsync", "+" deleteSink
	, "Select * from __InstanceDeletionEvent"
	. " within " interval
	. " where TargetInstance isa 'Win32_Process'")

return

ProcessDelete_OnObjectReady(prm) {
	
	local obj, proc
	
	obj := COM_DispGetParam(prm, 0, 9)
	proc := COM_Invoke(obj, "TargetInstance")
	COM_Release(obj)
	;http://msdn.microsoft.com/en-us/library/aa394372.aspx
	proc_cmd := COM_Invoke(proc, "CommandLine")
	proc_name := COM_Invoke(proc, "Name")
	proc_exe := COM_Invoke(proc, "ExecutablePath")
	proc_pID := COM_Invoke(proc, "ParentProcessID")
	proc_ID := COM_Invoke(proc, "Handle")
	
	COM_Release(proc)
	msgbox % proc_cmd "`n" proc_name "`n" proc_exe "`n" proc_pID "`n" proc_ID
	return

}