1
typedef struct DXGI_SAMPLE_DESC {
         UINT Count;
         UINT Quality;
} DXGI_SAMPLE_DESC, *LPDXGI_SAMPLE_DESC;

So in the above structure definition for multisampling, what is the real reason to use typedef and repeating the same structure name. Also what about *LPDXGI_SAMPLE_DESC; Why the pointer? What does a typedef pointer signify?

lighthouse
  • 23
  • 3

2 Answers2

1

This is mainly due to how C handles struct types. Things work slightly different in C++, which is why you don't see this pattern as often anymore.

Mind you that DirectX is a COM-API and as such is specified in terms of C, even though it uses objects and classes.

The first mention of DXGI_SAMPLE_DESC is the tag name, the second is the name of the typedef.

Omitting the tag name has the main drawback that it prevents the type from being forward-declared. There is also a number of technical reasons related to how Microsoft's toolchain generates code for COM interfaces why you would not want to skip the tag name.

Omitting the typedef on the other hand would force you to write struct DXGI_SAMPLE_DESC instead of plain DXGI_SAMPLE_DESC whenever you use the type in C code.

The *LPDXGI_SAMPLE_DESC is a second typedef that refers to a pointer to DXGI_SAMPLE_DESC instead of the plain struct type itself. This is standard typedef syntax that is still the same in C++.

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • So to define a pointer for the struct I would simply need to write: LPDXGI_SAMPLE_DESC ptr_msDESC; Hence ptr_msDESC would be the ptr to the struct declaration? – lighthouse Jun 09 '17 at 16:27
  • @lighthouse Correct. The LP-prefix for the name here is actually Microsoft-style hungarian notation for pointer (LP=long pointer, but the distinction between long and near pointers [died with the switch from 16 to 32 bit](https://stackoverflow.com/questions/3869830/near-and-far-pointers)). – ComicSansMS Jun 09 '17 at 18:44
  • Oh ok so that clears up LP. It is everywhere in windows. Kept reading as is but now I understand the context. Thanks! Why do they not deprecate the old notation when everything is long ptr? – lighthouse Jun 09 '17 at 19:24
  • @lighthouse Historical reasons. They did not want to break existing code back when moving from 16 to 32 bit and now they are probably stuck with it forever. The Windows API is full of historical quirks like this, and this one is definitely one of the more harmless ones. – ComicSansMS Jun 10 '17 at 11:12
  • Ah ok. I was quite dumb founded when I first saw LPCTSR... but yea its harmless once you can read it. Just a matter of notations in the end it seems. Thanks for the clear up mate! – lighthouse Jun 10 '17 at 16:18
0

If you use C++ this typedef doesn't matter for you. In C you would like to have it to avoid writing something like this void foo(struct S s);

Pointer is needed to allow use LPDXGI_SAMPLE_DESC in place you need pointer to this struct, e.g. in function arguments.

Yola
  • 18,496
  • 11
  • 65
  • 106