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:
  • Joined: 24 May 2009

I downloaded ImageMagick-6.6.5-2-Q16-windows-dll.exe & installed it, but ImageMagickObject.MagickImage.1 still isn't a valid class on my computer.

Have you tried this (in app dir)?

regsvr32 ImageMagickObject.dll

Yes - you're right. I had to register the DLL. BUT, more importantly, during installation there's a screen prompting: Select Additional Tasks. On this screen, one should make sure the check box is selected for Install ImageMagickObject OLE Control for VBscript, Visual Basic, and WSH.

olfen
  • Members
  • 115 posts
  • Last active: Dec 25 2012 09:48 AM
  • Joined: 04 Jun 2005
Sorry, I should have mentioned explicitly.

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

COM Object: Excel.Application
Purpose: Perform calculations, analyse information and visualise data in spreadsheets.
System Requirements: Microsoft Office Excel application
Documentation Link: <!-- m -->http://msdn.microsof...ry ... 12).aspx<!-- m -->
Other Links:
Basic Code Example:


oExcel := ComObjCreate("Excel.Application") ; create Excel Application object
oExcel.Workbooks.Add ; create a new workbook (oWorkbook := oExcel.Workbooks.Add)

oExcel.Range("A1").Value := 3 ; set cell A1 to 3
oExcel.Range("A2").Value := 7 ; set cell A2 to 7
oExcel.Range("A3").Formula := "=SUM(A1:A2)" ; set formula for cell A3 to SUM(A1:A2)

oExcel.Range("A1:A3").Interior.ColorIndex := 19 ; fill range of cells from A1 to A3 with color number 19
oExcel.Range("A3").Borders(8).LineStyle := 1 ; set top border line style for cell A3 (xlEdgeTop = 8, xlContinuous = 1)
oExcel.Range("A3").Borders(8).Weight := 2 ; set top border weight for cell A3 (xlThin = 2)
oExcel.Range("A3").Font.Bold := 1 ; set bold font for cell A3

A1 := oExcel.Range("A1").Value ; get value from cell A1, and store it in A1 variable
oExcel.Range("A4").Select ; select A4 cell
oExcel.Visible := 1 ; make Excel Application visible
MsgBox % A1 "`n" oExcel.Range("A2").Value ; check. You can use Round() function to round numbers to the nearest integer
ExitApp

How to access Workbook without opening it?



FilePath := "C:\Book1.xlsx" ; example path
oWorkbook := ComObjGet(FilePath) ; access Workbook object
MsgBox % oWorkbook.Sheets(1).Range("A1").Value ; get value from A1 cell in first sheet

How to access active Excel Application object?
Use oExcel := Excel_Get(), not oExcel := ComObjActive("Excel.Application"). More info here.
How to access active Workbook?



try
oWorkbook := Excel_Get().ActiveWorkbook ; try to access active Workbook object
catch
return ; case when Excel doesn't exist, or it exists but there is no active workbook. Just Return or Exit or ExitApp.

; if there is active workbook, code continues execution...
oWorkbook.ActiveSheet.Range("B2").Value := "B2" ; set value of B2 cell in active sheet to "B2"

How to access Excel object from Workbook object?



oExcel := oWorkbook.Application ; returns Excel application object that owns Workbook object


jethrow
  • Moderators
  • 2854 posts
  • Last active:
  • Joined: 24 May 2009
Thank you for the Excel Post :wink:

Btw, perhaps the wiki is a good place to put this reference (without the discussion).

This is a good idea, if anyone wants to update the wiki.

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006

COM Object: PowerPoint.Application
Purpose: Create powerpoint presentations
System Requirements:Microsoft Office Powerpoint application, OS>Win2000
Documentation Link:MSDN article,WMI article
Other Links:Application Object
Basic Code Example:


objPPT := ComObjCreate("PowerPoint.Application")
objPPT.Visible := True
objPresentation := objPPT.Presentations.Add
objPresentation.ApplyTemplate("C:\Program Files\Microsoft Office\Templates\Presentation Designs\Globe.pot")

strComputer := "."
objWMIService := ComObjGet("winmgmts:\\" . strComputer . "\root\cimv2")
colProcesses := objWMIService.ExecQuery("Select * From Win32_Process")._NewEnum

