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
HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
Run your AHK_L code then open e.g. Task Manager and close it, MsgBox is displayed.

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
my mistake
seems like it works fine, i just forgot to make objects global
here is a final code for logging closed processes:
EnableDeleteNotification()
{
	global
	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'")
	return
}

; Called when a process terminates:
ProcessDelete_OnObjectReady(objWbemObject,objWbemAsyncContext) 
{
	global
	local proc
	proc := objWbemObject.TargetInstance
	;http://msdn.microsoft.com/en-us/library/aa394372.aspx
	proc_cmd := proc.CommandLine
	proc_name := proc.Name
	proc_exe := proc.ExecutablePath
	proc_pID := proc.ParentProcessID
	proc_ID := proc.Handle

	msgbox % proc_cmd "`n" proc_exe "`n" proc_name "`n" proc_ID "`n" proc_pID

	return
}

StopClProcNotify()
{
	global
	ObjRelease(winmgmts)
	ObjRelease(deleteSink)
	winmgmts := deleteSink := ""
	return
}


fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
You don't need to use ObjRelease, it is automatically handled by AHK_L.

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
ok, thank you

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
how can i call object method similar to this VBScript code?

oXL = CreateObject("Excel.Application")
oWB = oXL.Workbooks.Add
[color=red]oWB.Sheets.Add After:=oWB.Sheets(oWB.Sheets.Count)[/color]
i'm trying following and all this gives me error:
oWB.Sheets.Add("",oWB.Sheets(oWB.Sheets.Count))
oWB.Sheets.Add(0,oWB.Sheets(oWB.Sheets.Count))
oWB.Sheets.Add("After",oWB.Sheets(oWB.Sheets.Count))
<!-- m -->http://msdn.microsof...ry ... 12).aspx<!-- m -->
the only one works is
oWB.Sheets.Add
but it works like adding sheet to the beginning of the sheets list
am i doing something wrong?

i would appreciate your help

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
Named parameters aren't supported yet.

expression.Add(Before, After, Count, Type)
Source: Add Method [Excel 2007 Developer Reference]

oWB.Sheets.Add(ComObjMissing(), oWB.Sheets(oWB.Sheets.Count))
Untested. ComObjMissing() indicates the parameter should be entirely omitted.

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
it works
thank you!
wonder how did i miss this in the help...

rani
  • Members
  • 217 posts
  • Last active: Jul 21 2016 12:53 PM
  • Joined: 18 Mar 2008
get SAP GUI controls

I tried with AHK_L (under winXP) to get SAP GUI controls,
with the mouse over coordiantes, but AHK_L could 'see' any control
means:
could not see the Control ID and could not see it's TEXT value

is there any tool in AHK that I can override this problem ?

I saw in internet, that with autoit, in some way, it can be achieved.
so why not with AHK_L ?

JAVA Forms
same problem I got with JAVA Forms( in winXP), that does not see any control, do I need another tool for that ?

any help will be appreciated

rgrds
rani

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
hey, guys
how can i pass an array as parameter if function needs it?
for example here is a code:
oExcel := ComObjCreate("Excel.Application")      ; create Excel Application object
oWB := oExcel.Workbooks.Add   ; create a new workbook
oExcel.Visible := 1
sFileName := "C:\SomeDelimetedFile.txt"
oSheet := oWB.Sheets[1]
oQT := oSheet.QueryTables.Add("TEXT;" . sFileName,oSheet.Range("$A$1"))
[color=red]oQT.TextFileColumnDataTypes := Array(1)[/color]
;http://msdn.microsoft.com/en-us/library/aa215749%28v=office.11%29.aspx
it says "incorrect parameter" when i try to do it as in example

a4u
  • Guests
  • Last active:
  • Joined: --
Since your array only has one element, you might be able to just use the number:
oQT.TextFileColumnDataTypes := 1
... otherwise, you would create a Com Ojbect Array (SafeArray):
Array := ComObjArray(3,1)
Array[0] := 1
oQT.TextFileColumnDataTypes := Array


sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
A post with examples of how to create a SafeArray is located here in this thread.

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
thanks, guys!

amnesiac
  • Members
  • 124 posts
  • Last active: May 01 2014 03:04 AM
  • Joined: 07 Nov 2010

[*:3e7udvi6]COM Object: CAPICOM Series
[*:3e7udvi6]Purpose: digitally sign data, sign code, verify digital signatures, envelop data for privacy, hash data, encrypt/decrypt data and more.
[*:3e7udvi6]System Requirements: 32-bit OS, for more details, please see MSDN
[*:3e7udvi6]Documentation Link: CAPICOM Reference
[*:3e7udvi6]Other Links:Platform SDK Redistributable: CAPICOM
[*:3e7udvi6]Basic Code Example: First require to install capicom.dll from microsoft's site above.
/*
************************************************************

 CAPIEncrypt.ahk

 This is a sample script to illustrate how to use the CAPICOMs EncryptedData 
 to encrypt/decrypt text file.

 Note: For simplicity, this script does not handle exception.

************************************************************
*/

ForReading := 1
ForWriting := 2

; CAPICOM's constants.                                             
CAPICOM_ENCRYPTION_ALGORITHM_RC2       := 0
CAPICOM_ENCRYPTION_ALGORITHM_RC4       := 1
CAPICOM_ENCRYPTION_ALGORITHM_DES       := 2
CAPICOM_ENCRYPTION_ALGORITHM_3DES      := 3
CAPICOM_ENCRYPTION_ALGORITHM_AES       := 4
        
