> http://msdn2.microsoft.com/en-us/library/0adb9zxe.aspx
>
> Use the following guidelines when applying these pragmas:
>
> Add the pragma preceding a function but not within a function
> body.
This is because any function is either native or managed, you can't mix.
(However a template function might be able to create some native
instantiations and some managed instantiations from the same source, but
there still is no mixing).
> Add the pragma after #include statements (do not use these
> pragmas before #include statements).
Ideally the header files should contain the pragma managed push/pop, this
helps ensure that any particular declaration has the same pragma in effect
in every compilation unit.
For purely native third-party headers, rather than modify the third-party
headers, you might have to place the pragma before a #include. To help
ensure this is done consistently, create a wrapper header file that sets
pragmas correctly (including managed, packing, etc), then includes the
unmodified third-party header.
> Also in the book Expert C++/CLI by Marcus Heege P170
>
> He basically recommends that you don't do it.
In some situations it may be unavoidable. In other cases it may help work
around a compiler bug.
ajtaylor@hushmail.com - 14 Feb 2008 17:04 GMT
> >http://msdn2.microsoft.com/en-us/library/0adb9zxe.aspx
>
[quoted text clipped - 28 lines]
> In some situations it may be unavoidable. In other cases it may help work
> around a compiler bug.
Thanks for your reply.
What I don't understand is I have created two assemblies and they both
follow a simple design
ManagedClass contains an unmanaged ptr to an unmanaged class. This
managed class provides a managed interface to some unmanaged code
(this is compiled with /clr).
The unmanaged class has a simple interface and only uses std::string
over and above doubles/ints etc... In the implementation of this class
(in the cpp file) more complex unmanaged types are used (including
BOOST). This is all compiled as unmanaged C++
The Only #include in the unmanaged C++ class header file is #include
<string>
Now, I use one of these assemblies and all works fine. The other one
crashes with the
is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)
error.
The only way to solve this is to wrap the #include of the unmanaged
header with the pragmas in the managed class implementation file.
ManagedClass.cpp
#pragma unmanaged
#include "UnManaged.h"
#pragma managed
Fair enough if this is the way you have to do it - but
a) Its inconsistent the fact that one assembly works and the other
doesn't despite being almost identical in code (and certainly
identical in design)
b) I though IJW should "just work" I can see how including a
unmanaged c++ header that #includes a std::string should cause runtime
crashes unless I use the #pragrmas.
I am finding C++/CLI development incredibly frustrating to say the
least.
Many thanks.
A
Tamas Demjen - 16 Feb 2008 01:32 GMT
> The only way to solve this is to wrap the #include of the unmanaged
> header with the pragmas in the managed class implementation file.
[quoted text clipped - 4 lines]
> #include "UnManaged.h"
> #pragma managed
If UnManaged.h is your own source code (not 3rd party), I recommend that
you put the following there:
#ifdef _MANAGED
#pragma managed(push, off)
#endif
#include ... // your unmanaged includes
class ... // your unmanaged classes
#ifdef _MANAGED
#pragma managed(pop)
#endif
This is what Ben meant by "pragma managed push/pop".
This way you can simply #include "UnManaged.h" from managed code.
There are many subtle details when you mix managed and unmanaged code.
Here's one more thing that you should watch out for:
http://tinyurl.com/ytlyqd
Tom
ajtaylor@hushmail.com - 18 Feb 2008 09:33 GMT
> ajtay...@hushmail.com wrote:
> > The only way to solve this is to wrap the #include of the unmanaged
[quoted text clipped - 29 lines]
>
> Tom
Thanks for the reply. I will add that to my code and see if that cures
the inconsistencies I am seeing.
BTW: I think some of my issues are stemming from using BOOST in my
code and it seems that I only started seeing these issues when I went
to SP1 in VS2005.