VB's API programming essence (1)

zhaozj2021-02-12  158

VB's API programming essence (1) In the last article, we introduced the basic knowledge of API programming and some "Cool" API calls. This issue we will cooperate with the topic to introduce the API function and its application examples of registry programming. Make reader friends to extend the previously learned registry knowledge to VB programming, continue to move into VB masters (road long ...). Of course, the previous period, I said that I want to introduce more "cool" API calls, can not yet, the generation of the flat tool bars introduced by the second half will make your program more "dazzling"! I. The API programming of the registry About the registry believes that you have a deeper understanding through the introduction of the previous topic. The system has six predefined keywords, which is the entry point of the user or system access to the registry. We often use only the first four keywords. When programming, we generally use only two keywords that are just hkey_current_user and hkey_local_machine, because data related to the application exists in both keywords. Many commercial software or specialized software will complete the software's correct installation and run by rewriting the registry to the registry, and dreams you have to master the technique of the programming. Use a good registry to have a lot of color to your application. Although VB itself provides four functions of the registry GetSetting, Savesetting, getAllSettings, deletesetting (more simple readers can refer to the VB online help), but these four functions can only be "HKEY_CURRENT_USER / SOFTWARE / Read, delete, modify the key value under VB and VBA ProgramSettings. For general applications, you can use them to achieve your goal, and use them to use them for a special requirement. Here, an example will be given to illustrate their limitations. Readers who are familiar with the DOS operating system know that the batch file of "Autoexec.bat" can be written to implement an application automatically run when the system is started. In Win95, we can put the application's shortcut to the system. The same effect is achieved in the launch group. However, if I need to automatically reach this effect after my application is installed, what should I do? In fact, the registry provides three such key: HKEY_LOCAL_MACHINE / Software / Microsoft / Windows / CurrentVersion / RunHKEY_LOCAL_MACHINE / Software / Microsoft / Windows / CurrentVersion / RunOnceHKEY_LOCAL_MACHINE / Software / Microsoft / Windows / CurrentVersion / RunServices three key words The difference is: run: This button will automatically run when the application is started; Runonce: This key is automatically run when the system is started next time, and it will not be run in the future; RunServices: function and Like "run", it is only different when the application is started. Now you know how to use the registry to meet your requirements. In fact, many installation software wants you to restart after the installation wizard is completed to complete the final installation. It is implemented by writing the last job required by the installation wizard to "runonce". However, if only the four functions of the VB itself is obviously unable to implement this feature. The author solves the limitations of the VB itself accessed by calling the API function in practice, and makes it a class module. So it is very convenient to call. Since the space is limited, I can only draw some from it, this part can also run independently. Readers want a complete source code, please contact me (Yue_xiang@263.net).

The following should be placed in the Declarations section of the code to your module: Option Explicit 'registry entry constants Public Const HKEY_CLASSES_ROOT = & H80000000Public Const HKEY_CURRENT_USER = & H80000001Public Const HKEY_LOCAL_MACHINE = & H80000002Public Const HKEY_USERS = & H80000003' registry access constants Public Const KEY_QUERY_VALUE = & H1Public Const KEY_SET_VALUE = & H2Public Const KEY_CREATE_SUB_KEY = & H4Public Const KEY_ENUMERATE_SUB_KEYS = & H8Public Const KEY_NOTIFY = & H10Public Const KEY_CREATE_LINk = & H20Public Const KEY_ALL_ACCESS = & H3F 'open / Create keys option constant Public Const REG_OPTION_NON_VOLATILE = 0 & Public Const REG_OPTION_VOLATILE = & H1' Create new key, or open existing key constant Public Const REG_CREATED_NEW_KEY = & H1Public Const REG_OPENED_EXISTING_KEY = & H2 'access predefined registry permission constant Public Const STANDARD_RIGHTS_ALL = & H1F0000Public Const SPECIFIC_RIGHTS_ALL = & HFFFF'API return code constants Public Const ERROR_SUCCESS = 0 & Public Const ERROR_ACCESS_DENIED = 5Public Const ERROR_NO_MORE_ITEMS = 259 'Return numerical type constant public const reg_none = (0) public const reg_sz = (1) public const reg_expand_sz = (2) public const reg_binary = (3) public conn st REG_DWORD = (4) PubliC ConSt REG_DWORD_LITTLE_ENDIAN = (4) Public Const REG_DWORD_BIG_ENDIAN = (5) Public Const REG_LINK = (6) Public Const REG_MULTI_SZ = (7) Public Const REG_RESOURCE_LIST = (8) Public Const REG_FULL_RESOURCE_DESCRIPTOR = (9) Public Const REG_RESOURCE_REQUIREMENTS_LIST = (10) 'access to the registry API function to use the structure type type SECURITY_ATTRIBUTESnLength As LonglpSecurityDescriptor As LongbInheritHandle As BooleanEnd TypeType FILETIMEdwLowDateTime As LongdwHighDateTime As LongEnd type' to use the API function declaration ............ (

In view of the consecutive volume, only the API's role is introduced, not one by one, and the relevant statement, please read the API browser), let's simply introduce these API: regopenkeyex (): Open the specified keyword (32-bit RegSetValueex (): Stores data in the value domain of the open registry keyword; regloseKey (): Release the handle of the specified keyword; regQueryValueex (): Find the value related to the key value you specify in the registry RegreateKeyEx (): Create and open the specified keyword, if it already exists; regenumKeyex (): Enumerates the subkey keyword (32-bit) of the specified open registry keyword; RegenumKey (): Like the same function, The difference is that it is 16 bits; RegenuValue (): Each time the value of the enumeration is specified to open the registry keyword, copy a name and data block with indexed values; regdeleteKey (): Delete a keyword and its sub Keywords; regdeletevalue (): Delete a value with name in the specified registry keyword. By calling these APIs we can easily implement the read, query, establishment, deletion of any keywords for the registry. The author only intends to introduce how to establish and delete a specific keyword. Other operating readers can play themselves. For example: To build a "MyApi" subkey in HKEY_LOCAL_MACHINE / NETWORK and establish a value domain called "YX" below it, set it to "Yue1975". We should call the API in the following way: Dim phkResult As Long 'Save Keywords handles established Dim Iresult As LongDim SA As SECURITY_ATTRIBUTESDim 1Create As Long' to establish a designated keyword caII RegCreateKeyEx (HKEY_LOCAL_MACHINE, "Network / MyApi", 0, "", REG_OPTION_NON_VOLATILE, _KEY_ALL_ACCESS, SA, phkResult, 1Create) 1Result = RegSetValueEx (phkResult, "yx", 0, REG_SZ, "yue1975", Clng (Len ( "yue1975") 1)) 'off key now RegCloseKey phkResult Use the Registry Editor to check the registry, you must generate the key values ​​you need. Another example: Now I want to delete the key value you just created, then you only need to call: DIM SUCCESS AS Longsuccess = regdeleteKey (HKEY_LOCAL_MACHINE, "Network / myapi") II. Bring a flat toolbar with API to believe in VB The enthusiasts have thousands of people want to make their tools in Word97 toolbar as COOL. Often we have to borrow an ActiveBar control to use others, so you don't say your own procedure becomes big, and that the control is not easy to use. The author uses the base class function SendMessagelong () when programming with VC5, and FindWindowEx () is easy to implement this Cool effect. Inspired by this inspiration, these two APIs are called in VB5 and also achieve the same effect.

转载请注明原文地址:https://www.9cbs.com/read-6089.html

New Post(0)