4

Possible Duplicate:
What is the purpose of __in __out __in_opt __allowed(), how do they work? Should I use similar constructs in my own code?

I saw a code in C in the following:

DWORD  GetFileVer(__in LPTSTR FileName, __out LPTSTR lpVersion, __in DWORD nSize)
{
...
}

I tried to compile it but failed. Could anyone tell me what does __in and __out mean? I consulted MSDN, but couldn't find any useful information. Thanks.

Community
  • 1
  • 1
zijuexiansheng
  • 337
  • 3
  • 14
  • IIRC, it's an annotation thing that helps with their static code analyzer. – Mysticial Sep 04 '12 at 01:35
  • Yeah, you're right. It's duplicated, and that one is much more detailed. Thanks. I just didn't find it when I searched for __in __out. Maybe the underscores were just ignored by the search engine. – zijuexiansheng Sep 04 '12 at 01:47

2 Answers2

6

__in and __out are a part of Microsoft's source code annotation language, or SAL.

By using source code annotations, you can make the intent behind your code explicit. These annotations also enable automated static analysis tools to analyze your code more accurately, with significantly fewer false positives and false negatives.

They are used by Microsoft tools to analyze your code for simple errors. You can see most of the simple primitive annotations used in headers here:

  • _in

    The function reads from the buffer. The caller provides the buffer and initializes it.

...

  • _out

    The function writes to the buffer. If used on the return value or with _deref, the function provides the buffer and initializes it. Otherwise, the caller provides the buffer and the function initializes it.

More complex annotations can be read about here, which are what Windows 8 developers are advised to stick to.

As James pointed out, you can just use preprocessor definitions to ignore them:

#define _in 
#define _out

... and so on.

Community
  • 1
  • 1
obataku
  • 29,212
  • 3
  • 44
  • 57
  • Thank you very much. But James really gave me a quick and accurate answer. And I compiled it successfully this time. And thanks for your answer. Both of yours are really helpful. O(∩_∩)O~ – zijuexiansheng Sep 04 '12 at 02:01
1

They are there to tell the programmer how the parameters are used. __in means the parameter is read-only, i.e. any changes to it will not be seen by the caller, __out means that the parameter is filled by the function and the data is used by the caller. You can just do

#define __in

#define __out

to get your program to compile.

Read the documentation too.

James
  • 9,064
  • 3
  • 31
  • 49