c# - Is there any point in declaring arguments as [In] when PInvoking? -
when using platform invokes, specifying arguments [in]
make difference in how runtime approaches these?
for instance, when pinvoking createremotethread
, lpthreadid
specified out
per article on msdn:
handle winapi createremotethread( _in_ handle hprocess, _in_ lpsecurity_attributes lpthreadattributes, _in_ size_t dwstacksize, _in_ lpthread_start_routine lpstartaddress, _in_ lpvoid lpparameter, _in_ dword dwcreationflags, _out_ lpdword lpthreadid );
and other arguments _in_
. in c# code, handle specific function so:
[dllimport("kernel32.dll", entrypoint = "createremotethread", setlasterror = true)] public static extern intptr createremotethread( intptr hprocess, intptr lpthreadattributes, uint dwstacksize, intptr lpstartaddress, intptr lpparameter, uint dwcreationflags, [out] intptr lpthreadid);
adding [out]
attribute lpthreadid
runtime knows marshal caller. runtime handle function different if i'd change function signature this:
[dllimport("kernel32.dll", entrypoint = "createremotethread", setlasterror = true)] public static extern intptr createremotethread( [in] intptr hprocess, [in] intptr lpthreadattributes, [in] uint dwstacksize, [in] intptr lpstartaddress, [in] intptr lpparameter, [in] uint dwcreationflags, [out] intptr lpthreadid);
or exact same thing; arguments considered [in]
default?
the [in]
, [out]
attributes important, tell framework whether or not needs concern marshaling parameter in particular direction. particularly useful things structs
expensive marshal. default framework assumes marshaling bidirectional, may have large performance impact.
update:
some of comments have brought fact primitive types marshaled [in]
default, correct. fact marshaling complicated gives onus being explicit possible in pinvoke declarations, leave little possible framework decide it supposed way.
Comments
Post a Comment