In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use IntPtr in C#". In daily operation, I believe many people have doubts about how to use IntPtr in C#. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use IntPtr in C#". Next, please follow the editor to study!
What is IntPtr?
Let's take a look at what MSDN says: the platform-specific type used to represent pointers or handles. This actually tells the fact that IntPtr can be used to represent a pointer or handle, which is a platform-specific type.
The IntPtr types in C # are called platform-specific integer types, and they are used for native resources, such as window handles. The size of a resource depends on the hardware and operating system used, but it is always large enough to contain a pointer to the system (so you can also include the name of the resource).
So, there must be parameters like the form handle in the API function you call, so when you declare this function, you should explicitly declare it as an IntPtr type.
For example, call the Win32API mciSendString function to control the CD-ROM drive in a C # program. The function prototype of this function is:
MCIERROR mciSendString (LPCTSTR lpszCommand, LPTSTR lpszReturnString, UINT cchReturn, HANDLE hwndCallback)
First declare this function in C #:
[DllImport ("winmm.dll")] private static extern long mciSendString (string a pencil string breco uint cmeno IntPtr d)
Then call it with this method:
MciSendString ("set cdaudio door open", null, 0, this.Handle)
You can also use IntPtr.Zero to set the handle to 0
Or use type casting:
MciSendString ("set cdaudio door open", null, 0, (IntPtr) 0)
Alternatively, use the IntPtr constructor:
IntPtr a = new IntPtr (2121)
The IntPtr type in 1.C# is called a "platform-specific integer type" and is used for native resources, such as window handles.
two。 The size of the resource depends on the hardware and operating system used, that is, an instance of this type will be 32-bit on 32-bit hardware and operating system and 64-bit on 64-bit hardware and operating system; but it is always large enough to contain the pointer to the system (so it can also contain the name of the resource).
3. When calling the API function, similar to a prototype function with a window handle parameter (HANDLE), it should be explicitly declared as an IntPtr type.
The 4.IntPtr type is safe for multithreaded operations.
5. Int and IntPtr transfer each other
Int iTunes 1; IntPtr p=new IntPtr (I); int ch_i= (int) p
6. IntPtr and string transfer each other
String str= "a"; IntPtr p=Marshal.StringToHGlobalAnsi (str); string s=Marshal.PtrToStringAnsi (p); Marshal.FreeHGlobal (p); at this point, the study on "how to use IntPtr in C#" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.