Kill child process(CefSharp.BrowserSubprocess.exe)

Just starting out? Need help? Post your questions and find answers here.
zikitrake
Enthusiast
Enthusiast
Posts: 738
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain
Contact:

Kill child process(CefSharp.BrowserSubprocess.exe)

Post by zikitrake »

I'm using the @RSBasiclibrary, PB.Ex_WebGadget (https://www.purebasic.fr/english/viewto ... 27&t=72703)

I need to rotate some proxies, but CEF proxy can only be initialized once per process.

So I'm trying to close the library and kill the CefSharp.BrowserSubprocess.exe processes that are in memory so I can restart CEF with a new proxy.

But the process CefSharp.BrowserSubprocess.exe is autorun even though the library is finished.

Is there any way to detach that process and kill it?

I read that RSBasic has been a little off lately, so I ask in general. Thanks in advance!

My code:

Code: Select all

;Version: 1.1.1.0

EnableExplicit

Global PBEx_WebGadget

#PBEx_WebGadgetEx_Version$ = "1.1.1.0"
#PB_Web_Zoom = 100

Prototype WebGadgetEx(ID, X, Y, Width, Height, URL.p-Unicode, ParentWindowID, ErrorOutput)
Prototype SetWebGadgetExText(ID, URL.p-Unicode, ErrorOutput)
Prototype FreeWebGadgetEx(ID, ErrorOutput)
Prototype IsWebGadgetEx(ID, ErrorOutput)
Prototype GetWebGadgetExAttribute(ID, Attribute, ErrorOutput)
Prototype SetWebGadgetExProxy(Proxy.p-Unicode, ErrorOutput)

Procedure Lib_Init()
  
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
    PBEx_WebGadget = OpenLibrary(#PB_Any, "PB.Ex_WebGadget_x86.dll")
  CompilerElseIf #PB_Compiler_Processor = #PB_Processor_x64
    PBEx_WebGadget = OpenLibrary(#PB_Any, "PB.Ex_WebGadget_x64.dll")
  CompilerEndIf
  
  If PBEx_WebGadget
    Global WebGadgetEx.WebGadgetEx = GetFunction(PBEx_WebGadget, "WebGadgetEx")
    Global SetWebGadgetExText.SetWebGadgetExText = GetFunction(PBEx_WebGadget, "SetWebGadgetExText")
    Global FreeWebGadgetEx.FreeWebGadgetEx = GetFunction(PBEx_WebGadget, "FreeWebGadgetEx")
    Global IsWebGadgetEx.IsWebGadgetEx = GetFunction(PBEx_WebGadget, "IsWebGadgetEx")
    Global GetWebGadgetExAttribute.GetWebGadgetExAttribute = GetFunction(PBEx_WebGadget, "GetWebGadgetExAttribute")
    Global SetWebGadgetExProxy.SetWebGadgetExProxy = GetFunction(PBEx_WebGadget, "SetWebGadgetExProxy")    
  EndIf
  
EndProcedure

Procedure RKillProcess(pID)
  Protected pHandle = OpenProcess_(#PROCESS_ALL_ACCESS, #False, pID)
  Protected Result;=TerminateProcess_(pHandle,0)
  Protected Retr = #WAIT_OBJECT_0
             
  While Retr = #WAIT_OBJECT_0
    ; use WaitForSingleObject to make sure it's dead
    Retr = WaitForSingleObject_(pHandle, 100)
    Result = TerminateProcess_(pHandle, 0)
  Wend  

  ProcedureReturn Result
EndProcedure

Procedure SearchPID(Name.s)
  Protected Process.PROCESSENTRY32, Snapshot.i, ProcCount.l, ProcName$
  Process\dwSize = SizeOf(PROCESSENTRY32)
  Snapshot = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, 0)
  If Snapshot
    ProcCount = Process32First_(Snapshot, Process)
    While ProcCount
      ProcName$ = UCase(PeekS(@Process\szExeFile, #MAX_PATH))
      If UCase(Name) = GetFilePart(ProcName$)        
        ProcedureReturn Process\th32ProcessID
      EndIf
      ProcCount = Process32Next_(Snapshot, Process)
    Wend
    CloseHandle_(Snapshot)
  EndIf
  ProcedureReturn 0
EndProcedure

Procedure SearchPIDchild(PIDparent.l, kill.b = #False, onlyThisFile$ = "")
  Protected Process.PROCESSENTRY32, Snapshot.i, ProcCount.l, ProcName$, PIDchild.l
  Process\dwSize = SizeOf(PROCESSENTRY32)
  Protected existingProcess.b
  Repeat
    existingProcess = #False
    Snapshot = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, 0)
    If Snapshot
      ProcCount = Process32First_(Snapshot, Process)
      While ProcCount
        If Process\th32ParentProcessID = PIDparent
          PIDchild = Process\th32ProcessID
          ProcName$ = PeekS(@Process\szExeFile, #MAX_PATH)
          ProcName$ = UCase(GetFilePart(ProcName$))
          Debug GetFilePart(ProcName$)     
          If kill
            If onlyThisFile$ <> "" 
              If UCase(onlyThisFile$) = ProcName$
                ;matalo
                RKillProcess(PIDchild)
                existingProcess = #True
                Delay(500)
              EndIf
            Else
              RKillProcess(PIDchild)
              existingProcess = #True
              Delay(500)
            EndIf
          EndIf        
        EndIf
        ProcCount = Process32Next_(Snapshot, Process)
      Wend
      CloseHandle_(Snapshot)
    EndIf
    
  Until existingProcess = #False
  
  ProcedureReturn 0
EndProcedure

Procedure Kill_CefSharpBrowser()
  ;Kill all CefSharp.BrowserSubprocess.exe process asociated to main .EXE)
  Protected exe$ = GetFilePart(ProgramFilename())
  Protected PID1 = SearchPID(exe$)
  If PID1
    Debug "Pidparent: " + Str(PID1)
    SearchPIDchild(PID1, #True, "CefSharp.BrowserSubprocess.exe")
  EndIf
EndProcedure


Define Output$ = Space(1000000)
Define ErrorOutput$ = Space(128)
Define webGadgetID.l 

If OpenWindow(0, 0, 0, 1200, 768, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ButtonGadget(2, 0, 0, 250, 20, "Init Lib and Create WebGadgetEx", 0)
  ButtonGadget(3, 250, 0, 250, 20, "Close Lib and Remove WebGadgetEx", 0)
  ButtonGadget(4, 500, 0, 100, 20, "End", 0)
  DisableGadget(3, #True)
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 2
            DisableGadget(2, #True)
            Lib_Init()
            SetWebGadgetExProxy("199.195.248.24:8080", @ErrorOutput$);https://free-proxy-list.net/            
            Debug "SetProxy Result: " + Trim(ErrorOutput$)
            webGadgetID = WebGadgetEx(#PB_Any, 0, 20, 800,600, "about:blank", WindowID(0), @ErrorOutput$)            
            DisableGadget(3, #False)
          Case 3
            DisableGadget(3, #True)
            FreeWebGadgetEx(webGadgetID, @ErrorOutput$)
            CloseLibrary(PBEx_WebGadget)
            Kill_CefSharpBrowser()
            DisableGadget(2, #False)
          Case 4
            If IsLibrary(PBEx_WebGadget)
              CloseLibrary(PBEx_WebGadget)
            EndIf        
            End
        EndSelect
    EndSelect    
  ForEver
EndIf
[/size]

Image