Why don't Direct2D and DirectWrite use traditional COM objects?

I play with a small 2D engine in C # and decided to use Direct2D and DirectWrite for rendering. I know that these are Windows Code Code Pack and SlimDX, but I would really like to delve into and write the interface from scratch. I am trying to do this without Managed C ++, but Direct2D and DirectWrite do not use traditional COM objects. They define the interfaces that are derived from IUnknown , but there seems to be no way to actually use them from C # with COM interoperability. There are IIDs in d2d1.h, but not CLSID.

Of course, I'm really new to COM interoperability, so maybe I just missed something. Can someone shed light on this situation?

+4
source share
3 answers

Got it. You cannot create objects directly, but d2d1.dll contains an export named D2D1CreateFactory , which returns the ID2DFactory interface. Using this, you can implement a Direct2D shell without managed C ++.

 public static class Direct2D1Util { public const string IID_ID2D1Factory = "06152247-6f50-465a-9245-118bfd3b6007"; [DllImport("d2d1.dll", PreserveSig = false)] [return: MarshalAs(UnmanagedType.Interface)] private static extern object D2D1CreateFactory(D2D1_FACTORY_TYPE factoryType, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, D2D1_FACTORY_OPTIONS pFactoryOptions); public static ID2D1Factory CreateFactory() { D2D1_FACTORY_OPTIONS opts = new D2D1_FACTORY_OPTIONS(); object factory = D2D1CreateFactory( D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED, new Guid(IID_ID2D1Factory), opts); return (ID2D1Factory)factory; } } 

I will not include all types because they are all defined in d2d1.h , but here is how to use the code to get the factory:

 ID2DFactory factory = Direct2D1Util.CreateFactory(); 

Make sure your ID2DFactory interface has the appropriate Guid and InterfaceType(ComInterfaceType.InterfaceIsIUnknown) attributes InterfaceType(ComInterfaceType.InterfaceIsIUnknown) .

+7
source

The "Windows Code API for Microsoft® .NET Framework" manages libraries that allow access to Direct2D and DirectWrite.

http://code.msdn.microsoft.com/WindowsAPICodePack

Update

While DirectX wrappers are written in managed C ++, they create managed libraries. By the looks of things, they can be used with C #. There will be many examples included in the download that do just that, for example, using the managed C ++ Direct wrapper libraries from C #.

Using Windows 7 features in my own C # application would be very enjoyable, so I am currently installing the Window7 SDK (so that I can create a C ++ DirectX managed solution) and all this will give bash.

I follow the instructions:

http://blogs.msdn.com/msaleh/archive/2009/08/25/introducing-directx-features-of-windows-api-code-pack.aspx

Update 2

Following the instructions in the link above, I got compiled C ++ libraries, and you can really use them with C # very easily.

Now I'm not sure if I understand correctly, but you are just looking at some way to use Direct2D and DirectWrite from C #, or it should be COM or P \ Invoke.

I would really doubt that there would be a COM wrapper, since this is an inconsistent technology. COM is not well designed for the types of high-frequency method call patterns that you get in graphical programming — it's too slow.

If you really want to run P \ Invoke, you can always look at DirectX header files (on the Windows 7 SDK) and use the tool to create all your P \ Invoke stubs, for example.

http://www.pinvoker.com/

This would probably be a great PITA, although for developing wrappers this way.

+6
source

There is a codeplex project that provides managed wrappers for Direct2D. I have no idea if this is good, but most likely the source code will help you get started.

http://direct2dsharp.codeplex.com/

+2
source

Source: https://habr.com/ru/post/1309825/


All Articles