• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            為生存而奔跑

               :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

            留言簿(5)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            積分與排名

            • 積分 - 326993
            • 排名 - 74

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            Implementing APIs in C# is a tough job for beginners. Before implementing API you should know how to implement structure in C#, type conversion, safe/unsafe code, managed/unmanaged code and lots more.

            Before implementing complex APIs we will start with simple MessageBox API. To implement code for the MessageBoxAPI open a new C# project and add one button. When button gets clicked the code will display a Message Box.

            Since we are using external library, add a namespace:

             Collapse code snippet
            using System.Runtime.InteropServices;

            Add the following lines to declare the API

             Collapse
            [DllImport("User32.dll")]
            public static extern int MessageBox(int h, string m, string c, int type);

            Here the DllImport attribute is used for calling the method from unmanaged code. "User32.dll" indicates the library name. The DllImport attribute specifies the dll location that contains the implementation of an extern method. Thestatic modifier is used to declare a static member, which belongs to the type itself rather than to a specific object,extern is used to indicate that the method is implemented externally. A method that is decorated with the DllImportattribute must have the extern modifier.

            MessageBox is the function name, which returns int and takes 4 parameters as shown in declaration.

            Many APIs use structures to pass and retrieve values, as it is less expensive. It also uses constant data type for passing constant data and simple data types for passing built-in data types as seen in the previous declaration of the MessageBox function.

            Add following code for button click event:

             Collapse
            protected void button1_Click(object sender, System.EventArgs e)
            {
            	MessageBox (0,"API Message Box","API Demo",0);
            }

            Compile and run project, after clicking on the button you will see a MessageBox, which you called using API the function!!!

            Using Structures

            Working with APIs, which use complex structures, or structures inside structures, is somewhat more complex than using simple APIs. But once you understand the implementation then the whole API world is yours.

            In next example we will use GetSystemInfo API which returns information about the current system.

            The first step is open a new C# form and add one button on it. Go to the code window of the form and add a namespace:

             Collapse
            using System.Runtime.InteropServices;

            Declare the structure, which is the parameter of GetSystemInfo.

             Collapse
            [StructLayout(LayoutKind.Sequential)]
            public struct SYSTEM_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;
            }

            Declare the API function:

             Collapse
            [DllImport("kernel32")]
            static extern void GetSystemInfo(ref SYSTEM_INFO pSI); 

            Where ref is next to the method parameter keyword it causes a method to refer to the same variable that was passed into the method.

            Add the following code in the button click event in which we first create a struct object and then pass it to function.

             Collapse
            protected void button1_Click (object sender, System.EventArgs e)
            {
            	try
            	{
            		SYSTEM_INFO pSI = new SYSTEM_INFO();
            		GetSystemInfo(ref pSI);
            		//
            
            		//
            
            		//

            Once you retrieve the structure, perform operations on the required parameter

            e.g.listBox1.InsertItem (0,pSI.dwActiveProcessorMask.ToString());:

             Collapse
            		//
            
            		//
            
            		//
            
            	}
            	catch(Exception er)
            	{
            		MessageBox.Show (er.Message);
            	}
            }
            posted on 2009-12-20 14:09 baby-fly 閱讀(296) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C#
            …久久精品99久久香蕉国产 | 久久精品国产亚洲AV高清热| 亚洲精品成人久久久| 日日狠狠久久偷偷色综合免费| 国产精品久久久久久久app| 性高湖久久久久久久久| 99久久99久久精品国产片| 国产精品久久久香蕉| 国产精品对白刺激久久久| 久久精品国产亚洲一区二区三区| 漂亮人妻被中出中文字幕久久| 91精品国产高清91久久久久久| 久久男人AV资源网站| 亚洲欧美精品伊人久久| 少妇熟女久久综合网色欲| 久久久精品午夜免费不卡| 久久久久久精品无码人妻| 人人狠狠综合久久亚洲婷婷| 99久久国产综合精品女同图片 | 人妻精品久久无码专区精东影业| 久久久国产一区二区三区| 亚洲AV无码久久寂寞少妇| 久久久久亚洲AV综合波多野结衣| 国产精品久久久久久一区二区三区| 亚洲欧美一级久久精品| 国产精品青草久久久久福利99 | 亚洲欧美日韩中文久久| 综合久久精品色| 欧美激情精品久久久久久久九九九 | 日日狠狠久久偷偷色综合免费| 久久国产一区二区| 2022年国产精品久久久久 | 中文精品久久久久人妻| 人妻少妇精品久久| 欧美国产成人久久精品| 亚洲美日韩Av中文字幕无码久久久妻妇| 久久精品国产91久久综合麻豆自制 | 久久这里只有精品首页| 中文字幕无码av激情不卡久久| 合区精品久久久中文字幕一区| 久久精品国产99久久久香蕉 |