Use API functions in Visual C # To obtain system information

Source: Internet
Author: User

 

API functions are used to build Windows applications. Program Is the cornerstone of Windows programming. Every windows application development tool provides indirect or direct calls to Windows

API function method, or call windows
The API of the API function, that is, the ability to call the dynamic Connection Library. Visual
C # can call the API functions of the dynamic link library like other development tools. In this article, I will introduce you to visual

In C #, how to call APIs with various returned values is a program that obtains system information through API function calls.

The basic process of calling an API in Visual C:

First, you must first import system. runtime. interopservices to the namespace before calling the API. This namespace is included in the visual

In C #, call some necessary sets of APIs. The specific method is as follows:
Using system. runtime. interopservices;

After the namespace is imported, We need to declare the API functions used in the program. Our program mainly obtains information about the system, so all the API functions used return system information. First

Method for declaring an API in C:



[Dllimport ("Kernel32")]

Public static extern void
Getwindowsdirectory (stringbuilder WINDIR, int
Count );

 
The "dllimport" attribute is never controllable.CodeIt specifies the location of the DLL, which contains the called external method; "Kernel32"
Class Library name; "public" indicates that the function's access type is public; "static" modifier declares a static element, and this element belongs to the type itself rather than the specified
Image; "extern" indicates that the method will be executed outside the project, and the method imported using dllimport must use the "extern" modifier;
The getwindowsdirectory function contains two parameters: stringbuilder and Int. The content returned by this method exists in
Stringbuilder parameters. At the same time, because the stringbuilder class is used here, we have to add
The namespace system. text. The method is the same as above.

The declaration of several other API functions is as follows:



[Dllimport ("Kernel32")]

Public static extern void
Getsystemdirectory (stringbuilder Sysdir, int
Count );

[Dllimport ("Kernel32")]

Public static extern void
Getsysteminfo (ref cpu_info cpuinfo );

[Dllimport ("Kernel32")]

Public static extern void
Globalmemorystatus (ref memory_info meminfo );

[Dllimport ("Kernel32")]

Public static extern void
Getsystemtime (ref systemtime_info stinfo );
 
These APIs are used to obtain the system path, CPU information, memory information, and system time.

After declaring all the API functions, we found that the last three functions use the cpu_info, memory_info, systemtime_info, and other structures.
Not within. net. Where do they come from? In fact, we need to use the above structure when using the above API call. we store the information obtained by the function call in the above struct, and finally return
Sequential output. These structures are complex, but if developers are skilled in using them, the entire API world will be under control by developers. The Declaration of the above struct is as follows:



// Define the following structures

// Define the CPU Information Structure


[Structlayout (layoutkind. Sequential)]

Public struct cpu_info

{

Public uint dwoemid;

Public uint dwpagesize;

Public uint
Lpminimumapplicationaddress;

Public uint
Lpmaximumapplicationaddress;

Public uint
Dwactiveprocessormask;

Public uint
Dwnumberofprocessors;

Public uint
Dwprocessortype;

Public uint
Dwallocationgranularity;

Public uint
Dwprocessorlevel;

Public uint
Dwprocessorrevision;

}


// Define the memory information structure


[Structlayout (layoutkind. Sequential)]

Public struct
Memory_info

{

Public uint dwlength;

Public uint
Dwmemoryload;

Public uint
Dwtotalphys;

Public uint
Dwavailphys;

Public uint
Dwtotalpagefile;

Public uint
Dwavailpagefile;

Public uint
Dwtotalvirtual;

Public uint
Dwavailvirtual;

}

// Define the system time information structure


[Structlayout (layoutkind. Sequential)]

Public struct
Systemtime_info

{

Public ushort wyear;

Public ushort wmonth;

Public ushort
Wdayofweek;

Public ushort wday;

Public ushort whour;

Public ushort wminute;

Public ushort wsecond;

Public ushort
Wmilliseconds;

}
 
