Use P/invoke annotate to ask native API problems-rect is not equal to system. Drawing. rectangle.

Source: Internet
Author: User

(Domestic version)

I used a pocketpc compiled last night.Program(Use native API) to switch to the. NET Compact framework Platform. Because only native API was used in the past, there was a problem... But it is always an easy solution! Here are some tips to share with you.

Last night, I downloaded the API explorer of the paulyao compiler to find the native API annotation method I needed. I just needed it.GetwindowrectThe problem is thatGetwindowrectThe usage of this API is as follows:



< Dllimport ( " User32.dll " ) > _

Public   Function getwindowrect () FunctionGetwindowrect (_

ByvalHwndAsIntptr _

,ByrefLprectAsSystem. Drawing. Rectangle _

)As Integer

End Function

In fact, it should be the following clear method:

< Structlayout (layoutkind. Sequential) > _

Public   Structure rect Structure Rect

Public   Left   As   Integer

Public Top As   Integer

Public   Right   As   Integer

Public Bottom As   Integer

End Structure


< Dllimport ( " User32.dll " ) > _

Public   Function getwindowrect () function getwindowrect (_
byval hwnd as intptr _
, byref lprect as rect _
) as integer
end function

This is just a typical example... So busy, I just tried it for a few hours and I couldn't even find out where it was... It was not until this morning that this API was clearly problematic. Because the former replaces the original rect with system. Drawing. rectangle. Coincidentally, the structure size of system. Drawing. rectangle is larger than that of rect. Therefore, the former does not produce an exception commit (Exception error. However, some incorrect data records are returned.

Instead of experiencing the differences between the two, I compiled a small program for an experiment. From the picture, we can see that the number obtained by using system. Drawing. rectangle is different from the number obtained by using rect. In addition, I also use the top, left, right, and bottom values in form object to compare them. However, the result is exactly the same as that of rect. In addition to program-based failover, I also use Spy ++ to perform another failover; this also indicates that the data obtained by rect is correct, not system. drawing. rectangle.

In addition, I also found that when calling the native API, I should pay special attention to the ANSI and Unicode annotation methods. For exampleFindwindowwOn the Windows XP platform, retrieve the hwnd of a window as & H0;Find0000waYou can obtain the hwnd value of the phase sequence. That is becauseFindwindowwIs used for Unicode platforms (pocketpc and WindowsCE ).

< Dllimport ( " User32.dll " ) > _

Public   Function find0000wa () FunctionFind0000wa (_

ByvalLpclassnameAs String_

,ByvalLpwindownameAs String_

)AsIntptr

End Function


< Dllimport ( " User32.dll " ) > _

Public   Function findwindoww () function findwindoww (_
byval lpclassname as string _< br>
, byval lpwindowname as string _< br>
) as intptr
end function

It solved my problem... At the same time, I also found a forum on www.pinvoke.net with P/invoke and an apiexplorer development tool similar to VB6.0 apiviewer. If you are a fan of WIN32API, you will not be able to handle these two good things.

Click "Next" to "Next Generation.

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.