From: Tony Johansson on 29 Apr 2010 08:42 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
From: Alberto Poblacion on 29 Apr 2010 09:40 "Tony Johansson" <johansson.andersson(a)telia.com> wrote in message news:%23y4Bal55KHA.1888(a)TK2MSFTNGP05.phx.gbl... > 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.
From: Tony Johansson on 29 Apr 2010 10:51 "Alberto Poblacion" <earthling-quitaestoparacontestar(a)poblacion.org> skrev i meddelandet news:uGWODG65KHA.1888(a)TK2MSFTNGP05.phx.gbl... > "Tony Johansson" <johansson.andersson(a)telia.com> wrote in message > news:%23y4Bal55KHA.1888(a)TK2MSFTNGP05.phx.gbl... >> 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
From: Tony Johansson on 29 Apr 2010 10:52 "Alberto Poblacion" <earthling-quitaestoparacontestar(a)poblacion.org> skrev i meddelandet news:uGWODG65KHA.1888(a)TK2MSFTNGP05.phx.gbl... > "Tony Johansson" <johansson.andersson(a)telia.com> wrote in message > news:%23y4Bal55KHA.1888(a)TK2MSFTNGP05.phx.gbl... >> 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
From: Peter Duniho on 29 Apr 2010 11:30 Tony Johansson wrote: > 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
|
Pages: 1 Prev: calling unmanaged code in Win32 API Next: Data type enforcement in DataGridView |