GetForegroundWindow() in Vista??


  • ok, i've been struggling with this for a while now, basically my program is as follows, it will run off a windows hotkey i have then it is suposed to recognize what window is open, it will send a CTRL + C command to that window which will in turn copy whatever text is selected in the active window, it thens pulls that information from the clipboard and stores it into a variable and sends it to a search query address that i have.  this program works like a dream on Win XP but i've tried it on 2 vista machines and it doesnt work, and i've tried it on 2 xp machines no problems at all.  I guess my question is, is there a difference in selecting the active window in Vista? because it doesnt seem to be sending the CTRL + C to the currently active window.  Any help or suggestions would be great, thank you.  my code is as follows.



    Imports System.IO


    Module CopyActiveWindow2

    Const KEYEVENTF_KEYUP As UInt32 = 2

    Const VK_CONTROL As Byte = &H11 ' 0x11h

    Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" ( _

    ByVal hWnd As IntPtr) As Boolean

    Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As IntPtr

    Declare Sub SendKeyboardEvent Lib "user32" Alias "keybd_event" ( _

    ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As UInt32, ByVal dwExtraInfo As UInt32)

    Sub main()


    Dim reader As System.IO.StreamReader

    reader = My.Computer.FileSystem.OpenTextFileReader("prefix file location for search")

    Dim mapUrl, board As String

    board = My.Computer.Clipboard.GetText()

    dest = reader.ReadLine()

    mapUrl = dest & "search url"

    Dim process As New System.Diagnostics.Process

    process.StartInfo.UseShellExecute = True

    process.StartInfo.FileName = mapUrl







    End Sub

    Public Sub SendCtrlC(ByVal hWnd As IntPtr)



    SendKeyboardEvent(VK_CONTROL, 0, 0, 0)

    SendKeyboardEvent(&H43, 0, 0, 0) ' Send the C key (0x43h is "C")

    SendKeyboardEvent(&H43, 0, KEYEVENTF_KEYUP, 0)

    SendKeyboardEvent(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0) ' Left Control Up

    End Sub

    End Module

    Friday, January 25, 2008 5:41 PM

All replies

  • Try to run your program as admin
    Saturday, August 06, 2011 12:23 AM
  • Have a  look here

    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 Express Be a good forum member - mark posts that contain the answers to your questions or those that are helpful c# in vb Translator: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Tuesday, August 09, 2011 6:38 AM