Use the Unicode API in VB
Introduction maybe everyone knows that all and string related functions within Visual Basic are unicode. Unicode is a set of character sets. Unlike other conventional single-byte character sets, it uses two bytes to represent a character, which makes the number of available characters greatly increases (theoretical, one byte can contain up to 256 characters, and two The byte can contain 65536 characters). Be careful here, don't confuse the double-byte character sets and Unicode and Unicode and Unicode in the GB2312: the former has both single-byte characters (such as Chinese), which makes management very troublesome, and it only A code page is supported; and Unicode's characters are double-bytes such that management, conversion, and use strings are very easy. And it supports all common text in the world, so that a program can display a variety of languages on the screen at the same time without care for the current code page. Ok, after I briefly introduced Unicode, now introduce Unicode API (what? I don't know what API? (Sweat) If this is the case, this article may not be suitable for you). Windows NT uses Unicode from the beginning, so that its subsequent products (Windows 2000, XP) have been using it. Windows 95, 98 and ME are not so lucky, they have always used a single (double) byte character set. This makes the same relationship with the same string with two different character sets: Unicode and non-Unicode. On the function name, the Unicode version of the API has a 'W' suffix represents wide, such as: MessageBoxw; not Unicode version API has a 'a' suffix represents ANSI, such as: MessageBoxa. Both versions of the API on Windows NT / 2000 / XP, that is, Windows NT supports Unicode and non-Unicode character sets. Almost all APIs on Windows 95/98 / me have only its non-Unicode versions, which means they support single (double) byte character sets. The VB and Unicode API are now unfair. VB After using Unicode, you can enjoy the various benefits of Unicode. However, the trouble is also coming ... because Windows 95/98 / ME does not support Unicode, and VB only supports Unicode, so when you call a non-Unicode API from the VB, VB first converts all strings first. Non-Unicode string, then call the API, and finally convert all the strings back to Unicode (see Figure 1). This makes the speed of the API becomes very slow, and the efficiency is low.
┌ - --────────────────────────────── ─ ─ ─ ─ │ │-> │ convert to non-Unicode (a) │ -> │ ││ │ └────────── ─ │ API (a) ││ │ │ │ ┌──────── ─ ┐ └ - ──── ─ ┘│ │ --│ convert to Unicode (W) │ <───────────────────────────────────────────────────────────────────────── ─────── ─ ┘ Figure 1: Calling non-Unicode API: Need to convert to see here, maybe someone will ask "Why don't you use Unicode API?" In the API viewer? " The API is non-Unicode (Alias is the end of 'A') because of compatibility issues. To make the program execute in Windows 9x and NT, you must call non-Unicode's API, or something will be very troublesome; but this is based on performance. If the program requires high performance, often use the API that needs to pass the string as a parameter, and only run in a Windows NT / 2000 / XP environment (such as server programs), you can use the Unicode API to replace the non-Unicode API with a UNICODE API (See Figure 2).
┌───── ─ ─ ────────────────────────────> │ ││ │ │ API ( W) ││ VB Program (W) │ │ ││ │ └─────── ─ ─ ─ ─ ─ ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ┘ ┘ └────── ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ It is enough: The following is a simple API declaration: (Role is the title of getting a form) Declare function getWindowText lib "user32" Alias "getWindowTexta" (Byval Hwnd As long, Byval Lpstring As String, Byval Cch as long) AS Long'GetWindowTextA 'indicating that this is the Unicode API the following is a non-modified API declarations: declare Function GetWindowText Lib "user32" Alias "GetWindowTextW" (ByVal hwnd As Long, ByVal lpString As Long, ByVal cch As Long) As Long'GetWindowTextW 'Indication This is a Unicode API, which modifies a non-Unicode API to become a Unicode API is: * Replace the end' A 'of the function name in Alias with' W '. * All byval *** as string is changed to byval *** as long. The above is a declaration method, the following is a call method: the original call method: RC = getWindowText (HWND, STR, STRLEN) modified call method: rc = getWindowText (hwnd, strptr (str), strlen) This shows that one is called The point of the Unicode API is to change the origin of string variable / constant to StrPtr (string variable / constant). For programmers for contact APIs, it is necessary to understand the principles of the above declaration methods and call methods. First, put the end 'A' of the end 'a' in the Alias to 'W' because we need to call the Windows NT / 2000 / XP Unicode version of the API. First introduce the VB variable length string variable. VB variable length string variable is actually a pointer to its Unicode string (see Figure 3).
┌ --────────────── ─ ┐│. . . │ │. . . ││ ┈┈┈┈┈┈┈│ │ ┈┈┈┈┈┈┈││Sarre A │ │ │ ┈┈┈┈┈┈┈│ │ ┈┈┈┈┈┈┈││ │ │ │ -> │ Characters 1 ││ ┈┈┈┈┈┈┈│ / │ ┈┈┈┈┈┈┈││ │ │ │ Characters 2 ││ ┈┈┈┈┈┈┈│ │ ┈┈┈┈┈ ┈┈││ Variables C │ │ Characters 3 ││ ┈┈┈┈┈┈┈│ │ ┈┈┈┈┈┈┈││Caries D │ │ Characters 4 ││ ┈┈┈┈┈┈┈│ │ ┈┈┈ ┈┈┈┈││. . . │ │. . . │ └ - ─ ┘ └ └ └ └ └ └ ─ ─ ─ ┘ Figure 3: VB variable length string variables All byval *** as string, change to byval *** as long because we want Pass the address of the VB Unicode string instead of the string to convert the address after ANSI. Change the original string variable / constant to StrPtr (string variable / constant) because we can get the address of the Unicode string (both the address of 'character 1' in Figure 3) by the StrPtr function. Example After knowing how to convert and call the Unicode API, let's take an example: 'Use Unicode API' in VB to select the LSTRCPY this function is complete because all Windows systems have supported its Unicode version, including Windows 98. And ME. 'Make everyone can debug this example in any Windows 95 or more. (In fact, Windows 95/98 / ME only supports approximately 16 API's Unicode version.) 'This program simply uses API to copy a string to another string. This program only needs a module that is enough. Please be sure to set the startup window of the project to SUB Main (). The role of the Option Explicit 'LSTRCPY API is to copy a string to another string. Unicode and non-Unicode versions are LSTRCPYW and LSTRCPYA, respectively. Private Declare Function lstrcpyA Lib "kernel32" (ByVal lpString1 As String, ByVal lpString2 As String) As LongPrivate Declare Function lstrcpyW Lib "kernel32" (ByVal lpString1 As Long, ByVal lpString2 As Long) As Long 'timeGetTime API used to calculate the time. Private Declare Function TimegetTime LIB "WINMM.DLL" () as longprivate const coupy_times = 10000 & 'to copy the number of times.