Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the new ICustomQueryInterface features of .NET 4.0?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The purpose of this article is to share with you what the new ICustomQueryInterface features of .NET 4.0 are. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

In the new features released in .NET Framework v4.0, a new Interface called ICustomQueryInterface has been added to the namespace System.Runtime.InteropServices. As the name implies, the function of this Interface is to allow users to control the behavior of QueryInterface, the most commonly used function in COM. Before v4.0, all QI actions acting on managed components were controlled by IUnkown:QueryInterface within CLR, for example, if you QI the famous IDispatch interface, you always get the IDispatch provided by CLR, and so on some commonly used Interface such as IMarshal/IProvideClassInfo. If you really want to replace the implementation provided by clr with your own IDispatch implementation, congratulations, ICustomQueryInterface is made for you! Of course, what ICustomQueryInterface brings is not just a simple Interface replacement, it can even make Aggregate managed components a reality.

Let's look at the definition of ICustomQueryInterface:

1: public interface ICustomQueryInterface 2: {3: CustomQueryInterfaceResult GetInterface ([In] ref Guid iid, out IntPtr ppv); 4:} 5:

Yes, it's that simple, just a GetInterface method, and then take a closer look at its method parameters, whether it is somewhat similar to the QueryInterface in C++. Haha, in fact, you can understand it as a managed implementation of QueryInterface. However, it also has a small function, that is, if it does not want to deal with the QI, it will return NotHandled. When clr sees the return value, it will call its own QI implementation to help you process the request.

Let's take a look at the flow chart of clr's internal processing of QI with this Interface:

What's New in .NET 4.0: ICustomQueryInterface

From this picture, we can see that except for the QI requests to IUnknown (not too demanding), all the other OK!

There are a lot of theories. Let's do some actual combat.

Look at the implementation of our managed components

1: using System

2: using System.Runtime.InteropServices

3:

4: namespace States

5: {

[Guid ("00020400-0000-0000-C000-0000001147")

InterfaceType (ComInterfaceType.InterfaceIsIUnknown)

8: public interface ICQ

9: {

10: int func ()

11: void slot2 ()

12: void slot3 ()

13: void slot4 ()

14:}

15:

[Guid ("11120400-0000-0000-C000-0000001148")

InterfaceType (ComInterfaceType.InterfaceIsIUnknown)

18: public interface IA

19: {

20: int FuncA ()

21:}

22:

[Guid ("22220400-0000-0000-C000-0000001149")

InterfaceType (ComInterfaceType.InterfaceIsIUnknown)

25: public interface IB

26: {

27: int FuncB ()

28:}

29:

30:

31:

[Guid ("00020400-0000-0000-C000-0000001150")

[ClassInterface (ClassInterfaceType.None)]

34: public class StatesComServer: ICustomQueryInterface, ICQ, IA, IB

35: {

36: public readonly Guid IID_IA = new Guid ("11120400-0000-0000-C000-0000001148")

37:

Public CustomQueryInterfaceResult GetInterface (In ref Guid iid, out IntPtr intf)

39: {

40: if (iid = = WellKnownGuids.IID_IDispatch)

41: {

42: intf = Marshal.GetComInterfaceForObject (this, typeof (ICQ), CustomQueryInterfaceMode.Ignore)

43: return CustomQueryInterfaceResult.Handled

44:}

45:

46: if (iid = = IID_IA)

47: {

48: intf = IntPtr.Zero

49: return CustomQueryInterfaceResult.Failed

50:}

51:

52: intf = IntPtr.Zero

53: return CustomQueryInterfaceResult.NotHandled

54:}

55:

56: public int func ()

57: {

58: Console.WriteLine ("This is Interface ICQ, not the IDIS patchy cards!")

59: return 2008

60:}

61:

62: public int FuncA ()

63: {

64: Console.WriteLine ("This is Interface IABG!")

65: return 3008

66:}

67:

68: public int FuncB ()

69: {

70: Console.WriteLine ("This is Interface IBDIQUE!")

71: return 4008

72:}

73:

74:

75: # region Empty Functions

76: public void slot2 () {}

77: public void slot3 () {}

78: public void slot4 () {}

79: # endregion

80:}

81:

82:}

83:

To explain a little bit, if the return value of GetInterface is CustomQueryInterfaceResult.Failed, it means that QI failed. CustomQueryInterfaceResult.NotHandled means to let clr handle the request. CustomQueryInterfaceResult.Handled tells clr that it has been processed, and that the pointer value is saved in intf and returned directly to the user.

Let's take a look at our client.

IDispatch * pDisp = NULL; printf ("Scenario 1: QI IDispatch interface, Expected the Custom IDispatch interface\ n"); hresult = pUnknown- > QueryInterface (IID_IDispatch, (void**) & pDisp); UINT count = 0; hresult = pDisp- > GetTypeInfoCount (& count); printf ("Return value of GetTypeInfoCount is% d\ n", count); IA * pA = NULL; printf ("Scenario 2: QI IA interface, Expected failed\ n"); hresult = pUnknown- > QueryInterface (IID_IA, (void**) & hresult) If (FAILED (hresult)) {printf ("Failed to QI IA with error code% x\ n", count);} IB * pB = NULL; printf ("Scenario 3: QI IB interface interface, Expected the IB interface\ n"); hresult = pUnknown- > QueryInterface (IID_IB, (void**) & pB); long I = 0; hresult = pB- > FuncB (& I)

Let's take a look at our output.

Scenario 1: QI IDispatch interface, Expected the Custom IDispatch interface This is Interface ICQ, not the IDispatch!!! Return value of GetTypeInfoCount is 2008 Scenario 2: QI iA interface, Expected failed Failed to QI IA with error code 7d8 Scenario 3: QI IB interface interface, Expected the IB interface This is Interface IB!!! These are the new ICustomQueryInterface features of .NET 4.0, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report