While colProcesses[objProcess]
{
objSlide := objPresentation.Slides.Add(1,2)
objShapes := objSlide.Shapes

objTitle := objShapes.Item("Rectangle 2")
objTitle.TextFrame.TextRange.Text := objProcess.Name

strText := "Working set size: " . objProcess.WorkingSetSize . "`r`n"
strText .= "Priority: " . objProcess.Priority . "`r`n"
strText .= "Thread count: " . objProcess.ThreadCount . "`r`n"

objTitle := objShapes.Item("Rectangle 3")
objTitle.TextFrame.TextRange.Text := strText
}

objPresentation.SaveAs(A_ScriptDir . "\Process.ppt")
MsgBox % objPresentation.Slides.Count . " slides saved to " . A_ScriptDir . "\Process.ppt"
objPresentation.Close
objPPT.Quit


shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006

COM Object: XStandard.Zip
Purpose: Zip & Unzip with many features
System Requirements: 32-bit OS, XZip.dll
Documentation Link: http://www.xstandard...mentation/xzip/
Other Links:

Basic Code Example:

How to archive (or zip) multiple files

objZip := ComObjCreate("XStandard.Zip")
objZip.Pack("C:\Temp\golf.jpg", "C:\Temp\images.zip")
objZip.Pack("C:\Temp\racing.gif", "C:\Temp\images.zip")

How to archive (or zip) multiple files with different compression levels

objZip := ComObjCreate("XStandard.Zip")
objZip.Pack("C:\Temp\reports.doc", "C:\Temp\archive.zip","" ,"" , 9)
objZip.Pack("C:\Temp\boat.jpg", "C:\Temp\archive.zip","" ,"" , 1)

How to archive (or zip) multiple files with default path

objZip := ComObjCreate("XStandard.Zip")
objZip.Pack("C:\Temp\reports.doc", "C:\Temp\archive.zip", True)
objZip.Pack("C:\Temp\boat.jpg", "C:\Temp\archive.zip", True)

How to archive (or zip) multiple files with a custom pat

objZip := ComObjCreate("XStandard.Zip")
objZip.Pack("C:\Temp\reports.doc", "C:\Temp\archive.zip", True, "files/word")
objZip.Pack("C:\Temp\boat.jpg", "C:\Temp\archive.zip", True, "files/images")

How to archive (or zip) multiple files using wildcards

objZip := ComObjCreate("XStandard.Zip")
objZip.Pack("C:\Temp\*.jpg", "C:\Temp\images.zip")

How to unzip files

objZip := ComObjCreate("XStandard.Zip")
objZip.UnPack("C:\Temp\images.zip", "C:\Temp\")

How to unzip files using wildcards

objZip := ComObjCreate("XStandard.Zip")
objZip.UnPack("C:\Temp\images.zip", "C:\Temp\", "*.jpg")

How to get a listing of files and folder in an archive

objZip := ComObjCreate("XStandard.Zip")
objContents := objZip.Contents(A_ScriptDir . "\test.zip")._NewEnum
While objContents[objItem]
Msgbox % objItem.Path . objItem.Name . "`n"

How to remove a file from an archive

objZip := ComObjCreate("XStandard.Zip")
objZip.Delete("headshots/smith.jpg", "C:\Temp\images.zip")

How to move a file in an archive

objZip := ComObjCreate("XStandard.Zip")
objZip.Move("headshots/jones.jpg", "staff/jones.jpg", "C:\Temp\images.zip")

How to rename a file in an archive

objZip := ComObjCreate("XStandard.Zip")
objZip.Move("headshots/jones.jpg", "headshots/randy-jones.jpg", "C:\Temp\images.zip")


jethrow
  • Moderators
  • 2854 posts
  • Last active:
  • Joined: 24 May 2009
Very Nice - just tested out a few examples :) . I however didn't like that I had to enter my email to download the file. So, with some quick searching I found this download for the DLL (v2.5 - trial version). I'd recommend including this, or some other link to a direct download, in your post.

sbc
  • Members
  • 321 posts
  • Last active: Jun 07 2011 10:24 AM
  • Joined: 25 Aug 2009

COM Object: Shell.Application
Purpose: Retrieves file properties.
System Requirements: General
Documentation Link: GetDetailsOf, ParseName
Other Links: Retrieving Extended File Properties
Basic Code Example - this example demonstrates how to retrieve the properties of a specified file :
 

filepath := "C:\Windows\System32\notepad.exe"	;set the target full file path

SplitPath, filepath , FileName, DirPath,

objShell := ComObjCreate("Shell.Application")
objFolder := objShell.NameSpace(DirPath)		;set the directry path
objFolderItem := objFolder.ParseName(FileName)	;set the file name

Loop 283	
   if propertyitem := objFolder.GetDetailsOf(objFolderItem, A_Index)
      properties .= A_Index . " " .  objFolder.GetDetailsOf(objFolder.Items, A_Index) . ":`t " . propertyitem . "`n"

msgbox % properties


shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006

Very Nice - just tested out a few examples :) . I however didn't like that I had to enter my email to download the file. So, with some quick searching I found this download for the DLL (v2.5 - trial version). I'd recommend including this, or some other link to a direct download, in your post.


