Page 1 of 2 12 LastLast
Results 1 to 40 of 61

Thread: My Windows API Library - New version available for download

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    My Windows API Library - New version available for download

    EDIT (29/07/2010) : The latest version of this library is available here: http://cjwdev.wordpress.com/2010/07/...2-1-available/

    I've been doing a fair amount of work with Windows APIs recently for one reason or another and every time I get one working I add it to a class library that I started months ago and add a managed .NET method that 'wraps' the API and makes it more .NET friendly and easier to reuse. Now that I'm starting to build up a bit of a collection, I thought it would be good to share this class library so that other people can use my managed .NET wrapper methods rather than having to mess about with the Windows APIs directly (I know a lot of people are not keen on doing that).

    So in summary: The basic idea is that a developer could add a reference to my DLL and then they would not have to use a lot of Windows APIs directly, they could just call my .NET methods that do all the API work for them. For example one of my wrapper methods named GetTopLevelWindows combines 6 different Windows APIs to return a list of all visible windows with their handle, title bar text, class name, owning process and window icon.


    So I'm just looking for some suggestions from people on any particular APIs that they would like to see included in this library. Anything that is a bit of a pain to use and would benefit from a managed version really

    Here is a list of what I've got so far: (EDIT: Updated 29/07/2010)

    Managed Methods
    RefreshDevices - Causes the OS to refresh its list of connected devices, equivalent to doing "scan for hardware changes" in device manager
    GetFreeDiskSpace - Returns the amount of space free on a remote disk, specified via UNC path
    GetParentProcess - returns a Process object for the process that started the specified process (aka the parent process)
    MapNetworkDrive - creates a network drive mapped to the specified UNC path
    RemoveNetworkDrive - deletes an existing mapped network drive
    GetUncPathFromNetworkDrive - Returns the UNC path that the specified network drive is mapped to
    DoesNativeMethodExist - Determines whether or not a specified method exists in an unmanaged DLL
    ShareExistingFolder - shares a folder and assigns the specified share permissions
    Is64BitOperatingSystem (property) - Returns true if the OS is 64 bit, even if the process calling this property is running in 32 bit emulation mode.
    GetAllIcons - Returns a List(Of Icon) for all of the icons in the specified dll, exe or ico file
    GetIconAtIndex - Returns the icon at the specified index in the specified dll, exe or ico file
    GetTopLevelWindows - Returns a list of all visible windows with their handle, class name, title bar text, owning process, and window icon
    GetChildWindows - Returns a list of all child windows of a specified window, in the same format as GetTopLevelWindows
    IsWow64Process - determines whether or not the specified process is a 32 bit process running on a 64 bit OS
    ReleaseIpAddresses - releases all IPv4 addresses that were acquired via DHCP
    RenewIpAddresses - renews the DHCP lease for all IPv4 addresses that were acquired via DHCP
    JoinToDomain - Joins the computer to the specified domain
    RemoveFromDomain - Removes the computer from the domain it is joined to (can also be run against a remote computer)
    GetStringResource - gets a string from a DLL resource address (e.g @%Systemroot%\system32\wbem\wmisvc.dll,-203)
    ReadMemory - reads a specified amount of bytes from another process's virtual memory
    GetCommandLineArgs - gets the command line arguments that were passed to any currently running process when it was started (even if it was not started by your program)
    GetServices - gets a list of all services that are in the specified state (running, stopped etc) with their process IDs
    GetServicesFromProcess - pass in a process and get back a list of any services that are running in that process
    FlashWindow - Causes the specified window to flash to get the user's attention
    SetWindowState - Make a specified window hide, minimize, maximize, etc
    InstallService - Creates a Windows Service with the specified parameters on the local machine or a remote machine
    DeleteService - Removes the specified Windows Service from the local machine or a remote machine
    EmptyRecycleBin - Empties the recycle bin
    GetRecycleBinItemcount - Returns the number of items in the recycle bin
    GetRecycleBinSize - Gets the total size of the items in the recycle bin
    RenameComputer - Renames the local computer and corresponding domain account
    GetWindowFromTitle - Gets a NativeWindow instance for the window with the specified title (NativeWindow instance includes title, owning process, icon, handle etc)
    GetWindowsFromPartialTitle - Same as GetWindowFromTitle but finds any windows with the specified string anywhere in their title
    GetWindowSize - Gets the size in pixels of any window
    GetActiveWindow - Gets the currently active window (aka foreground window)
    MakeWindowTopMost - Makes the specified window a topmost window (i.e it stays at the foreground even when not in focus, like task manager)
    MakeWindowNotTopMost - Stops a window from being a topmost window if it currently is one
    SetParentWindow - Makes the specified window a child of another window
    DeleteUserProfile - Removes the specified user's windows profile and settings

    Native APIs
    GetFreeDiskSpaceEx
    WNetCancelConnection
    WNetAddConnection2
    NetShareAdd
    InitializeSecurityDescriptor
    SetEntriesInAcl
    SetSecurityDescriptorDacl
    IsValidSecurityDescriptor
    SystemParametersInfo
    NetShareAdd
    WNetGetConnection
    GetProfileType
    GetForegroundWindow
    ShowWindow
    RegisterHotKey
    UnregisterHotKey
    CM_Reenumerate_DevNode
    NtQueryInformationProcess
    ExtractIconEx
    DestroyIcon
    DoesWin32MethodExist
    IsWow64Process
    GetModuleHandle
    GetProcAddress
    EnumWindows
    EnumChildWindows
    GetWindowTextLength
    GetWindowText
    IsWindowVisible
    GetWindowThreadProcessID
    GetClassName
    SetParent
    SendMessage
    GetWindowLong
    GetClassLong
    IpReleaseAddress
    IpRenewAddress
    GetInterfaceInfo
    NetJoinDomain
    NetUnjoinDomain
    LoadLibrary
    FreeLibrary
    LoadString
    CloseHandle
    OpenProcess
    ReadProcessMemory
    CreateProcess
    CreatePipe
    PeekNamedPipe
    WaitForMultipleObjects
    ReadFile
    WriteFile
    DuplicateHandle
    FlashWindowEx
    OpenSCManager
    EnumServicesStatusEx
    CloseServiceHandle
    CreateService
    DeleteService
    OpenService
    StartService
    SetWindowPos
    GetWindowRect
    DeleteProfile
    SetComputerNameEx
    NetRenameMachineInDomain
    SHQueryRecycleBin
    SHEmptyRecycleBin
    GetProfilesDirectory
    EnumPwrSchemes
    SetActivePwrScheme
    GetActivePwrScheme


    So yeah... any suggestions that you would like me to add?

    Cheers
    Chris
    Last edited by chris128; Jul 29th, 2010 at 01:38 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: My Windows API Pack

    BitBlt, GetDC, ReleaseDC, FindWindow(Ex), SetWindowPos, SetParent. EnumWindows and EnumChildWindows especially because I can't get those two to work without an exception.

  3. #3

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Yeah I had some fun with EnumWindows last night... kept getting memory access violations, so I'll try and get that working tonight.

    As for SetWindowPos, FindWindow, SetParent - I kind of didnt want to do those because they are relatively simple APIs that I wouldn't really be able to simplify much (if at all) by making a managed wrapper method and there are loads of examples on the internet of how to do them already. I guess I could just include them in the Native API section so people don't have to dig out the definitions and declare them themselves.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Just looked at 2 of the other APIs you mentioned Minitech, GetDC and ReleaseDC, and I'm not sure how I could make those any simpler either. GetDC just accept a handle to whatever window you want to draw on right? I guess I could make a managed method that avoids the developer having to get the handle to the window - i.e they just pass in the name of a process or the title of a window and the method takes care of finding the handle for it and getting the DC. Is that the kind of thing you were thinking of or did you just mean I should include the definitions for those APIs in the NativeAPI section for people to use directly themselves?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: My Windows API Pack

    I was thinking along the lines of "pass in a Control or Process object", and automatically call ReleaseDC() in Finalize().

  6. #6

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Oh right, I was assuming you wanted to call it against external programs but I'm guessing by you saying pass in a Control that you must be referring to the .NET app executing the function? I've not got much (well any) experience with GDI stuff so not sure how well I would be able to design such a function but I dont mind having a go.

    Oh and I've got my EnumWindows wrapper method working fine now, although making it a standard synchronous function that just returns a List was fun... because the API uses a callback function to return the results that it calls on another thread, once for every window. Might post a thread about that in a minute actually as I'm not entirely happy with my 'solution'. EDIT: I've since found that this is not the case and the EnumWindows API is synchronous not asynchronous
    Last edited by chris128; Jun 3rd, 2010 at 02:06 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: My Windows API Pack

    Yeah, having the wrapper for EnumWindows return a List(Of IntPtr) would be great!

  8. #8

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Well I've made it at the moment so it returns a Dictionary(Of IntPtr, String) - the IntPtr is obviously the handle and the String is the title bar text for that window (which I grab via the GetWindowText API) I was going to just return a list of IntPtr but then realised thats not much use as you have no idea which IntPtr (handle) relates to which window, hence the title bar text being included with each item.
    I've also currently got it so that it only returns visible windows (by using the IsWindowVisible API), as otherwise I get a massive list of windows that I'm guessing most developers would not be interested in. It still brings back the start menu as a window and a couple of other items that you would not normally class as windows but its nothing compared to the list you get when you dont restrict it to visible windows only.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: My Windows API Pack

    Hm, I don't think that's a good idea. But that's just my opinion.

    Like, what if you don't know what the text is? I think you should create your own custom class - WindowCollection - from which you can obtain IntPtrs by title text, part of title text, process name, process ID, etc.

  10. #10

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Yeah I had seriously considered that as well, and still am, but even giving you the window text with the handle is more than you would normally get with EnumWindows. Not sure how I would get any process details just from the handle to a window but I'll look into it. EDIT: Actually, looks like I can use GetWindowThreadProcessId for that
    Cheers for the suggestions
    Last edited by chris128; Jun 3rd, 2010 at 03:49 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #11

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Updated original post to include some of the new APIs and managed methods that I added last night

    EDIT: I've also now got my EnumWindows wrapper returning a List(Of ManagedWindow) - where ManagedWindow is my own class that has the following properties: Handle (IntPtr), ClassName (String), Title (String), OwningProcess (Process)
    Last edited by chris128; Jun 3rd, 2010 at 08:03 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  12. #12
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: My Windows API Pack

    How did you get EnumWindows to do that?

    ... genius.

  13. #13

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Its not the actual API that is returning a List(Of ManagedWindow), its my function that calls the API - but I'm guessing you know that...
    I can post the code if you want but its a little long and I'm not 100% happy with the way it works yet - which is what this thread is about: http://www.vbforums.com/showthread.php?t=616959
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  14. #14

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Well now that I have got that issue sorted out (was basically just me misunderstanding how the API worked and trying to get around a problem that didnt exist ), here is the code for my EnumWindows wrapper (creatively named GetWindows) if you are interested:


    I've not bothered including the API definitions here but you can tell which methods are API calls as they are preceded by NativeAPI.
    vb.net Code:
    1. Public Shared Function GetWindows() As List(Of ManagedWindow)
    2.         Dim WindowList As New List(Of ManagedWindow)
    3.         Dim ListHandle As GCHandle = GCHandle.Alloc(WindowList)
    4.         Try
    5.             NativeAPI.EnumWindows(New NativeAPI.EnumWindowsProc(AddressOf CallBack), GCHandle.ToIntPtr(ListHandle))
    6.         Finally
    7.             ListHandle.Free()
    8.         End Try
    9.         Return WindowList
    10. End Function
    11.  
    12. Private Shared Function CallBack(ByVal handle As IntPtr, ByVal lParam As IntPtr) As Boolean
    13.         If NativeAPI.IsWindowVisible(handle) Then
    14.             Dim TitleBuilder As New System.Text.StringBuilder(NativeAPI.GetWindowTextLength(handle) + 1)
    15.             Dim WndList As List(Of ManagedWindow) = DirectCast(GCHandle.FromIntPtr(lParam).Target, List(Of ManagedWindow))
    16.             NativeAPI.GetWindowText(handle, TitleBuilder, TitleBuilder.Capacity)
    17.             Dim ProcessID As Integer = -1
    18.             NativeAPI.GetWindowThreadProcessId(handle, ProcessID)
    19.             Dim ClassNameBuilder As New System.Text.StringBuilder(255)
    20.             NativeAPI.GetClassName(handle, ClassNameBuilder, ClassNameBuilder.Capacity)
    21.             WndList.Add(New ManagedWindow(handle, TitleBuilder.ToString, Process.GetProcessById(ProcessID), ClassNameBuilder.ToString))
    22.         End If
    23.         Return True
    24. End Function

    and here's my basic ManagedWindow class:

    vb.net Code:
    1. Public Class ManagedWindow
    2.  
    3.         Private _Title As String = String.Empty
    4.         Public Property Title() As String
    5.             Get
    6.                 Return _Title
    7.             End Get
    8.             Set(ByVal value As String)
    9.                 _Title = value
    10.             End Set
    11.         End Property
    12.  
    13.         Private _Handle As IntPtr
    14.         Public Property Handle() As IntPtr
    15.             Get
    16.                 Return _Handle
    17.             End Get
    18.             Set(ByVal value As IntPtr)
    19.                 _Handle = value
    20.             End Set
    21.         End Property
    22.  
    23.         Private _OwningProcess As Process
    24.         Public Property OwningProcess() As Process
    25.             Get
    26.                 Return _OwningProcess
    27.             End Get
    28.             Set(ByVal value As Process)
    29.                 _OwningProcess = value
    30.             End Set
    31.         End Property
    32.  
    33.         Private _ClassName As String = String.Empty
    34.         Public Property ClassName() As String
    35.             Get
    36.                 Return _ClassName
    37.             End Get
    38.             Set(ByVal value As String)
    39.                 _ClassName = value
    40.             End Set
    41.         End Property
    42.  
    43.         Public Sub New()
    44.         End Sub
    45.  
    46.         Public Sub New(ByVal Hwnd As IntPtr, ByVal TitleText As String, ByVal Owner As Process, ByVal nativeclassname As String)
    47.             _Handle = Hwnd
    48.             _Title = TitleText
    49.             _OwningProcess = Owner
    50.             _ClassName = nativeclassname
    51.         End Sub
    52.  
    53.         Public Overrides Function ToString() As String
    54.             If OwningProcess Is Nothing Then
    55.                 Return Handle.ToString & " --- " & ClassName & " --- " & Title
    56.             Else
    57.                 Return Handle.ToString & " ---- " & ClassName & " --- " & Title & " ---- " & OwningProcess.ProcessName
    58.             End If
    59.         End Function
    60.  
    61. End Class

    I'll be adding comments to everything before I make the full project available but hopefully you can tell what that code is doing without them
    Last edited by chris128; Jun 3rd, 2010 at 02:10 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  15. #15
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: My Windows API Pack

    Whoa, I thought it was async too! Well, at least you made it a bit more obvious there.

  16. #16

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Yeah well the idea is that someone would just call that GetWindows method instead of having to do all the work that it does every time they wanted to enumerate windows or write their own helper/wrapper method.
    e.g:
    Code:
    For Each Window As ManagedWindow In GetWindows()
                MessageBox.Show("Handle: " & Window.Handle.ToString & vbNewLine & _
                                "Process: " & Window.OwningProcess.ProcessName & vbNewLine & _
                                "Class: " & Window.ClassName & vbNewLine & _
                                "Title: " & Window.Title)
    Next
    I've got EnumChildWindows working in the same way as well now - returning a List(Of ManagedWindow) with all of the relevant info in

    I've included SetParent in the NativeAPI section as well now (cant really think of any way to simplify it with a managed version though) and I'll add FindWindow as you mentioned it earlier - anything else?
    Last edited by chris128; Jun 4th, 2010 at 06:02 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  17. #17

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Updated list of APIs and wrapper methods in original post now to include some new ones that I've added today for releasing/renewing IP addresses and joining a computer to a domain
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  18. #18

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Quote Originally Posted by minitech View Post
    BitBlt, GetDC, ReleaseDC, FindWindow(Ex), SetWindowPos, SetParent. EnumWindows and EnumChildWindows especially because I can't get those two to work without an exception.
    I just started looking at GetDC and ReleaseDC and although I got them to work and created a Drawing.Graphics object from the DC they retrieved, I found you can do the exact same thing without using APIs by just using Drawing.Graphics.FromHwnd
    Or were you using them for some other purpose? I'm just wondering if its worth including those two APIs or not.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  19. #19
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: My Windows API Pack

    ... really? Learned something new Guess those aren't necessary.
    How about Read/WriteProcessMemory? I'm having trouble with those ones It would be great to provide some good and easy-to-follow documentation on them.

  20. #20

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    I was going to but I couldn't think of a legit purpose for using them so one problem is that I wouldn't really be able to test it and the second problem is that I dont want to be helping people make malicious software.

    Oh and also, I'm just adding FindWindow - do you think its worth making a wrapper method that just lets you specify the window title rather than having to specify the class name as well to make it simpler for people who are not used to using the APIs? Also perhaps an overloaded version that lets you specify the process name (and then obviously it brings back a list of all windows owned by that process, rather than a single window)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  21. #21
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: My Windows API Pack

    I just thought it seemed interesting. I don't have VS Pro, so I can't use the Memory Viewing window, and I wanted to see what memory actually looked like. That's not malicious, right?
    But I agree, there are too many people who would take that and make a game bot or something. I guess you're right.

  22. #22

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Well if you are just curious, you can read your own program's memory without using any Windows APIs, just use Runtime.InteropServices.Marshal.ReadByte. All memory looks like though is a series of Bytes - you have to know what each byte corresponds to in that given chunk of memory to be able to make any sense of it. So you could see the same type of thing that you would see when you read memory by simply declaring a Byte array and doing System.Text.Encoding.ASCII.GetBytes - just a series of numbers between 0 and 255 basically, nothing exciting I'm afraid I believe with ReadProcessMemory you just end up with a byte array too, so without documentation of how a particular program stores its in memory data I'm not sure how people actually make any use of it... perhaps just a lot of trial and error but it seems unlikely.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  23. #23
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: My Windows API Pack

    No, I would pass it to System.Text.Encoding.ASCII.GetString, so I could pick out strings in my program's memory. Just for fun. But thanks for the tip!!! I would give you rep, but I can't right now.

  24. #24

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Updated original post with several new native and managed APIs that I've added over the last few days

    There's just a few more that I want to get working before I release this but if anyone has any suggestions for additional APIs they would like to see in this then I'll be happy to add them if I can get them working
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  25. #25
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: My Windows API Pack

    That's quite a list. Maybe you should consider grouping them? So like:
    Code:
    Namespace API
         Public Module Window
              ' ...
         End Module
         Public Module Icons
              ' ...
         End Module
         Public Module Process
              ' ...
         End Module
         ' etc.
    End Namespace

  26. #26

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Yeah I have well with classes rather than modules but yeah I thought to keep this thread simple I'd just leave them listed as they were before
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  27. #27

    Re: My Windows API Pack

    Perhaps FlashWindowEx()? And maybe the simpler FlashWindow?

    I could write the handler for FlashWindow() for you, but FlashWindowEx() would drive me mad.

  28. #28

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Ah yeah good call, I'll get FlashWindowEx added in there with a managed wrapper for it (compared to the last couple I've been working on it should be quite straight forward )
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  29. #29

    Re: My Windows API Pack

    I just looked at FlashWindowEx()....I could actually do that one! I feel smart now!

    But if you're doing it, I won't bother.

    Perhaps you should get on MSN *hint hint*...

  30. #30

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Too late I've already done it My managed method can be used like this:

    vb Code:
    1. FlashWindow(Me.Handle, FlashItemOption.FlashTitleAndTaskbar, FlashDurationOption.FlashUntilFlashStopSet)
    and I've made another named StopFlashWindow (bet you cant guess what that does) :
    vb Code:
    1. StopFlashWindow(Me.Handle)

    I figured having 2 separate parameters for the FlashWindow method, one for the object to flash (title bar, taskbar or both) and one for the duration of the flash (until StopFlashWindow is called or just until the window gets focus) would be easier for people who aren't familiar with bitwise addition

    Here's my definitions (with comments) if anyone is interested:

    vb Code:
    1. <StructLayoutAttribute(LayoutKind.Sequential)> _
    2.    Public Structure FLASHWINFO
    3.         ''' <summary>
    4.         ''' The size of this structure
    5.         ''' </summary>
    6.         Public cbSize As UInteger
    7.         ''' <summary>
    8.         ''' A handle to the window to flash
    9.         ''' </summary>
    10.         ''' <remarks></remarks>
    11.         Public hwnd As System.IntPtr
    12.         ''' <summary>
    13.         ''' The flash parameters that specify what to flash and for how long
    14.         ''' </summary>
    15.         Public dwFlags As UInteger
    16.         ''' <summary>
    17.         ''' The number of times to flash the window. 0 = infinite
    18.         ''' </summary>
    19.         Public uCount As UInteger
    20.         ''' <summary>
    21.         ''' The length of time between flashes. 0 = OS default
    22.         ''' </summary>
    23.         Public dwTimeout As UInteger
    24.     End Structure
    25.  
    26.     ''' <summary>
    27.     ''' Flashes a window to alert the user that it requires attention
    28.     ''' </summary>
    29.     ''' <param name="pfwi">A FLASHWINFO structure that specifies parameters for the flash</param>
    30.     <DllImportAttribute("user32.dll", EntryPoint:="FlashWindowEx")> _
    31.     Public Shared Function FlashWindowEx(<InAttribute()> ByVal pfwi As FLASHWINFO) As <MarshalAsAttribute(UnmanagedType.Bool)> Boolean
    32.     End Function

    and my managed methods / enums :
    vb Code:
    1. ''' <summary>
    2.     ''' Flashes a window to alert a user that it requires attention
    3.     ''' </summary>
    4.     ''' <param name="Handle">A handle to the window to flash</param>
    5.     ''' <param name="Item">The part of the window to flash - title bar, taskbar, or both</param>
    6.     ''' <param name="Duration">Determines when the flashing stops</param>
    7. Public Shared Sub FlashWindow(ByVal Handle As IntPtr, ByVal Item As FlashItemOption, ByVal Duration As FlashDurationOption)
    8.         Dim FlashInfo As New FLASHWINFO
    9.         FlashInfo.hwnd = Handle
    10.         FlashInfo.dwFlags = Item Or Duration
    11.         FlashInfo.cbSize = CUInt(Marshal.SizeOf(FlashInfo))
    12.         FlashWindowEx(FlashInfo)
    13. End Sub
    14.  
    15.     ''' <summary>
    16.     ''' Stops a window from flashing
    17.     ''' </summary>
    18.     ''' <param name="Handle">A handle to the window to stop flashing</param>
    19. Public Shared Sub StopFlashWindow(ByVal Handle As IntPtr)
    20.         Dim FlashInfo As New FLASHWINFO
    21.         FlashInfo.hwnd = Handle
    22.         FlashInfo.dwFlags = 0 'FLASHW_STOP
    23.         FlashInfo.cbSize = CUInt(Marshal.SizeOf(FlashInfo))
    24.         FlashWindowEx(FlashInfo)
    25. End Sub
    26.  
    27. Public Enum FlashItemOption As UInteger
    28.         ''' <summary>
    29.         ''' Only flashes the window's title bar and border, not the window's item/icon on the taskbar
    30.         ''' </summary>
    31.         FlashTitleBar = 1
    32.         ''' <summary>
    33.         ''' Only flashes the window's item/icon on the taskbar
    34.         ''' </summary>
    35.         FlashTaskbar = 2
    36.         ''' <summary>
    37.         ''' Flashes both the item on the taskbar and the window itself.
    38.         ''' Equivelant to using both FlashTitleBar and FlashTaskbar
    39.         ''' </summary>
    40.         FlashTitleAndTaskbar = 3
    41. End Enum
    42.  
    43. Public Enum FlashDurationOption As UInteger
    44.         ''' <summary>
    45.         ''' Flash until StopFlashWindow is called
    46.         ''' </summary>
    47.         FlashUntilFlashStopSet = 4
    48.         ''' <summary>
    49.         ''' Flash until the window has got focus
    50.         ''' </summary>
    51.         FlashUntilWindowActivated = &HC
    52. End Enum

    Oh and yeah I'll go on MSN in a minute
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  31. #31

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Added some new APIs and managed methods today for installing and uninstalling windows services on the local computer or a remote computer
    Once I've done a couple of other windows service related APIs, I'm getting quite close to the end of the ones I can think of to add. Got a couple of random ones to do like one that gets information about the page file(s) and one that turns high contrast mode on, but after that I'm out of ideas really so if anyone has any other suggestions let me know!
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  32. #32

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Haven't had much chance over the last week to work on this so I think I'll just tidy some of it up and finish the methods I've already got then release it for others to use as I've had a couple of people asking for it already. I'll stick a link here when it is done
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  33. #33

    Re: My Windows API Pack

    Just so you know, I'm unable to get FlashWindowEx() to work or the wrapper. It constantly returns false >.<, I don't know why.

  34. #34

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    That's odd as it works fine for me - do you mean the code I posted in this thread or the source code I sent you for the API pack?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  35. #35

  36. #36

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Ah I think I've found the issue - it only seems to work correctly on my machine when I set the program to compile to x64, though I could have sworn I tested it on x86 as I normally do... but I guess I cant have. From a quick glance at the code I cant see why that would be the case but I'll look into it and get it sorted, thanks for letting me know!
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  37. #37

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Try this declaration of the API instead -the only difference is that it is using ByRef instead of ByVal and that seems to have got it working on my machine so that it works in both x86 and x64 mode.

    vb Code:
    1. ''' <summary>
    2.     ''' Flashes a window to alert the user that it requires attention
    3.     ''' </summary>
    4.     ''' <param name="pfwi">A FLASHWINFO structure that specifies parameters for the flash</param>
    5.     <DllImportAttribute("user32.dll", EntryPoint:="FlashWindowEx")> _
    6.     Public Shared Function FlashWindowEx(<InAttribute()> ByRef pfwi As FLASHWINFO) As <MarshalAsAttribute(UnmanagedType.Bool)> Boolean
    7.     End Function
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  38. #38

    Re: My Windows API Pack

    Perfect. That seemed to work just fine Thanks!

  39. #39
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: My Windows API Pack

    Just one small point to note: when applying attributes, you can leave the 'Attribute' part out of the type name, e.g.
    vb.net Code:
    1. ''' <summary>
    2. ''' Flashes a window to alert the user that it requires attention
    3. ''' </summary>
    4. ''' <param name="pfwi">A FLASHWINFO structure that specifies parameters for the flash</param>
    5. <DllImport("user32.dll", EntryPoint:="FlashWindowEx")> _
    6. Public Shared Function FlashWindowEx(<In()> ByRef pfwi As FLASHWINFO) As <MarshalAs(UnmanagedType.Bool)> Boolean
    7. End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  40. #40

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Windows API Pack

    Yeah I was aware of that thanks, I just got into the habit of putting it in because that's how the API viewer that Microsoft released writes it and I used to use that a lot. I never normally include it for DllImport though so no idea why I did in that example...
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width