Example of Windows API function calling in Visual Basic

Source: Internet
Author: User
Tags return tag

Example of Windows API function calling in Visual Basic

【Abstract]
This document describes how to use Windows API function in Visual Basic, and introduces

API function. For example, the VB sequence stores the initial parameters and checks the original information.

[Close key]
Windows Microsoft (Microsoft) Company's Visual Window System.
Visual Basic (VB) Microsoft (Microsoft) Company's visualized editing tools.
Application program interface.
Original file (for example, demo. INI)

[Zheng Wen]

I. use VB to call the API function method.

(1) Use declare to declare the number of API functions to be called. If the return value of this function is returned, it can be specified as a sub process; if a return value exists, you can specify the number of function functions.

Note: All the descriptions of the number of API letters are in. /Vb/winapi/win31api. in HLP, only the copy and paste methods can be used to store the corresponding content.

(2) once an API function is specified, it can be called as a function of VB. However, it should be noted that, if the parameter data is not delivered correctly, it can lead to a dead machine.

2. The VB program has the initial parameter number for private storage.

The obtaining and saving of the initial parameters of the Windows software is based on read expansion. the INI text file is implemented, that is, search for the required parameters from the specified INI file before running the program, the parameter values are reversed to the running environment of the process. When the process returns, the environment parameters are saved to the specified INI file. Getprivateprofilestring and writeprivateprofilestring in the number of API functions provided by windows have these functions.

Description:
(1) Description: declare function getprivateprofilestring lib "Kernel32" alias "alias" (byval lpapplicationname as string, byval lpkeyname as any, byval lpdefault as string, byval lpreturnedstring as string, byval nsize as long, byval lpfilename as string) as long

Function: Obtain the parameter number (string) corresponding to the specified key pair in the INI file and copy the parameter number to lpreturnedstring.

Parameter
Type
Description
Lpapplicationname
String
The segment of the key to be searched
Lpkeyname
String
The key name corresponding to the parameter
Lpdefault
String
Indicates the missing province value returned when the specified key word is not stored.
Lpreturnedstring
String
Pre-split the character string with a long length to less than nsize
Nsized
Integer
Maximum number of characters to be loaded into the lpreturnedstring slow-forward zone
Lpfilename
String
Name of the initial file
Return Value
Integer
Number of characters copied to the lpreturnedstring slow-forward area

(2) writeprivateprofilestring
Declaration: declare function writeprivateprofilestring lib "Kernel32" alias "writeprivateprofilestringa" (byval lpapplicationname as string, byval lpkeyname as any, byval lpstring as any, byval lpfilename as string) Long

Function: set the number of parameters (character strings) in the specified key field of the initial component ).
Return Value: if it is set to success, true is returned; otherwise, false is returned.

Iii. VB pre-test system information

The required API functions include getwindowsdirectory, getwinflags, getversion, and getsystemdirectory. For specific usage methods, see the example.

Iv. Example

Demo. prj

(1) create a project file demo. prj, create a module File demo. Bas, and insert the following statements into demo. Bas.

Declare function getwindowsdirectory lib "Kernel32" alias "getwindowsdirectorya" (byval lpbuffer as string, byval nsize as long) as long

Declare function getprivateprofilestring lib "Kernel32" alias "inline" (byval lpapplicationname as string, byval lpkeyname as any, byval lpdefault as string, byval lpreturnedstring as string, byval nsize as long, byval lpfilename as string) as long

Declare function writeprivateprofilestring lib "Kernel32" alias "writeprivateprofilestringa" (byval lpapplicationname as string, byval lpkeyname as any, byval lpstring as any, byval lpfilename as string) as long
Declare function getkeyboardtype lib "USER32" (byval ntypeflag as long) as long
Declare function getsystemdirectory lib "Kernel32" alias "getsystemdirectorya" (byval lpbuffer as string, byval nsize as long) as long
Declare function getversion lib "Kernel32" () as long
Declare function getwinflags lib "Kernel32" () as long

Note:

  1. Getwindowsdirectory: Obtain the path of the Windows directory.
  2. Getsystemdirectory: Obtain the path of the Windows system sub-directory.
  3. Getversion: this function is returned to the previous Windows Version Number and DOS version number. The "low-bit" section of the return value indicates the Windows primary version number, and the "low-bit" section indicates the Windows secondary version number, the low-bit section of the High-bit character represents the DOS minor version number, and the high-bit section of the High-bit character represents the primary version number of the DOS.

4. getwinflags: return the function to the internal storage configuration in the Windows operating system.

Return tag Value
Description
Wf_80x87
Intel Digital Assistant
Wf_cpu386
80386 CPU
Wf_cpu.pdf
80486 CPU
Wf_enhanced
Windows system run in 386 Enhanced Mode
Wf_pmode
Protection Mode for Windows System Operation
Wf_standard
Running Windows systems in standard mode
Wf_wlo
Run in OS/2