CAPICOM_ENCRYPTION_KEY_LENGTH_MAXIMUM  := 0
CAPICOM_ENCRYPTION_KEY_LENGTH_40_BITS  := 1
CAPICOM_ENCRYPTION_KEY_LENGTH_56_BITS  := 2
CAPICOM_ENCRYPTION_KEY_LENGTH_128_BITS := 3
CAPICOM_ENCRYPTION_KEY_LENGTH_192_BITS := 4
CAPICOM_ENCRYPTION_KEY_LENGTH_256_BITS := 5

strSF         := "C:\test.txt"
strOF         := "C:\Encrypted.txt"
Algorithm     := CAPICOM_ENCRYPTION_ALGORITHM_AES
KeyLength     := CAPICOM_ENCRYPTION_KEY_LENGTH_56_BITS
Password      := "123456"

EncryptCommand(strSF, strOF, Algorithm, KeyLength, Password)
;~ DecryptCommand(strSF, strOF, Password)
return

; End Main


; EncryptCommand() - Encrypt content of text file ObjectFile.

EncryptCommand(SourceFile, ObjectFile, Algorithm, KeyLength, Password)
{
   Content := ""
   Message := ""
   EncryptedData := ""
   
   ; Create the EncryptedData object.
   EncryptedData := ComObjCreate("CAPICOM.EncryptedData")
   
   ; algorithm, key size, and encryption password.
   EncryptedData.Algorithm.Name := Algorithm
   EncryptedData.Algorithm.KeyLength := KeyLength
   EncryptedData.SetSecret(Password)
   
   ; Display main title.
   MsgBox, % "Encrypting text file " . SourceFile . "."

   ; Load content of text file to be encrypted.
   LoadFile(SourceFile, Content)
   
   ; Now encrypt it.
   EncryptedData.Content := Content
   Message := EncryptedData.Encrypt
   
   ; Finally, save encrypted message to strSF.
   SaveFile(ObjectFile, Message)
   MsgBox, % "Successful - Encrypted message saved to " . ObjectFile . "."

   ; Free resources.
   EncryptedData := ""
   
}

; DecryptCommand() - Decrypt an encrypted file.

DecryptCommand(SourceFile, ObjectFile, Password)
{
   Message := ""
   EncryptedData := ""
   
   ; Create the EncryptedData object.
   EncryptedData := ComObjCreate("CAPICOM.EncryptedData")
   
   ; decryption password.
   EncryptedData.SetSecret(Password)
   
   ; Display main title.
   MsgBox, % "Decrypting encrypted text file " . SourceFile . "."
   
   ; Load the encrypted message.
   LoadFile(SourceFile, Message)
   
   ; Now decrypt it.
   EncryptedData.Decrypt(Message)
      
   ; Finally, save decrypted content to strSF.
   SaveFile(ObjectFile, EncryptedData.Content)
   MsgBox, % "Successful - Decrypted content saved to " . ObjectFile . "."
   
   ; Free resources.
   EncryptedData := ""

}

; LoadFile() - Read content of a text file.

LoadFile(FileName, ByRef Buffer)   
{
	global ForReading
    objFSO := "", objTS := ""
	
    objFSO := ComObjCreate("Scripting.FileSystemObject")

    If Not objFSO.FileExists(FileName)
	{
        MsgBox, % "Error: File " . FileName . " not found."
        ExitApp
    }
   
    objTS := objFSO.OpenTextFile(FileName, ForReading)  
    Buffer := objTS.ReadAll
}

; SaveFile() - Save string to file.

SaveFile(FileName, ByRef Buffer)
{
	global ForWriting
    objFSO := "", objTS := ""
	
    objFSO := ComObjCreate("Scripting.FileSystemObject")
    
    objTS := objFSO.OpenTextFile(FileName, ForWriting, True)
    objTS.Write(Buffer)
    
}

return


silveredge78
  • Members
  • 499 posts
  • Last active: Mar 14 2014 03:19 AM
  • Joined: 25 Jul 2006
I am interested in implementing AHK_L but need to migrate some of my code accordingly.

I use a variant of the first post on ADO COM - Database Query. Can someone convert it to AHK_L?

I also need code to read a file from inside an SQL database (I know how to connect to it and can read from it currently using the code above), and then write the file out - basically an export.

Cany anyone help me?[/url]
SilverEdge78

Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008
I can't test it, as I don't have a database to test on.

sSource  := "SELECT * FROM " . "atlas" ; . " WHERE country LIKE 'A%'"
sConnect := "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" . "C:\Database\atlas.mdb"

prs :=  ComObjCreate("ADODB.Recordset")
prs.Open(sSource, sConnect)
While !prs.EOF
{
   pFields :=  prs.Fields
   Loop, %   pFields.Count
      pField:= pFields.Item(A_Index-1)
   ,   sData .= pField.Name . ": " . pField.Value . "`r`n"
   prs.MoveNext()
}
prs.Close()
MsgBox, % sData
If you get an error, press Control-C in the Msgbox and post it here. Of course if the prs.Open line gives you an error, the first two lines are wrong and I can't help you with that.
aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run