PInvoke - [Out] vs out?

  • Thread starter Thread starter Tim Johnson
  • Start date Start date
T

Tim Johnson

I thought passing an int* to a C-function via P/Invoke ought to be done like
this:

static extern void MyFunc( [Out] Int32 myvalue);

Int32 val=0; //Init required to silence warning
MyFunc(val);

But this throws an exception at runtime. However this works:

static extern void MyFunc( out Int32 myvalue);

Int32 val; //Note no initialization required!
MyFunc(out val); //out required here too or won't compile!

What's the diff and when is one correct vs. the other?
 
AFAIK out is the same as [Out] ref. This allows you to get equivalent to out
functionality in VB.NET although quite frankly I don't see why you would
need it. Using out (as opposite to [Out]) is correct
 
So "out" is equivalent to "[Out] ref", not just "[Out]"?

--
Tim Johnson
High Point Software
www.high-point.com
(503) 312-8625

Alex Feinman said:
AFAIK out is the same as [Out] ref. This allows you to get equivalent to out
functionality in VB.NET although quite frankly I don't see why you would
need it. Using out (as opposite to [Out]) is correct

--
Alex Feinman
---
Visit http://www.opennetcf.org
Tim Johnson said:
I thought passing an int* to a C-function via P/Invoke ought to be done
like
this:

static extern void MyFunc( [Out] Int32 myvalue);

Int32 val=0; //Init required to silence warning
MyFunc(val);

But this throws an exception at runtime. However this works:

static extern void MyFunc( out Int32 myvalue);

Int32 val; //Note no initialization required!
MyFunc(out val); //out required here too or won't compile!

What's the diff and when is one correct vs. the other?

--
Tim Johnson
High Point Software
www.high-point.com
(503) 312-8625
 
yes

--
Alex Feinman
---
Visit http://www.opennetcf.org
Tim Johnson said:
So "out" is equivalent to "[Out] ref", not just "[Out]"?

--
Tim Johnson
High Point Software
www.high-point.com
(503) 312-8625

Alex Feinman said:
AFAIK out is the same as [Out] ref. This allows you to get equivalent to out
functionality in VB.NET although quite frankly I don't see why you would
need it. Using out (as opposite to [Out]) is correct

--
Alex Feinman
---
Visit http://www.opennetcf.org
Tim Johnson said:
I thought passing an int* to a C-function via P/Invoke ought to be done
like
this:

static extern void MyFunc( [Out] Int32 myvalue);

Int32 val=0; //Init required to silence warning
MyFunc(val);

But this throws an exception at runtime. However this works:

static extern void MyFunc( out Int32 myvalue);

Int32 val; //Note no initialization required!
MyFunc(out val); //out required here too or won't compile!

What's the diff and when is one correct vs. the other?

--
Tim Johnson
High Point Software
www.high-point.com
(503) 312-8625
 
Back
Top