mixed-mode C++ and the Out attribute

  • Thread starter Thread starter Chris Ellis
  • Start date Start date
C

Chris Ellis

Hi,

I'm not sure if this is the right newsgroup for this question. I'm not
sure if the problem lies in C# or in C++, but I am working on a project that
uses a mixed-mode C++ dll to wrap some API's into classes. I am then using
those classes from a library in C#. In one of my C++ functions, I wanted to
use an out parameter. This is the code that I used to declare/define that
function:

// declaration
Stream* OpenOrCreateStream( String* Name,
[System::Runtime::InteropServices::Out] bool* bCreated );

// definition

StructuredStorage::Stream* StructuredStorage::Storage::OpenOrCreateStream(
String* Name, [System::Runtime::InteropServices::Out] bool* bCreated )
{
...
}

this compiles fine and the definition of the function as shown by ildasm is
as follows:

..method public instance class StructuredStorage.Stream
OpenOrCreateStream(string Name,
[out] bool* bCreated) cil managed

now, in C#, I call that function in the following statemen:

Stream sProt = m_Database.OpenOrCreateStream( PROTECTION_STREAM, out
bCreated );

and that yields me the following error:

error CS1503: Argument '2': cannot convert from 'out bool' to 'bool*'

and when intellisense shows me the function arguments in C#, it shows:
string, bool*

I seem to be following the documentation on the Out attribute exactly and I
have no idea why this is causing me a problem. Currently, my only solution
is to enable unsafe code in C# and call the function passing the address of
a boolean variable, but I would far prefer to do it using the out parameter.
Does anyone see what I'm doing wrong or is this a known problem?

Please let me know if you need any more info. I provided everything I could
think of... oh.... I'm using .NET 1.1 in Visual Studio 2003.

Thanks in advance,
Chris
 
Jon,

Thank you. That did the trick. I'm honestly not certain why. I was
under the assumption that __gc related only to objects that are garbage
collected, but unless this is referring to a bool on the garbage collected
heap, it appears that my assumption was not correct.

Thanks again,
Chris

Jon said:
Try:

Stream* OpenOrCreateStream( String* Name,[Out] bool _gc* bCreated );

Hi,

I'm not sure if this is the right newsgroup for this question. I'm not
sure if the problem lies in C# or in C++, but I am working on a project that
uses a mixed-mode C++ dll to wrap some API's into classes. I am then using
those classes from a library in C#. In one of my C++ functions, I wanted to
use an out parameter. This is the code that I used to declare/define that
function:

// declaration
Stream* OpenOrCreateStream( String* Name,
[System::Runtime::InteropServices::Out] bool* bCreated );

// definition

StructuredStorage::Stream* StructuredStorage::Storage::OpenOrCreateStream(
String* Name, [System::Runtime::InteropServices::Out] bool* bCreated )
{
...
}

this compiles fine and the definition of the function as shown by ildasm is
as follows:

.method public instance class StructuredStorage.Stream
OpenOrCreateStream(string Name,
[out] bool* bCreated) cil managed

now, in C#, I call that function in the following statemen:

Stream sProt = m_Database.OpenOrCreateStream( PROTECTION_STREAM, out
bCreated );

and that yields me the following error:

error CS1503: Argument '2': cannot convert from 'out bool' to 'bool*'

and when intellisense shows me the function arguments in C#, it shows:
string, bool*

I seem to be following the documentation on the Out attribute exactly and I
have no idea why this is causing me a problem. Currently, my only solution
is to enable unsafe code in C# and call the function passing the address of
a boolean variable, but I would far prefer to do it using the out parameter.
Does anyone see what I'm doing wrong or is this a known problem?

Please let me know if you need any more info. I provided everything I could
think of... oh.... I'm using .NET 1.1 in Visual Studio 2003.

Thanks in advance,
Chris
 
It's more accurate to say that __gc relates to objects that *may* be on the
garbage collected heap. (For example, consider the case where you have a
bool inside a __gc class -- a pointer to that bool will point to a location
under garbage collector control -- so having a __nogc * for this is
absolutely forbidden.) In fact there is an implicit conversion from a __nogc
pointer to a __value type to a __gc pointer to the __value type -- i.e., if
I have a bool __nogc *, then I can pass it to your routine expecting a bool
__gc *.

There's useful info on this here:
<ms-help://MS.MSDNQTR.2003FEB.1033/vcmxspec/html/vcManagedExtensionsSpec_7_2
..htm>


Chris Ellis said:
Jon,

Thank you. That did the trick. I'm honestly not certain why. I was
under the assumption that __gc related only to objects that are garbage
collected, but unless this is referring to a bool on the garbage collected
heap, it appears that my assumption was not correct.

Thanks again,
Chris

Jon said:
Try:

Stream* OpenOrCreateStream( String* Name,[Out] bool _gc* bCreated );

Hi,

I'm not sure if this is the right newsgroup for this question.
I'm
project
that
uses a mixed-mode C++ dll to wrap some API's into classes. I am then using
those classes from a library in C#. In one of my C++ functions, I wanted to
use an out parameter. This is the code that I used to declare/define that
function:

// declaration
Stream* OpenOrCreateStream( String* Name,
[System::Runtime::InteropServices::Out] bool* bCreated );

// definition

StructuredStorage::Stream* StructuredStorage::Storage::OpenOrCreateStream(
String* Name, [System::Runtime::InteropServices::Out] bool* bCreated )
{
...
}

this compiles fine and the definition of the function as shown by
ildasm
is
as follows:

.method public instance class StructuredStorage.Stream
OpenOrCreateStream(string Name,
[out] bool* bCreated) cil managed

now, in C#, I call that function in the following statemen:

Stream sProt = m_Database.OpenOrCreateStream( PROTECTION_STREAM, out
bCreated );

and that yields me the following error:

error CS1503: Argument '2': cannot convert from 'out bool' to 'bool*'

and when intellisense shows me the function arguments in C#, it shows:
string, bool*

I seem to be following the documentation on the Out attribute exactly and I
have no idea why this is causing me a problem. Currently, my only solution
is to enable unsafe code in C# and call the function passing the
address
 
Back
Top