2 people like it.

Get url opened by Google Chrome's main window

This is a small example of invoking Win32 API. It gets the url which is opened by Google chrome's main window.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
open System

open System.Runtime.InteropServices
module Win32 =
  [<DllImport "user32.dll">] 
  extern IntPtr FindWindow(string lpClassName,string lpWindowName)
  [<DllImport "user32.dll">] 
  extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName , string windowTitle)
  [<DllImport("user32.dll", CharSet=CharSet.Auto)>]
  extern IntPtr SendMessage(IntPtr hWnd, uint32 Msg, IntPtr wParam , IntPtr lParam)


let getChromeMainWindowUrl () =
  let ps = Diagnostics.Process.GetProcessesByName "chrome"
  seq { 
  for p in ps do
    let mainWnd = Win32.FindWindow("Chrome_WidgetWin_1", p.MainWindowTitle)
    let addrBar = Win32.FindWindowEx(mainWnd, 0n, "Chrome_OmniboxView", null)
    if addrBar <> 0n then
      let url = Marshal.AllocHGlobal 100
      let WM_GETTEXT = 0x000Du
      Win32.SendMessage (addrBar, WM_GETTEXT, 50n, url) |> ignore
      let url = Marshal.PtrToStringUni url
      yield url } 
  |> Seq.head 
namespace System
namespace System.Runtime
namespace System.Runtime.InteropServices
Multiple items
type DllImportAttribute =
  inherit Attribute
  new : dllName:string -> DllImportAttribute
  val EntryPoint : string
  val CharSet : CharSet
  val SetLastError : bool
  val ExactSpelling : bool
  val PreserveSig : bool
  val CallingConvention : CallingConvention
  val BestFitMapping : bool
  val ThrowOnUnmappableChar : bool
  member Value : string

Full name: System.Runtime.InteropServices.DllImportAttribute

--------------------
DllImportAttribute(dllName: string) : unit
Multiple items
type IntPtr =
  struct
    new : value:int -> nativeint + 2 overloads
    member Equals : obj:obj -> bool
    member GetHashCode : unit -> int
    member ToInt32 : unit -> int
    member ToInt64 : unit -> int64
    member ToPointer : unit -> unit
    member ToString : unit -> string + 1 overload
    static val Zero : nativeint
    static member Add : pointer:nativeint * offset:int -> nativeint
    static member Size : int
    ...
  end

Full name: System.IntPtr

--------------------
IntPtr()
IntPtr(value: int) : unit
IntPtr(value: int64) : unit
IntPtr(value: nativeptr<unit>) : unit
val FindWindow : lpClassName:string * lpWindowName:string -> IntPtr

Full name: Script.Win32.FindWindow
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val lpClassName : string
val lpWindowName : string
val FindWindowEx : parentHandle:IntPtr * childAfter:IntPtr * lclassName:string * windowTitle:string -> IntPtr

Full name: Script.Win32.FindWindowEx
val parentHandle : IntPtr
val childAfter : IntPtr
val lclassName : string
val windowTitle : string
type CharSet =
  | None = 1
  | Ansi = 2
  | Unicode = 3
  | Auto = 4

Full name: System.Runtime.InteropServices.CharSet
field CharSet.Auto = 4
val SendMessage : hWnd:IntPtr * Msg:uint32 * wParam:IntPtr * lParam:IntPtr -> IntPtr

Full name: Script.Win32.SendMessage
val hWnd : IntPtr
Multiple items
val uint32 : value:'T -> uint32 (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.uint32

--------------------
type uint32 = UInt32

Full name: Microsoft.FSharp.Core.uint32
val Msg : uint32
val wParam : IntPtr
val lParam : IntPtr
val getChromeMainWindowUrl : unit -> string

Full name: Script.getChromeMainWindowUrl
val ps : Diagnostics.Process []
namespace System.Diagnostics
Multiple items
type Process =
  inherit Component
  new : unit -> Process
  member BasePriority : int
  member BeginErrorReadLine : unit -> unit
  member BeginOutputReadLine : unit -> unit
  member CancelErrorRead : unit -> unit
  member CancelOutputRead : unit -> unit
  member Close : unit -> unit
  member CloseMainWindow : unit -> bool
  member EnableRaisingEvents : bool with get, set
  member ExitCode : int
  ...

Full name: System.Diagnostics.Process

--------------------
Diagnostics.Process() : unit
Diagnostics.Process.GetProcessesByName(processName: string) : Diagnostics.Process []
Diagnostics.Process.GetProcessesByName(processName: string, machineName: string) : Diagnostics.Process []
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val p : Diagnostics.Process
val mainWnd : IntPtr
module Win32

from Script
property Diagnostics.Process.MainWindowTitle: string
val addrBar : IntPtr
val url : nativeint
type Marshal =
  static val SystemDefaultCharSize : int
  static val SystemMaxDBCSCharSize : int
  static member AddRef : pUnk:nativeint -> int
  static member AllocCoTaskMem : cb:int -> nativeint
  static member AllocHGlobal : cb:nativeint -> nativeint + 1 overload
  static member AreComObjectsAvailableForCleanup : unit -> bool
  static member BindToMoniker : monikerName:string -> obj
  static member ChangeWrapperHandleStrength : otp:obj * fIsWeak:bool -> unit
  static member CleanupUnusedObjectsInCurrentContext : unit -> unit
  static member Copy : source:int[] * startIndex:int * destination:nativeint * length:int -> unit + 15 overloads
  ...

Full name: System.Runtime.InteropServices.Marshal
Marshal.AllocHGlobal(cb: int) : nativeint
Marshal.AllocHGlobal(cb: nativeint) : nativeint
val WM_GETTEXT : uint32
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val url : string
Marshal.PtrToStringUni(ptr: nativeint) : string
Marshal.PtrToStringUni(ptr: nativeint, len: int) : string
module Seq

from Microsoft.FSharp.Collections
val head : source:seq<'T> -> 'T

Full name: Microsoft.FSharp.Collections.Seq.head
Raw view New version

More information

Link:http://fssnip.net/iT
Posted:3 years ago
Author:nagat01
Tags: win32 , web