Body part and C defined by struct
There is not much difference. For details about the definition of each struct Member, refer to the SDK documentation in online help. At the same time, we also found a descriptive text enclosed in brackets on the top of each struct definition. These descriptions are related to the structure member layout. There are three options:

Layoutkind. Automatic: to improve efficiency, allow the running state to re-sort type members.


Note: never use this option to call uncontrolled Dynamic Linked Library functions.
Layoutkind. explicit: sort type members by fieldoffset attribute for each domain.

Layoutkind. Sequential: sorts the type members in the memory that are not under the jurisdiction of the type definition.

In the above program, we use the third method for convenience.

After all the API functions and related struct declarations are completed, we use these APIs to implement our program functions-to obtain system-related information.

The interface can be arranged as follows, but interested readers can naturally make full use of their imagination to make the interface layout better.


 
After the simple interface is configured, we add the message processing function of a button ("Get information" button) as follows:

Private void
Getinfo_click (Object sender, system. eventargs E)

{


// Call the getwindowsdirectory and getsystemdirectory functions to obtain the Windows path and system path respectively.

Const int nchars = 128;

Stringbuilder buff = new
Stringbuilder (nchars );


Getwindowsdirectory (buff, nchars );

Windowsdirectory. Text =
"Windows path:" Buff. tostring ();


Getsystemdirectory (buff, nchars );

Systemdirectory. Text =
"System Path:" Buff. tostring ();


// Call the getsysteminfo function to obtain CPU Information

Cpu_info cpuinfo;

Cpuinfo = new
Cpu_info ();

Getsysteminfo (ref
Cpuinfo );

Numberofprocessors. Text =
"Cpuinfo. dwnumberofprocessors. tostring () is available in this computer ()
"CPUs ";

Processortype. Text =
"CPU type:" cpuinfo. dwprocessortype. tostring ();

Processorlevel. Text =
"CPU level:" cpuinfo. dwprocessorlevel. tostring ();

Oemid. Text = "cpu oem id is"
Cpuinfo. dwoemid. tostring ();

Pagesize. Text =
"The page size in CPU is" cpuinfo. dwpagesize. tostring ();


// Call the globalmemorystatus function to obtain memory information.

Memory_info meminfo;

Meminfo = new
Memory_info ();

Globalmemorystatus (ref
Meminfo );

Memoryload. Text =
Meminfo. dwmemoryload. tostring () "% memory in use ";

Totalphys. Text =
"The total physical memory is" meminfo. dwtotalphys. tostring () "Byte ";

Availphys. Text =
"The available physical memory is" meminfo. dwavailphys. tostring () "Byte ";

Totalpagefile. Text =
"The total size of the swap file is" meminfo. dwtotalpagefile. tostring () "Byte ";

Availpagefile. Text =
"The file size is still interchangeable" meminfo. dwavailpagefile. tostring ()
"Byte ";

Totalvirtual. Text =
"The total virtual memory is" meminfo. dwtotalvirtual. tostring () "Byte ";

Availvirtual. Text =
"Unused Virtual Memory" meminfo. dwavailvirtual. tostring () "bytes ";
 

// Call the getsystemtime function to obtain the system time information.

Systemtime_info stinfo;

Stinfo = new
Systemtime_info ();

Getsystemtime (ref
Stinfo );

Date. Text =
Stinfo. wyear. tostring () "year" stinfo. wmonth. tostring () "month"
Stinfo. wday. tostring () "day ";

Time. Text = (stinfo. whour
8). tostring () "point" stinfo. wminute. tostring () "point"
Stinfo. wsecond. tostring () "seconds ";

}

In the above message processing functions, we use the various API functions declared at the beginning of the program to obtain information about the system, and finally display the results in the form of text labels on the interface. For the naming method of each text tag, seeSource code.

Finally, run the program as follows:

 

Conclusion:

Through the study in this article, I believe that developers who use basic APIs can immediately access the category and quickly master the visual

C # operations on the API. The example above is just a very simple example program, but interested readers can further improve its functions and make more perfect system information detection programs.

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.