Thanks, added.

sbc
  • Members
  • 321 posts
  • Last active: Jun 07 2011 10:24 AM
  • Joined: 25 Aug 2009

The previous example is for extended properties. This is for general properties.
 
COM Object: winmgmts
Purpose: Retrieves file&folder properties.
System Requirements: General
Documentation Link: Enumerating Folders and Folder Properties, Enumerating Files and File Properties
Basic Code Example - this example demonstrates how to retrieve the properties of a specified file and folder:
 

filepath := "C:\Windows\System32\notepad.exe"	;set the target full file path
StringReplace, filepath, filepath, \, \\, All	;double backslashes
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . A_ComputerName . "\root\cimv2") 
WQLQuery = SELECT * FROM CIM_Datafile WHERE Name = '%filepath%'		;CIM_Datafile
colFiles := objWMIService.ExecQuery(WQLQuery)._NewEnum 

While colFiles[objFile] {
	FileProperties := "Access mask:`t`t" . objFile.AccessMask . "`n"
	. "Archive:`t`t`t" . objFile.Archive . "`n"
	. "Compressed:`t`t" . objFile.Compressed . "`n"
	. "Compression method:`t`t" . objFile.CompressionMethod . "`n"
	. "Creation date:`t`t" . objFile.CreationDate . "`n"
	. "Computer system name:`t" . objFile.CSName . "`n"
	. "Drive:`t`t`t" . objFile.Drive . "`n"
	. "8.3 file name:`t`t" . objFile.EightDotThreeFileName . "`n"
	. "Encrypted:`t`t" . objFile.Encrypted . "`n"
	. "Encryption method:`t" . objFile.EncryptionMethod . "`n"
	. "Extension:`t`t`t" . objFile.Extension . "`n"
	. "File Name:`t`t" . objFile.FileName . "`n"
	. "File Size:`t`t`t" . objFile.FileSize . "`n"
	. "File Type:`t`t`t" . objFile.FileType . "`n"
	. "File system name:`t`t" . objFile.FSName . "`n"
	. "Hidden:`t`t`t" . objFile.Hidden . "`n"
	. "Last accessed:`t`t" . objFile.LastAccessed . "`n"
	. "Last modified:`t`t" . objFile.LastModified . "`n"
	. "Manufacturer:`t`t" . objFile.Manufacturer . "`n"
	. "Name:`t`t`t" . objFile.Name . "`n"
	. "Path:`t`t`t" . objFile.Path . "`n"
	. "Readable:`t`t`t" . objFile.Readable . "`n"	
	. "System:`t`t`t" . objFile.System . "`n"
	. "Version:`t`t`t" . objFile.Version . "`n"
	. "Writeable:`t`t`t" . objFile.Writeable 
}
msgbox % FileProperties
folderpath := "C:\Windows\System32"	;set the target full foler path
folderpath := RTrim(folderpath,"\") ;remove a trailing backslash, if present 
StringReplace, folderpath, folderpath, \, \\, All	;double backslashes
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . [color=brown]A_ComputerName . "\root\cimv2") 
WQLQuery = SELECT * FROM Win32_Directory WHERE Name = '%folderpath%'	;Win32_Directory
colFolder := objWMIService.ExecQuery(WQLQuery)._NewEnum 	
While colFolder[objFolder] {
	FolderProperties := "Archive:`t`t`t" . objFolder.Archive . "`n"
	. "Caption:`t`t`t" . objFolder.Caption . "`n"
	. "Compressed:`t`t" . objFolder.Compressed . "`n"
	. "Compression method:`t" . objFolder.CompressionMethod . "`n"
	. "Creation date:`t`t" . objFolder.CreationDate . "`n"
	. "Encrypted:`t`t" . objFolder.Encrypted . "`n"
	. "Encryption method:`t`t" . objFolder.EncryptionMethod . "`n"
	. "Hidden:`t`t`t" . objFolder.Hidden . "`n"
	. "In use count:`t`t" . objFolder.InUseCount . "`n"
	. "Last accessed:`t`t" . objFolder.LastAccessed . "`n"
	. "Last modified:`t`t" . objFolder.LastModified . "`n"
	. "Name:`t`t`t" . objFolder.Name . "`n"
	. "Path:`t`t`t" . objFolder.Path . "`n"
	. "Readable:`t`t`t" . objFolder.Readable . "`n"
	. "System:`t`t`t" . objFolder.System . "`n"
	. "Writeable:`t`t`t" . objFolder.Writeable
}
msgbox % FolderProperties