5. getkeyboardtype (): this function is counted as a system key disk type. If ntypeflag is set to 0, the Return key disk type is returned.
Return Value
Description
1
Ibm pc/XT or SSD
2
Olivetti "ICO" key disk (102 keys)
3
IBM or SSD (84 keys)
4
IBM enhanced or similar key disks (101 or 102 keys)
5
Nokia 1050 or key-like Disk
6
Nokia 9140 or key-like Disk
7
Daily key disk

6. Call these API functions to add more functions. You can compile some packages containing functions. The functions of user-defined function numbers also need to be in the module.


Function Sysdir ()
Temp = space $ (255)
Stringlen = getsystemdirectory (temp, 255)
Sysdir = left $ (temp, stringlen)
End Function
___________________________________________________________________________
Function WINDIR () as string
Temp = space $ (255)
Stringlen = getwindowsdirectory (temp, 255)
WINDIR = left $ (temp, stringlen)
End Function
___________________________________________________________________________
Function keytype ()
Kbtype = getkeyboardtype (0)
Select case kbtype
Case 1
Keytype = "ibm pc/XT, or compatible (83key )"
Case 2
Keytype = "Olivetti 'ico '(102key )"
Case 3
Keytype = "IBM at, or similar (84key )"
Case 4
Keytype = "IBM enhance (101 or 102 keys )"
Case 5
Keytype = "Nokia 1050 or similar"
Case 6
Keytype = "Nokia 1050 or similar"
Case 6
Keytype = "Nokia 9140 or similar"
Case 7
Keytype = "Japan keyboard"
End select
End Function
___________________________________________________________________________
Function initial ()
Form1.left = Val (stringformini ("form", "Left", "0", "C:/Windows/demo. ini "))
Form1.top = Val (stringformini ("form", "TOP", "0", "C:/Windows/demo. ini "))
Form1.windowstate = Val (stringformini ("form", "windowstate", "0 ","
C:/Windows/demo. ini "))
End Function
_______________________________________________________________________
Function saveinfor ()
X = writeprivateprofilestring ("form", "Left", STR $ (form1.left ),
"C:/Windows/demo. ini ")
X = writeprivateprofilestring ("form", "TOP", STR $ (form1.top ),
"C:/Windows/demo. ini ")
X = writeprivateprofilestring ("form", "windowstate", STR $
(Form1.windowstate), "C:/Windows/demo. ini ")
End Function
___________________________________________________________________________
Function stringformini (sectionname as string, keyname as string,
Default as string, filename as string) as string
Maxstringlen %= 255
Returnedstr $ = space $ (maxstringlen %)
Result % = getprivateprofilestring (sectionname, keyname,
Default, returnedstr $, maxstringlen %, filename)
Returnedstr $ = ltrim $ (rtrim $ (returnedstr $ ))
Returnedstr $ = left $ (returnedstr $, Len (returnedstr $)-1)
Stringformini = returnedstr $
End Function
__________________________________________________________________________
Function winver ()
Ver & = getversion ()
Winhigh = Ver & mod 256
Winlow = int (Ver &/256) mod 256
Winver = (winhigh * 100) + winlow)/100
End Function
___________________________________________________________________________

Function dosver ()
Ver & = getversion ()
Temp = Server &/65536
Doshigh = int (temp/256) mod 256
Doslow= temp mod 256
Dosver = (doshweigh * 100) + doslow)/100
End Function
___________________________________________________________________________
Function CPU ()
Flags & = getwinflags ()
Match = 1
Select case match
Case (flags & and & H8)/& H8

CPU = 486
Case (flags & and & h4)/& h4

CPU = 386
End select
End Function
___________________________________________________________________________
Function mode ()
Flags & = getwinflags ()
If flags & and & H20 then
Mode = "enhanced"
Else
Mode = "standard"
End if
End Function

(2) create a window file form1.frm
Form1.caption = "system information"
Sub form_paint ()
CLS
Print
Print, "system information"
Print
Print, "windowsdir:", WINDIR ()
Print, "SystemDir:", Sysdir ()
Print, "windowsversion:", winver ()
Print, "dosversion:", dosver ()
Print, "keyboardtype:", keytype ()
End sub
Sub form_load ()
X = initial ()

End sub
Sub form_unload (cancel as integer)
X = saveinfor ()
End sub

(3) run the local program. (This process is passed under Windows 486 and windows)
This process can be used to check and test the information of the front-end system, and the left and top positions of the window ports and the window-like states can be recorded in the demo during exit. in the INI file, when the next operation is performed, the window port can hold the bit and state of the last return.
Note: you must use the text editor to compile demo. ini.


[Form]
Left = 1, 100
Maximum = 100
Windowstate = 0

And stored in the current system Windows directory.
This article only introduces the method of using the number of API letters for the purpose of throwing bricks and causing Jade. Really should use API letters, but also need more detailed information and the greater effort of the reader.
[Subjects]
Windows3.1 API function, data structure, and message consumption details

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.