Mixed question: Should I wrap unmanaged headers with #pragmas?

G

Guest

I have an older MFC C++ app with some new Managed C++ being thrown in. Recently I turned on the /clr flag for one of my old .cpp files. I then placed '#pragma unmanaged' just below my #include statements near the top of the file. These headers contain older unmanaged class declarations.

When running I discovered a new bug, a crash, at a point in execution that was nowhere near any code that's been touched in quite a while. After hunting down logical code paths with no luck finding the problem, I tried turning off the /clr flag and that made the problem go away. I will need /clr later, so I want to get to the bottom of this bug.

I tried moving the #pragma unmanaged to the very top of the file, but it then refused to compile stating:
vcclr.h(23) : error C3821: 'const unsigned char': managed type cannot be used in an unmanaged function.

So next I tried moving the #pragma unmanaged just below the call to #include stdafx.h, which was the first file included. This compiled. (As an aside, can anyone explain why it matters if stdafx.h is called after #pragma managed or unmanaged?) Anyway, it compiled so I tried running and TADAAA! The program ran smoothly.

Now the big question. Do I actually need to consciously consider my current managed/unmanaged context when including my headers? What in particular could be in a header file that would cause my runtime execution to break under these circumstances? Or did I only hide a bug that was previously hidden but lately uncovered with /clr compilation?

Any pointers?

thanks
knormand
 
S

Sin

As an aside, can anyone explain why it matters if stdafx.h is called
after #pragma managed or unmanaged?

Can't comment on your other questions but I can tell you that #include
<stdafx.h> needs to be the first line in your file when precompiled headers
are used. Think of it as a compiler limitation. No idea what the real reason
is, but the compiler could at least tell us stdafx.h is not included on the
first line rather than babling unrelated errors. Anyways... I don't use
precompiled headers anymore and things have been running smootly ever
since... lol

Alex.
 

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