Sorry for such silly question,
I forget to import System.Runtime.InteropServices.
I search for sites and still dunno what is the meaning of PreserveSig
and MarshalAs.
Does anyone can explain whats that.
Thank you so much.
Sure...
MarshalAs is a hint to the marshaling engine about what type is expected. For
example, it is a mapping so to speak between the native .net type and the
native data type of the called assembly.
PreserveSig tells the runtime to ignore normal HRESULT, retval behavior. The
real return value of a COM function is an HRESULT. All other return values
are returned via a [out, retval] parameter... So, a function that looked like
this in COM:
HRESULT DoCoolStuff ([in] int i, [out, retval] int* j);
would translate into:
Function DoCoolStuff (ByVal i As Integer) As Integer
That's well and good - unless your function can return a success value other
then S_OK.... See, there are times when you can have COM functions that can
return other values that still represent success. So, you want to capture
that (and sometimes it is important)... But, the problem is if the function
returns anything other then S_OK the .net runtime will discard the [out,
retval] parameter and throw an exception based of the HRESULT (some HRESULTS
map to native .NET exceptions - some don't, in that case you get a
COMException). PreserveSig prevents this behavior. By adding PreserveSig you
can then get the actual HRESULT value for inspection... So, your above
function is now something like:
Function DoCoolStuff (ByVal i As Integer, <Out> ByRef j) As Integer
HTH,