|
Resolve an internet host address
|
Total Hit (2762) |
The System.Net.Dns class exposes a few static methods that let you resolve an internet domain name - such as www.vb2themax.com - into a 4-part numeric IP address, also known as dotted-quad notation.
The Resolve static method takes a string and returns an IPHostEntry object; you can learn the actu
....Read More |
Rating
|
|
|
Caveats of the CopyMemory API function
|
Total Hit (4114) |
Here's the correct declaration of the CopyMemory API function, which is so useful whenever you want to move a block of bytes between two memory locations:
«Code LangId=2»
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, _
source As Any, ByVal bytes As Long)
«/Code
....Read More |
Rating
|
|
|
Determine whether an API function is available
|
Total Hit (3465) |
In many cases you may want to call an API function, but you aren't sure whether the Windows version the application is running on supports that particular function.
The easiest way to test whether a function exists and can be called is to replicate through VB code what Windows itself would do whe
....Read More |
Rating
|
|
|
Extract null-delimited strings
|
Total Hit (3632) |
Most API function that return a string require that you pass a buffer where they can place the result as a null-terminated ANSI string (a.k.a. ASCIIZ string). The calling code must then extract the string by taking all the characters up to the first Chr$(0) character, if there is one. For example, t
....Read More |
Rating
|
|
|
Avoid duplicate entries for the same serviced component
|
Total Hit (2618) |
A common mistake when creating a .NET component that runs under Component Services - that is, a class that inherits from ServicedComponents - is that you forget to assign a fixed version and/or GUID to the component being built. In this case, whenever the assembly is modified and recompiled, a new v
....Read More |
Rating
|
|
|
List all COM components on local machine
|
Total Hit (2831) |
The following routine parses the registry and lists all the installed COM components:
«Code LangId=2»
' this code assumes that you have used this Imports statement
' Imports Microsoft.Win32
' Print ProgID, CLSID, and path of all the COM components
' installed on this computer.
Sub DisplayC
....Read More |
Rating
|
|
|
List all running Windows processes
|
Total Hit (2757) |
The System.Diagnostics.Process class exposes the GetProcesses static method, that returns a list of all running processes. It is quite easy to leverage this feature to display all running processes in a ListBox or ComboBox control:
«Code LangId=2»
' This code assume that you've used the followin
....Read More |
Rating
|
|
|
Prevent a second process instance from running
|
Total Hit (2968) |
VB6 developers can use the App.PrevInstance property to check whether there is another instance of the same process already running on the current machine. This property isn't available any longer in VB.NET, so you must retrieve this information by using the properties of the Process object.
The
....Read More |
Rating
|
|
|
Retrieve information about the current user
|
Total Hit (2510) |
You can easily retrieve information about the current user by means of a few properties of the Environment class. The Environment.UserName returns the name of the user; the Environment.UserInteractive property returns True if the user is an interactive user; the UserDomainName returns the user's dom
....Read More |
Rating
|
|
|
Retrieve Windows and System directories
|
Total Hit (3115) |
In VB.NET you don't need to call the GetWindowsDirectory and GetSystemDirectory API functions to retrieve the path the Windows and System directories. Retrieving the System directory is as simple as calling the Environment.SystemDirectory property:
«Code LangId=2»
Dim sysDir As String = Environm
....Read More |
Rating
|
|
|
Retrieving special system paths
|
Total Hit (2643) |
The GetFolderPath method of the Environment class lets you retrieve the path of several important system directories. For example, here's how you determine the path of the Desktop directory for the current user:
«Code LangId=2»
Console.WriteLine(Environment.GetFolderPath _
(Environment.Spec
....Read More |
Rating
|
|
|
Wait for a process to terminate
|
Total Hit (3402) |
You can use the VB.NET Shell command (in the Microsoft.VisualBasic namespace) to run an external process and wait for its termination, but you can get better control if you work directly with the Process class in the System.Diagnostics namespace. You can start a process with the Start static method
....Read More |
Rating
|
|
|
Write a console utility to kill a process
|
Total Hit (2930) |
The System.Diagnostics.Process class exposes two methods that let you kill a process: CloseMainWindow should be used with processes that have a graphical interface, whereas the Kill method should be used for apps without a user interface (or those whose main window is disable and can't process the W
....Read More |
Rating
|
|
|
Write a console utility to list processes
|
Total Hit (4074) |
The Process class provides all you need to create a command-line utility that lists all the processes running on the system and all the related information. This utility is therefore similar to the Windows Task Manager, except you can run it from the command prompt. All processes are sorted alphabet
....Read More |
Rating
|
|
|
Control what happens when an unhandled exception occurs
|
Total Hit (2988) |
By default, when an unhandled exception occurs in a managed application, a dialog box appears that asks you whether you want to debug the application with one of the debuggers that are listed in a listbox. (Notice that Windows Forms applications display a different dialog box.)
The behavior of th
....Read More |
Rating
|
|
|
Clipping the mouse to a rectangle area
|
Total Hit (2106) |
The Clip property of the Cursor is the rectangle within which the mouse cursor is confined, or its value is Nothing if the mouse can move over the entire screen. This rectangle is in screen coordinates, so you must do some conversions if you want to confine the mouse to an object on the form:
«Co
....Read More |
Rating
|
|
|
Get Mouse Position Anywhere, Anytime
|
Total Hit (3836) |
Some control events provide the mouse pointer's current position within the control's client area; others provide only the screen coordinates of the mouse pointer (the same as returned by Cursor.Position). Is there a function that tells you where the mouse is positioned within a specific control? We
....Read More |
Rating
|
|
|
Mouse clipping with the Cursor class
|
Total Hit (2153) |
The Clip property of the System.Windows.Forms.Cursor class represents the rectangle within which the mouse cursor is confined, or Nothing if the mouse can move over the entire screen. This rectangle is in screen coordinates, so you must do some conversions if you want to confine the mouse to an obje
....Read More |
Rating
|
|
|
Retrieving the state of the Shift, Alt and Ctrl keys, at any time
|
Total Hit (2527) |
You can know the state of the Shift, Alt and Control keys at any time, not just from inside a Keyxxx event. The shared property Control.ModifierKeys returns a bit coded value that identifies which of those keys are pressed, and you can use the And bit operator if a specific key is pressed. The follo
....Read More |
Rating
|
|
|
Showing help
|
Total Hit (2680) |
The System.Windows.Forms.Help class encapsulates the HTML Help 1.0 engine and lets you display the index, the search page, or a specific topic in an HTML file in HTML Help format or a compiled help file (.chm) authored with the HTML Help Workshop or some third party tool.
This class exposes two
....Read More |
Rating
|
|
|
Change the application priority
|
Total Hit (2921) |
The application's priority - and more in general the thread's priority - indicates how the application is "important" for the CPU. The more the priority is hight the more the CPU will dedicate his time to execute it.
In .NET the priority of a thread is indicated to the Thread.Priority property. B
....Read More |
Rating
|
|
|
Check whether Microsoft Word is installed
|
Total Hit (3069) |
The .NET frameworks provides a set of classes that let you work with the Registry, so it is quite easy to create routine that looks for specific keys to determine whether a program is installed or not. For example, the following code checks whether Microsoft Word is installed on the local computer:
....Read More |
Rating
|
|
|
Create a command-line filter utility
|
Total Hit (2777) |
The Console class exposes two properties that make it very simple to create command-line utilities that work as filters, exactly like the FIND and MORE utilities that are provided with the operating system. The Console.In property returns a TextReader object that reads from the standard input channe
....Read More |
Rating
|
|
|
Determine the Windows version
|
Total Hit (3109) |
You don't need to call any Windows API function in VB.NET to determine which version of Windows your application is running on, because this information is exposed by the Environment.OSVersion property. For example, if you run this code under Windows 2000:
«Code LangId=2»
Console.WriteLine(Envir
....Read More |
Rating
|
|
|
Determine type and number of CPUs
|
Total Hit (4432) |
There is no method call that directly retuns information about the number and type of installed CPU(s). However, this information is stored in a few environment variables, so it's just a matter of extracting it with the Environment.GetEnvironmentVariable method:
«Code LangId=2»
' this code is me
....Read More |
Rating
|
|
|
Duplicate the SET operating system command
|
Total Hit (2581) |
The Environment.GetEnvironmentVariables property returns the list of all the environment variables, as an IDictionary object. If you want to display them sorted by their name, as the SET command does at the prompt, you need the following tricky code:
«Code LangId=2»
' Get all the variables, as a
....Read More |
Rating
|
|
|
Launch another process in a specified directory
|
Total Hit (2697) |
The System.Diagnostics.Process class provides several methods to launch and controls other Windows processes. For example, the following code sample shows how to run Notepad, pass arguments to it, and run it from inside a given intial directory:
«Code LangId=2»
' this code assumes that you have
....Read More |
Rating
|
|
|
Drawing Cardinal splines
|
Total Hit (4350) |
GDI+ supports two different forms of a complex curve that can't be represented as an arc of a circle or an ellipse: a cardinal spline and a Bézier spline. A cardinal spline is the curve that you would create by taking a piece of flexible material-such as a thin stripe of iron or wood-and making it p
....Read More |
Rating
|
|
|
Measuring a string drawn with a specific font
|
Total Hit (2632) |
When you're dynamically drawing text (either on a Windows Form or Web Form application), you may need to calculate the width and height of a string printed with a particular font. This is easy with the Graphics class's Measure string method, that takes in input the string being measured, and the fon
....Read More |
Rating
|
|
|
Process individual pixels of a bitmap
|
Total Hit (3903) |
While you can access individual pixels of a bitmap by means of the GetPixel and SetPixel methods of the Bitmap object, in practice you seldom want to use these methods, as they are simply too slow for most cpu-intensive operations. Fortunately there is a faster way, which consists of moving all the
....Read More |
Rating
|
|