jethrow
  • Moderators
  • 2854 posts
  • Last active:
  • Joined: 24 May 2009
Thanks for the examples sbc. A few things:
[*:2hs5czjm]Could you separate the winmgmts & Scripting.FileSystemObject into two threads so it's easier to reference on the first page?[*:2hs5czjm]For the FileSystemObject, all the comments make it look confusing (or maybe my screen is just too small). I noticed that the links all start with "http://msdn.microsoft.com/en-us/library/" - perhaps note this and just put the end of each link for comments?[*:2hs5czjm]Also, fyi, are you familiar with the Trim() functions? I noticed you trimmed a "\" using SubStr - which works, but this might be simpler:
folderpath := "C:\Windows\System32\"   ;set the target's full folder path
folderpath := RTrim(folderpath,"\") ;remove a trailing backslash, if present
MsgBox, %folderpath%
Also, for anyone posting, once the For-loop is fixed in v1.0.91 I would recommend modifying any posts to reflect this syntax rather than while enum[obj].

sbc
  • Members
  • 321 posts
  • Last active: Jun 07 2011 10:24 AM
  • Joined: 25 Aug 2009
Another way to retrieve file/folder properties.
[*:3u1rpt1s]COM Object: Scripting.FileSystemObject[*:3u1rpt1s]Purpose: Retrieves file&folder properties.[*:3u1rpt1s]System Requirements: General[*:3u1rpt1s]Documentation Link: FileSystemObject[*:3u1rpt1s]DateLastAccessed Property, DateCreated Property, DateLastModified Property[*:3u1rpt1s]Size Property[*:3u1rpt1s]Name Property[*:3u1rpt1s]Shortname Property[*:3u1rpt1s]Type property[*:3u1rpt1s]Attributes Property[*:3u1rpt1s]Path Property[*:3u1rpt1s]Shortpath Property[*:3u1rpt1s]Basic Code Example - this example demonstrates how to retrieve the properties of a specified file and folder via FileSystemObject:[/list]
filepath := [color=#666666]"C:\Windows\System32\notepad.exe"[/color]   ;set the target's full file path
folderpath := [color=#666666]"C:\Windows\System32"[/color]   ;set the target's full folder path

objFSO := [color=#107095]ComObjCreate[/color]([color=#666666]"Scripting.FileSystemObject"[/color])
[color=#107095]ComObjError[/color](false)   ;disable COM error messages for some non available properties
objFile := objFSO.GetFile(filepath)
objFolder := objFSO.GetFolder(folderpath)

FileProperties := [color=#666666]"Date Created:`t`t"[/color] . objFile.DateCreated . [color=#666666]"`n"[/color]  
   . [color=#666666]"Last Accessed:`t`t"[/color] . objFile.DateLastAccessed . [color=#666666]"`n"[/color]
   . [color=#666666]"Last Modified:`t`t"[/color] . objFile.DateLastModified . [color=#666666]"`n"[/color]
   . [color=#666666]"File Size(bytes):`t`t"[/color] . objFile.Size . [color=#666666]"`n"[/color]  
   . [color=#666666]"File Name:`t`t"[/color] . objFile.Name . [color=#666666]"`n"[/color] 
   . [color=#666666]"File Short Name:`t`t"[/color] . objFile.ShortName . [color=#666666]"`n"[/color] 
   . [color=#666666]"File Type:`t`t`t"[/color] . objFile.Type . [color=#666666]"`n"[/color]  
   ;Attributes 0: Normal 1:ReadOnly 2:Hidden 4:System 8:Volume 16:Directroy 32:Archive 1024:Alias 2048:Compressed
   . [color=#666666]"Attributes:`t`t`t"[/color] . objFile.attributes . [color=#666666]"`n"[/color]  
   . [color=#666666]"File Path:`t`t`t"[/color] . objFile.Path  . [color=#666666]"`n"[/color]   
   . [color=#666666]"File Short Path:`t`t"[/color] . objFile.ShortPath  . [color=#666666]"`n"[/color]   
   
FolderProperties := [color=#666666]"Date Created:`t`t"[/color] . objFolder.DateCreated . [color=#666666]"`n"[/color] 
   . [color=#666666]"Last Accessed:`t`t"[/color] . objFolder.DateLastAccessed . [color=#666666]"`n"[/color]
   . [color=#666666]"Last Modified:`t`t"[/color] . objFolder.DateLastModified . [color=#666666]"`n"[/color]
   . [color=#666666]"Size(bytes):`t`t"[/color] . objFolder.Size . [color=#666666]"`n"[/color]
   . [color=#666666]"Folder Name:`t`t"[/color] . objFolder.Name . [color=#666666]"`n"[/color]
   . [color=#666666]"Folder Short Name:`t`t"[/color] . objFolder.ShortName . [color=#666666]"`n"[/color] 
   . [color=#666666]"Folder Type:`t`t"[/color] . objFolder.Type . [color=#666666]"`n"[/color]  
   . [color=#666666]"Attributes:`t`t`t"[/color] . objFolder.attributes . [color=#666666]"`n"[/color] 
   . [color=#666666]"Folder Path:`t`t"[/color] . objFolder.Path  . [color=#666666]"`n"[/color]  
   . [color=#666666]"Folder Short Path:`t`t"[/color] . objFolder.ShortPath  
   
[color=#107095]ComObjError[/color](true)      

[color=#107095]msgbox[/color] % FileProperties
[color=#107095]msgbox[/color] % FolderProperties

Thanks for the examples sbc. A few things:
[*:3u1rpt1s]Could you separate the winmgmts & Scripting.FileSystemObject into two threads so it's easier to reference on the first page?

No problem. Also removed the links in the code comments and put them in the template. Changed to use RTrim() for a trailing backslash as well.

Also, for anyone posting, once the For-loop is fixed in v1.0.91 I would recommend modifying any posts to reflect this syntax rather than while enum[obj].

Currently I'm using R61 forthe regex bug. (My scripts have started not working since the last update.) As long as the bug persists, I may not be able to use the For loop for objects.

By the way, I'm wondering if somebocy can write examples for ResponseStream. :roll:

sbc
  • Members
  • 321 posts
  • Last active: Jun 07 2011 10:24 AM
  • Joined: 25 Aug 2009
[*:1h6k2tk4]COM Object: winmgmts[*:1h6k2tk4]Purpose: Manipulating Services[*:1h6k2tk4]System Requirements: General[*:1h6k2tk4]Documentation Link:[*:1h6k2tk4]Win32_Service Class
[*:1h6k2tk4]WMI Scripting Primer: Part 3
[*:1h6k2tk4]Services[*:1h6k2tk4]Other Links: Scripts to manage Services[*:1h6k2tk4]Basic Code Example - retrieves service information and demonstrates starting and stopping a service:[/list]
objWMIService := [color=#107095]ComObjGet[/color]([color=#666666]"winmgmts:{impersonationLevel=impersonate}!\\"[/color] . [color=brown]A_ComputerName[/color] . [color=#666666]"\root\cimv2"[/color]) 

;Determining the services which can be stopped
WQLQuery = SELECT * FROM Win32_Service WHERE AcceptStop = True		;"AcceptStop = True"
colServices := objWMIService.ExecQuery(WQLQuery)._NewEnum
count := 0
[color=#107095]While[/color] colServices[objService] && ++count
	StoppableServices .= objService.DisplayName . [color=#666666]"`n"[/color]
[color=#107095]MsgBox[/color],, Services which can be stopped: %count%, %StoppableServices%

;Determining the services wich can be paused
WQLQuery = SELECT * FROM Win32_Service WHERE AcceptPause = True		;AcceptPause = True
colServices := objWMIService.ExecQuery(WQLQuery)._NewEnum
count := 0
[color=#107095]While[/color] colServices[objService] && ++count
	PausableServices .= objService.DisplayName . [color=#666666]"`n"[/color]
[color=#107095]MsgBox[/color],, Services which can be paused: %count%, %PausableServices%

;Retrieves a complete list of services and their associated properties.
WQLQuery = Select * from Win32_Service
colServices := objWMIService.ExecQuery(WQLQuery)._NewEnum
[color=#107095]While[/color] colServices[objService] {
	ServicePropertyList .= objService.DisplayName . [color=#666666]"`n"[/color]
	. [color=#666666]"`tSystemName:`t"[/color] . objService.SystemName . [color=#666666]"`n"[/color]
	. [color=#666666]"`tName:`t"[/color] . objService.Name . [color=#666666]"`n"[/color]
	. [color=#666666]"`tServiceType:`t"[/color] . objService.ServiceType . [color=#666666]"`n"[/color]
	. [color=#666666]"`tState:`t"[/color] . objService.State . [color=#666666]"`n"[/color]
	. [color=#666666]"`tExitCode:`t"[/color] . objService.ExitCode . [color=#666666]"`n"[/color]
	. [color=#666666]"`tProcessID:`t"[/color] . objService.ProcessID . [color=#666666]"`n"[/color] 
	. [color=#666666]"`tAcceptPause:`t"[/color] . objService.AcceptPause . [color=#666666]"`n"[/color] 
	. [color=#666666]"`tAcceptStop:`t"[/color] . objService.AcceptStop . [color=#666666]"`n"[/color] 
	. [color=#666666]"`tCaption:`t"[/color] . objService.Caption . [color=#666666]"`n"[/color] 
	. [color=#666666]"`tDescription:`t"[/color] . objService.Description . [color=#666666]"`n"[/color] 
	. [color=#666666]"`tDesktopInteract:`t"[/color] . objService.DesktopInteract . [color=#666666]"`n"[/color] 
	. [color=#666666]"`tDisplayName:`t"[/color] . objService.DisplayName . [color=#666666]"`n"[/color] 
	. [color=#666666]"`tErrorControl:`t"[/color] . objService.ErrorControl . [color=#666666]"`n"[/color] 
	. [color=#666666]"`tPathName:`t"[/color] . objService.PathName . [color=#666666]"`n"[/color] 
	. [color=#666666]"`tStarted:`t"[/color] . objService.Started . [color=#666666]"`n"[/color] 
	. [color=#666666]"`tStartMode:`t"[/color] . objService.StartMode . [color=#666666]"`n"[/color] 
	. [color=#666666]"`tStartName:`t"[/color] . objService.StartName . [color=#666666]"`n"[/color] 	
}
logfile = %[color=brown]A_ScriptDir[/color]%\ServicePropertyList.txt
[color=#107095]FileDelete[/color], %logfile%
[color=#107095]FileAppend[/color], %ServicePropertyList%, %logfile%
[color=#107095]Run[/color], Notepad [color=#666666]"%logfile%"[/color]
;Run As Admin
[color=#107095]if[/color] not [color=brown]A_IsAdmin[/color] {
    [color=#107095]If[/color] [color=brown]A_IsCompiled[/color]
       [color=#107095]DllCall[/color]([color=#666666]"shell32\ShellExecute"[/color] . ([color=brown]A_IsUnicode[/color] ? [color=#666666]""[/color] :[color=#666666]"A"[/color]), ([color=brown]A_PtrSize[/color]=8 ? ptr : uint), 0, str, [color=#666666]"RunAs"[/color], str, [color=brown]A_ScriptFullPath[/color], str, params , str, [color=brown]A_WorkingDir[/color], int, 1)
    [color=#107095]Else[/color]
       [color=#107095]DllCall[/color]([color=#666666]"shell32\ShellExecute"[/color] . ([color=brown]A_IsUnicode[/color] ? [color=#666666]""[/color] :[color=#666666]"A"[/color]), ([color=brown]A_PtrSize[/color]=8 ? ptr : uint), 0, str, [color=#666666]"RunAs"[/color], str, [color=brown]A_AhkPath[/color], str, [color=#666666]""[/color][color=#666666]""[/color] . [color=brown]A_ScriptFullPath[/color] . [color=#666666]""[/color][color=#666666]""[/color] . [color=brown]A_Space[/color] . params, str, [color=brown]A_WorkingDir[/color], int, 1)
    [color=#107095]ExitApp[/color]
}

objWMIService := [color=#107095]ComObjGet[/color]([color=#666666]"winmgmts:{impersonationLevel=impersonate}!\\"[/color] . [color=brown]A_ComputerName[/color] . [color=#666666]"\root\cimv2"[/color]) 

;Determinig which services are currently stopped and the start mode is 'Manual'
WQLQuery = SELECT DisplayName FROM Win32_Service Where State='Stopped' [color=#107095]AND[/color] StartMode='Manual'
colServices := objWMIService.ExecQuery(WQLQuery)._NewEnum
count := 0
[color=#107095]While[/color] colServices[objService] && ++count
	StoppedServices .= objService.DisplayName . [color=#666666]"`n"[/color]
[color=#107095]MsgBox[/color],, Services Currently Stopped [color=#107095]and[/color] 'Manual': %count%, %StoppedServices%

;Start the First Service in the List of the Stopped Services of the 'Manual' mode
colServices := objWMIService.ExecQuery(WQLQuery)._NewEnum
[color=#107095]While[/color] colServices[objService] {
	DemoRunService := objService.DisplayName
	[color=#107095]if[/color] errReturnCode := objService.StartService()
		[color=#107095]MsgBox[/color], 48,, Error Code: %errReturnCode%`nCould Not Start the Service, %DemoRunService%
	[color=#107095]else[/color] {
		[color=#107095]Msgbox[/color] %DemoRunService% is Started. After OK is Pressed, It Will be Stopped.
		[color=#107095]if[/color] errReturnCode := objService.StopService()
			[color=#107095]MsgBox[/color], 48,, Error Code: %errReturnCode%`nCould Not Stop the Service, %DemoRunService%
		[color=#107095]else[/color]
			[color=#107095]Msgbox[/color] Successfully Stopped the Service : %DemoRunService%
	}
	[color=#107095]Break[/color]	;just one service to start and stop for demo
}


sbc
  • Members
  • 321 posts
  • Last active: Jun 07 2011 10:24 AM
  • Joined: 25 Aug 2009
[*:38c2kjzw]COM Object: WScript.Shell
[*:38c2kjzw]Purpose: Various Administration Tasks
[*:38c2kjzw]System Requirements: General, WSH vertion 5.6 for the Exc() method
[*:38c2kjzw]Documentation Link WshShell Object
[*:38c2kjzw]Other Links: WSH Primer
[*:38c2kjzw]Basic Code Example:
;Retrieve console's outputs
;http://msdn.microsoft.com/en-us/library/cbxxzwb5%28v=VS.85%29.aspx
;http://technet.microsoft.com/en-us/library/ee156605.aspx
objShell := [color=#107095]ComObjCreate[/color]([color=#666666]"WScript.Shell"[/color])
objExec := objShell.Exec([color=#666666]"ipconfig.exe"[/color])
[color=#107095]While[/color] !objExec.Status	;wait until ipconfig.exe starts
    [color=#107095]Sleep[/color] 100
strLine := objExec.StdOut.ReadAll()	;read the output at once
[color=#107095]msgbox[/color] % strLine   

objExec := objShell.Exec([color=#666666]"cmd /c ping -n 3 -w 1000 www.google.com"[/color])
[color=#107095]While[/color] !objExec.StdOut.AtEndOfStream ;read the output line by line
	[color=#107095]if[/color] [color=#107095]InStr[/color](objExec.StdOut.ReadLine(), [color=#666666]"Reply"[/color]) {
		[color=#107095]Msgbox[/color] Reply received.
		[color=#107095]Break[/color]
	}

;Retrieve Special Folder paths
;http://technet.microsoft.com/en-us/library/ee156616.aspx
[color=#107095]Msgbox[/color] % [color=#666666]"Desktop:`n`t"[/color] . objShell.SpecialFolders([color=#666666]"Desktop"[/color]) . [color=#666666]"`n"[/color]
    . [color=#666666]"Favorites:`n`t"[/color] . objShell.SpecialFolders([color=#666666]"Favorites"[/color]) . [color=#666666]"`n"[/color]
    . [color=#666666]"MyDocuments:`n`t"[/color] . objShell.SpecialFolders([color=#666666]"MyDocuments"[/color]) . [color=#666666]"`n"[/color]
    . [color=#666666]"SendTo:`n`t"[/color] . objShell.SpecialFolders([color=#666666]"SendTo"[/color]) . [color=#666666]"`n"[/color]
    . [color=#666666]"StartMenu:`n`t"[/color] . objShell.SpecialFolders([color=#666666]"StartMenu"[/color]) . [color=#666666]"`n"[/color]
    . [color=#666666]"Startup:`n`t"[/color] . objShell.SpecialFolders([color=#666666]"Startup"[/color])
	
;Add logs to Application Event Log 	
;http://technet.microsoft.com/en-us/library/ee156617.aspx
objShell.LogEvent(0, [color=#666666]"Test Writing: Success"[/color])
objShell.LogEvent(1, [color=#666666]"Test Writing: Error"[/color])
objShell.LogEvent(2, [color=#666666]"Test Writing: Warning"[/color])
objShell.LogEvent(4, [color=#666666]"Test Writing: Information"[/color])
objShell.LogEvent(8, [color=#666666]"Test Writing: Audit Sucess"[/color])
objShell.LogEvent(16, [color=#666666]"Test Writing: Audit Failure"[/color])	

;Hello World Script with MessageBox and Notepad
objExec := objShell.Exec([color=#666666]"notepad.exe"[/color])			;Run, notepad,,, PID
[color=#107095]While[/color] !objShell.AppActivate(objExec.ProcessID)	;WinWaitActive, ahk_pid %PID%
	[color=#107095]sleep[/color] 100
objShell.SendKeys([color=#666666]"Hello World!{Enter}"[/color]) ;Send, Hell World{Enter}
iBtn := objShell.Popup([color=#666666]"Do you cancel?"[/color], 5, [color=#666666]"Continue?"[/color], 33)	;Msgbox,33, Continue?, Do you cancel?, 5
[color=#107095]if[/color] iBtn = 1         ;OK is pressed
    objShell.Popup([color=#666666]"Done"[/color], 5, [color=#666666]"You pressed OK"[/color], 64)
[color=#107095]else[/color] [color=#107095]if[/color] iBtn = 2    ;Cancel is pressed
    objShell.Popup([color=#666666]"Canceled"[/color], 5, [color=#666666]"You pressed Cancel"[/color], 64)
[color=#107095]else[/color]	;Timeout or other reasons
    objShell.Popup([color=#666666]"Forced Execution"[/color], 5, [color=#666666]"None pressed"[/color], 64)
objExec.Terminate()								;Process, Close, %PID%

And for a report,

oSC := [color=#107095]ComObjCreate[/color]([color=#666666]"ScriptControl"[/color])

I get the following error with the AHK_L Unicode 64bit build on Win7. The error does not occur with the Unicode 32bit build.

---------------------------
Error: 0x80040154 - Class not registered
Line#
---> 004: oSC := ComObjCreate("ScriptControl")



jethrow
  • Moderators
  • 2854 posts
  • Last active:
  • Joined: 24 May 2009
Thanks for the example :) . It may be worth noting that much of what can be done with WScript.Shell can be done natively with AHK. Additionally (and perhaps I should note this in the original post) but when I think documentation link, I think of a list of properties/methods & corresponding documentation (such as this).

Error: 0x80040154 - Class not registered
Line#
---> 004: oSC := ComObjCreate("ScriptControl")

I cannot test since I'm running XP, but you could try CLSID Registry Scanner [COM/ActiveX], which would show you the registered Classes. I would be interested to see what you find.