ComObjConnect() [AHK_L 53+]

COM 객체의 이벤트를 주어진 접두사를 가진 함수에 연결합니다.

ComObjConnect(ComObject , PrefixOrSink)

매개변수

ComObject

이벤트를 일으키는 객체.

객체가 IConnectionPointContainer 인터페이스를 지원하지 않거나 또는 객체의 클래스에 관한 유형정보를 열람할 수 없으면, 에러 메시지가 나타납니다. 이것은 ComObjError() 또는 try/catch으로 억누르거나 처리할 수 있습니다.

[v1.1.22+]: IProvideClassInfo 인터페이스를 객체가 지원하면 그 클래스의 관한 유형정보를 열람하는 데 사용됩니다. 그렇지 않으면, ComObjConnect는 객체의 IDispatch 인터페이스를 통하 유형 정보를 열람하려고 시도하는데, 이것은 신뢰성이 떨어집니다.

PrefixOrSink

A string to prefix to the event name to determine which function to call when an event occurs, or [in v1.1.01+] an event sink object defining a method for each event to be handled.

생략하면, 객체가 "연결이 끊깁니다"; 즉, 스크립트는 더 이상 그의 이벤트 고지를 받지 않습니다.

사용법

효과적으로 ComObjConnect를 사용하려면, 먼저 관심을 둔 이벤트를 처리할 함수를 스크립트에 작성해야 합니다. "이벤트-처리자"라고 하는 그런 함수는 구조가 다음과 같습니다:

PrefixEventName([Params..., ComObject])
{
    ... event-handling code ...
    return ReturnValue
}

Prefix should be the same as the PrefixOrSink parameter if it is a string; otherwise, it should be omitted. EventName should be replaced with the name of whatever event the function should handle.

Params는 이벤트가 가진 매개변수에 상응합니다. 이벤트에 매개변수가 없으면, Params도 완전히 생략되어야 합니다. ComObject는 선택적입니다. 그리고 Params의 개수가 올바르게 정의되어 있을 경우에만 사용해야 합니다; 그 안에 ComObjConnect에 건넸던 원래 포장 객체를 가리키는 참조가 들어 있습니다. "ComObject"는 여러분의 스크립트 문맥에 맞게 보다 의미있는 이름으로 교체되어야 합니다.

이벤트 처리자는 반환 값이 있을 수 있다는 사실에 주목하십시오. COM-종속적 유형의 값을 돌려주려면, ComObject(type, value)를 사용하십시오. 예를 들어, return ComObject(0,0)는 variant 유형의 VT_EMPTY를 돌려줍니다. 이것은 자바스크립트에서 undefined를 돌려주는 것 (또는 아무것도 돌려주지 않는 것)과 동등합니다.

이벤트 처리를 켜려면 ComObjConnect(yourObject, "Prefix")를 호출합니다.

객체와 연결을 끊으려면 (이벤트 처리를 중지하려면) ComObjConnect(yourObject)를 호출합니다.

매개변수 개수를 알지 못하면, 가변 함수를 사용할 수 있습니다.

Event Sink [v1.1.01+]

If PrefixOrSink is an object, whenever an event is raised, the corresponding method of that object is called. Although the object can be constructed dynamically, it is more typical for PrefixOrSink to refer to a class or an instance of a class. In that case, methods are defined as shown above, but without Prefix.

As with any call to a method, the method's (normally hidden) this parameter contains a reference to the object through which the method was called; i.e. the event sink object, not the COM object. This can be used to provide context to the event handlers, or share values between them.

To catch all events without defining a method for each one, define a __Call meta-function.

ComObject retains a reference to PrefixOrSink, so it cannot be deleted before the object is disconnected.

[v1.1.36.02+]: ComObject releases its reference to PrefixOrSink automatically if the COM object releases the connection. For example, Internet Explorer does this when it exits. If the script does not retain its own reference to PrefixOrSink, it can use __Delete to detect when this occurs. If the object is hosted by a remote process and the process terminates unexpectedly, it may take several minutes for the system to release the connection.

논평

The script must retain a reference to ComObject, otherwise it would be freed automatically and would disconnect from its COM object, preventing any further events from being detected. There is no standard way to detect when the connection is no longer required, so the script must disconnect manually by calling ComObjConnect.

The #Persistent directive may be needed to keep the script running while it is listening for events.

On failure, the function may throw an exception, exit the script or simply return, depending on the current ComObjError() setting and other factors.

ComObjCreate(), ComObjGet(), ComObjActive(), ComObjError(), WScript.ConnectObject (MSDN)

예제

Launches an instance of Internet Explorer and connects events to corresponding script functions with the prefix "IE_". For details about the COM object and DocumentComplete event used below, see InternetExplorer object (Microsoft Docs).

ie := ComObjCreate("InternetExplorer.Application")

; 상응하는 스크립트 함수에 접두사를 "IE_"로 하여 이벤트를 연결합니다.
ComObjConnect(ie, "IE_")

ie.Visible := true  ; 이것은 IE7에서 제대로 작동하지 않는다고 알려져 있습니다.
ie.Navigate("https://www.autohotkey.com/")
#Persistent

IE_DocumentComplete(ieEventParam, url, ieFinalParam) {
    global ie
    if (ie != ieEventParam)
        s .= "First parameter is a new wrapper object.`n"
    if (ie == ieFinalParam)
        s .= "Final parameter is the original wrapper object.`n"
    if ((disp1:=ComObjUnwrap(ieEventParam)) == (disp2:=ComObjUnwrap(ieFinalParam)))
        s .= "Both wrapper objects refer to the same IDispatch instance.`n"
    ObjRelease(disp1), ObjRelease(disp2)
    MsgBox % s . "Finished loading " ie.Document.title " @ " url
    ie.Quit()
    ExitApp
}