Calling unmanaged Win32 API from managed code

T

Tony Johansson

Hi!

Here is an example from MSDN
I have two questions concering this text.
1. Why do they have brackets around some out prefix parameter. Is [Out]
lpBuffer the same as out lpBuffer ?
2. They also have brackets around In like this [In]. What does that mean ?

Class Backup
{
[DllImport("kernel32.dll")]
static extern bool BackupRead(IntPtr hFile, [Out] byte[] lpBuffer,
uint nNumberOfBytesToRead, out uint
lpNumberOfBytesRead, bool bAbort, bool bProcessSecurity, out
IntPtr lpContext);

[DllImport("kernel32.dll")]
static extern bool WriteFile(IntPtr hFile, byte[] lpBuffer, uint
nNumberOfBytesToWrite,
out uint lpNumberOfBytesWritten, [In] ref
System.Threading.NativeOverlapped lpOverlapped);
}

//Tony
 
A

Alberto Poblacion

Tony Johansson said:
1. Why do they have brackets around some out prefix parameter. Is [Out]
lpBuffer the same as out lpBuffer ?

No, it is not the same. [Out] is an attribute, namely
System.Runtime.InteropServices.OutAttribute. It generates metadata that
informs the marshaller that the value needs to be sent back to the caller.
On the other hand, "out" is a keyword that informs the compiler that it
should pass the argument by reference and verify that the called routine
returns a value in that parameter.
2. They also have brackets around In like this [In]. What does that mean ?

Simimarly, [In] refers to InAttribute, which indicates that data should
be marshaled from the caller to the callee, but not back to the caller.
 
T

Tony Johansson

Alberto Poblacion said:
Tony Johansson said:
1. Why do they have brackets around some out prefix parameter. Is [Out]
lpBuffer the same as out lpBuffer ?

No, it is not the same. [Out] is an attribute, namely
System.Runtime.InteropServices.OutAttribute. It generates metadata that
informs the marshaller that the value needs to be sent back to the caller.
On the other hand, "out" is a keyword that informs the compiler that it
should pass the argument by reference and verify that the called routine
returns a value in that parameter.
2. They also have brackets around In like this [In]. What does that mean
?

Simimarly, [In] refers to InAttribute, which indicates that data should
be marshaled from the caller to the callee, but not back to the caller.

What happen if I don't use these In and Out attribute ?
When should I use them. I looked in the docs and I can't find any
explanation when to use these special
In and Out attribute ?

//Tony
 
T

Tony Johansson

Alberto Poblacion said:
Tony Johansson said:
1. Why do they have brackets around some out prefix parameter. Is [Out]
lpBuffer the same as out lpBuffer ?

No, it is not the same. [Out] is an attribute, namely
System.Runtime.InteropServices.OutAttribute. It generates metadata that
informs the marshaller that the value needs to be sent back to the caller.
On the other hand, "out" is a keyword that informs the compiler that it
should pass the argument by reference and verify that the called routine
returns a value in that parameter.
2. They also have brackets around In like this [In]. What does that mean
?

Simimarly, [In] refers to InAttribute, which indicates that data should
be marshaled from the caller to the callee, but not back to the caller.

What happen if I don't use these In and Out attribute ?
When should I use them. I looked in the docs and I can't find any
explanation when to use these special
In and Out attribute ?

//Tony
 
P

Peter Duniho

Tony said:
What happen if I don't use these In and Out attribute ?
When should I use them. I looked in the docs and I can't find any
explanation when to use these special
In and Out attribute ?

If you don't provide those attributes, then depending on the declaration
of the method signature, the marshaler may fail to marshal data back
from the method (if the parameter is not passed by reference but is
still a pointer type of some sort) or it may marshal the data back from
the method when it didn't need to (if the parameter is passed by
reference but is really only an input parameter).

IMHO, you should use them always when they apply. Every bit of
information you can provide to the marshaler for p/invoke is useful.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top