unmanaged & managed C++ questions

I

Ian

1. I want to be use mix unmanaged code with managed code. In particular,
the unmanaged code uses MFC CFile to perform file I/O. Can CFile be used
in unmanaged code that is mixes with managed code? I've tried this but
keep getting a system::exception is thrown with the message "External
component has thrown an exception" when the CFile instance is created.

2. How does the assignment operator check to see that the source and
destination objects are not the same in managed code? For example:

ref class A
{
A% operator=( A %a )
{
// if( this != &a ) // this line generates a compiler error.
{
...
}
}
};

The compile generates the error message "error C2446: '!=' : no conversion
from 'const CManaged' to 'CManaged ^const '" when the line "if( this !=
&a )" is uncommented. While this is to be expected, what is the proper way
to check if the source and destination objects are the same?

Ian
 
I

Ian

Ian said:
1. I want to be use mix unmanaged code with managed code. In particular,
the unmanaged code uses MFC CFile to perform file I/O. Can CFile be used
in unmanaged code that is mixes with managed code? I've tried this but
keep getting a system::exception is thrown with the message "External
component has thrown an exception" when the CFile instance is created.

I'll post the answer for any other .NET newbies who might be trying to do
the same thing. MFC cannot be called from a .NET application (which is what
I was trying to do) but .NET can be called from an MFC application.

Ian
 
B

Ben Voigt

Ian said:
1. I want to be use mix unmanaged code with managed code. In particular,
the unmanaged code uses MFC CFile to perform file I/O. Can CFile be used
in unmanaged code that is mixes with managed code? I've tried this but
keep getting a system::exception is thrown with the message "External
component has thrown an exception" when the CFile instance is created.

2. How does the assignment operator check to see that the source and
destination objects are not the same in managed code? For example:

ref class A
{
A% operator=( A %a )
{
// if( this != &a ) // this line generates a compiler
error.

According to http://msdn2.microsoft.com/en-us/library/hxad2z4x.aspx, you
don't need an '&' at all.

http://msdn2.microsoft.com/en-us/library/9zekbcac.aspx adds some more
confusion.

And http://msdn2.microsoft.com/en-us/library/ms177191.aspx unambiguously
states that you should use % to get a tracking handle, not &.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top