Click here to Skip to main content
15,886,258 members
Articles / Programming Languages / Visual Basic

Managed API Library

Rate me:
Please Sign up or sign in to vote.
4.53/5 (3 votes)
7 Jan 2017CPOL4 min read 6.7K   135   4  
Common API functions translated into easier-to-use functions

Introduction to Managed API Interface

Managed API Interface (or "msw32.dll") is a .NET Dynamic Link Library assembly that contains a variety of managed and unmanaged code functions. The majority of these functions use the Windows API, but are changed into more manageable functions. For example, the API "SHGetFileInfo" is translated into "GetLargeIcon" and "GetSmallIcon".

Background

I found a lot of things unachievable by using managed code, such as getting the small (16x16) icon associated with a file or folder. Typically, a Google search turned up a lot of results, all of them using either registry references and ExtractIcon or the easier method using SHGetFileInfo. I decided to take the most common API calls I use in my applications and translate them into easier-to-use functions.

Please give credit, anyhow, to Cobalt Gaming in your applications, and make sure to send me a link to download (or a free copy :D) of your applications if you use this library in your projects. Thanks in advance, and enjoy! :-)

Download the demo, including the main features!

Download library and demo

msw32 is freeware, but you may not claim it as your own work.

Using the code

The library itself is very easy to use, just the parameter labels should be enough to deduct what the function is asking for. You must add a reference to "msw32.dll" and insert this code at the top of your application:

VB.NET
Imports msw32.ManagedAPI

Documentation

ElevateButton() Function, Within Class "ManagedAPI"

The "ElevateButton()" function is used to apply a UAC shield to any button, standard style or a CommandLinkEx. You should pass a Button control for the hWnd parameter. Use this function when clicking the button will execute an operation that requires administrator priveliges.

ElevateButton(Button1)

ExtractResIcon() Function, Within Class "ManagedAPI"

The "ExtractResIcon()" function is used to extract an icon from a Windows Resource File (typically .exe or .dll). The most common icon resource files are "imageres.dll" and "shell32.dll" both located in the "System32" folder. Pass the resource file's path as the first argument, True for the second argument for a large (32x32) icon, False for the second argument for a small (16x16) icon. The last argument should be the resource index of the icon resource you wish to extract from the resource file.

ExtractResIcon("imageres.dll", True, 0) 'Gets the first icon in imageres.dll

GetLargeIcon() Function, Within Class "ManagedAPI"

The "GetLargeIcon()" function is used to extract the large (32x32) icon associated with a file/folder or drive. Pass the path of the file/folder/drive to extract the icon from as the first parameter.

GetLargeIcon("C:\") 'Gets the icon for drive C, typically a hard disk with a Windows logo

GetShellFileInfo() Function, Within Class "ManagedAPI"

The "GetShellFileInfo()" function is used to retrieve a SHFILEINFO structure from the passed parameter. Pass the path of the file/folder/drive to extract the structure from as the first parameter. This function is not very useful, use the handles on top of SHFILEINFO such as "GetSmallIcon()" and "GetLargeIcon()".

Uses Structure SHFILEINFO

GetShellFileInfo("C:\") 'Returns a SHFILEINFO structure containing data regarding drive C

GetSmallIcon() Function, Within Class "ManagedAPI"

The "GetSmallIcon()" function is used to extract the small (16x16) icon associated with a file/folder or drive. Pass the path of the file/folder/drive to extract the icon from as the first parameter.

GetSmallIcon("C:\") 'Gets the icon for drive C, typically a hard disk with a Windows logo

IconResourceCount() Function, Within Class "ManagedAPI"

The "IconResourceCount()" function is used to return the amount of icon resources within a Windows Resource File. Pass the resource file path as the first argument.

IconResourceCount("shell32.dll") 'Returns the amount of icons within shell32.dll

RegClassDescription() Function, Within Class "ManagedAPI"

The "RegClassDescription()" function is used to return the set description associated with a file type extension. Pass the extension (including the preceding period) as the first argument.

RegClassDescription(".txt") 'Returns the description for .txt, typically Text Document

SetExplorerTheme() Function, Within Class "ManagedAPI"

The "SetExplorerTheme()" function is used to apply the Explorer theme to the controls ListView, TreeView, and Form. It can be used alongside (but preceding) the functions StyleTreeView(), and StyleListView(). Pass either a TreeView or ListView control as the hWnd argument.

An example of all themes applied to a ListView and TreeView

SetExplorerTheme(ListView1) 'Applies Explorer theming to ListView1

StyleTreeView() Function and StyleListView() Function, Within Class "ManagedAPI"

Using Enums ListViewTheme and TreeViewTheme

The "StyleTreeView()" function and the "StyleListView()" function are used to apply additional visual styling to any ListView or TreeView controls. The ListView theme is Semi-Transparent Selection Box and TreeView themes are Hover Node-Expander Fade and Node-Expand Auto-Scroll.

StyleTreeView(TreeView1, TreeViewTheme.Fade) 'Applies the Fade theme to TreeView1

SetHint() Function, Within Class "ManagedAPI"

The "SetHint()" function is used to apply a hint to a TextBox control. Pass the TextBox as the hWnd and a string as the hint.

SetHint(TextBox1, "This is a hint") 'Applies a hint to TextBox1

SetProgressState() Function, Within Class "ManagedAPI"

Using Enum ProgressBarState

The "SetProgressState()" function is used to set the state of a ProgressBar. States include Paused (Yellow), Error (Red) and Normal (Green).

SetProgressState(ProgressBar1, ProgressBarState.Paused) 'Sets ProgressBar1's state to Pause

Class CommandLinkEx Inherits Button, Within Namespace msw32

Command links are a special type of button. Use CommandLinkEx to place a command link button on your form. The Text property can change the big/main text, and the Misc.Note property will change the small bottom text. These CommandLinkEx controls will change according to the form's BackColor property, and can be elevated using the ElevateButton() function.

The Windows API Functions (SendMessage, SetWindowTheme, SHGetFileInfo, DestroyIcon, ExtractIconEx) are also incuded in this library. Class "SHFILEINFO_REF" and it's contents serve purpose only to other functions within this library and should be disregarded and not used.

History

2017-01-05 Created assembly with basic functions, 2017-01-06 Added CommandLinkEx and SHGetFileInfo functions. Also removed SendUDP() (Both functions), 2017-01-07 Created CodeProject article and documented all functions, enums and classes inside of this library.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --