\CLR Mixed mode compilation woes!

H

Herby

Iv compiled my current C++ project as \clr as i want to start putting
in some specific C++\CLI code to serialize my MFC objects to .NET
equivalents.
The program crashes on startup, something to do with the stack frame
being corrupted.

My first concern is the program has ballooned in terms of the size of
the executable image.
When it attempts to run it fails and some report about stack frame
corruption.

Iv noticed any C files have to be converted to C++.

The program links to a DLL and Libs that have been compiled as C.

Is this a problem when compiling a program as /CLR that then links with
C libs etc and on startup loads C Dlls?

How can i specifically instruct the compiler to only compiler specific
functions/methods as managed(mixed mode) and to leave everything else
as native?
Is this possible?

Thanks in advance.
 
N

Nishant Sivakumar

Have you first tried just compiling and running it successfully in VC++ 2005
(without /clr turned on) ? Does it work okay then?
 
C

Carl Daniel [VC++ MVP]

Herby said:
Iv compiled my current C++ project as \clr as i want to start putting
in some specific C++\CLI code to serialize my MFC objects to .NET
equivalents.
The program crashes on startup, something to do with the stack frame
being corrupted.

My first concern is the program has ballooned in terms of the size of
the executable image.
When it attempts to run it fails and some report about stack frame
corruption.

You should be specific: exactly how is it failing, exactly what is reported?
Iv noticed any C files have to be converted to C++.

Yes. You cannot compile C as managed code.
The program links to a DLL and Libs that have been compiled as C.

That's fine.
Is this a problem when compiling a program as /CLR that then links with
C libs etc and on startup loads C Dlls?

No, there's no problem with that. Consider that the operating system itself
is C and is exposed through DLLs...
How can i specifically instruct the compiler to only compiler specific
functions/methods as managed(mixed mode) and to leave everything else
as native?

See #pragma managed and #pragma unmanaged. You also can throw the /clr
compiler option only on files that contain managed code and continue to
compile everything else as native code. That might save you from having to
insert #pragma unmanaged a bunch of places.

Note that header files that define declarations that will be shared between
native and managed code should generally be written as:

// MySharedHeader.h
#pragma once
#pragma managed(push, off)

// your declarations here

#pragma managed(pop)

This will ensure that the declarations in the shared header are always seen
as unmanaged regardless of how the compilation unit the references the
header file is compiled.

Note also that preprocessor macros (#define) are neither managed nor
unmanaged - they're simply text substitutions, so they'll work identically
in either environment.

-cd
 
H

Herby

Ok, Iv not actually added anything managed yet.
Im just trying to compile it with the \CLR option.
I have now switched off clr for each source file but left it on for the
project.

One day this will all be 2nd nature i hope - must just persevere :)

Im much more a software developer than a compiler tweaking geek! :)

And it now works!

So ill call it a day there! and dream about how i can begin to add some
new managed code.

Once again, thanks alot for all your help!
 
H

Herby

Ok, have moved on, but having problems with shared headers.
Have added #pragmas as described above, but still throwing out
errors...

e.g.

// constants.h
#pragma once
#pragma managed(push,off)

extern BOOL bIsOLE;

#pragma managed(pop)

-----------------------------------------------------------------------------

//Convert2DotNet.cpp // compiled with /clr
// Have taken out pre-compiled header here stdafx.h
#include "constants.h"

bool Convert2DotNet(void)
{
return true;
}

-----------------------------------------------
When compiling get many errors including
c:\dev.2005.net\designtime\common\constants.h(24) : error C2146: syntax
error : missing ';' before identifier 'bIsOLE'

Can anyone help?

Thanks.
 
H

Herby

I think that one is simply because i had excluded stdafx.h and this
file includes all the system dependencies etc, which go on to define
BOOL.
I cannot change the system headers - so how am i to proceed on this
one?
 
C

Carl Daniel [VC++ MVP]

Herby said:
I think that one is simply because i had excluded stdafx.h and this
file includes all the system dependencies etc, which go on to define
BOOL.
I cannot change the system headers - so how am i to proceed on this
one?

The error indicates that BOOL wasn't defined before you used it in
constants.h. Based on what you posted, the error is correct.

The following compiles without error for me:

<code>
#pragma managed(push,off)

#include <windows.h>

extern BOOL IsOle;

#pragma managed(pop)

bool f()
{
return IsOle;
}
</code>

compile with cl -c -clr

-cd
 
H

Herby

Thanks Carl, Iv been through so much to get this far and this would
appear to be my last hurdle, and im worried...

My problem now is im going to be referencing my unmanaged classes that
derive from the MFC CObject.
So I need something like:

<code>
#pragma managed(push, off)

#include <afx.h> // MFC core and standard components
#include "constants.h"

#pragma managed(pop)



bool Convert2DotNet(void)
{
return true;
}
</code>

When I now compile this i get the following errors...

C:\Program Files\Microsoft Visual Studio 8\VC\include\vcclr.h(42) :
error C3821: 'cli::interior_ptr<Type>': managed type or function cannot
be used in an unmanaged function
with
[
Type=unsigned char
]
C:\Program Files\Microsoft Visual Studio 8\VC\include\vcclr.h(42) :
error C3821: 's': managed type or function cannot be used in an
unmanaged function


Headers can still be a real headache it seems...

Can i resolve this?

Thanks.
 
H

Herby

Can anyone help me on this?
it has become a real show stopper!

How can i reference umanaged types derived from the MFC CObject within
a managed module?

I thought this was a big selling point of the mixed mode approach?
 
H

Herby

Yes I can help myself. I 'managed' to do it.

<code>
#include <afx.h> // MFC core and standard components
#include<afxtempl.h>

#pragma managed(push, off)
#include"map_dwo.h"
#include"RuntimeMap.h"
#include"Convert2DotNet.h"
#pragma managed(pop)

#using<Runtime2.DLL>

using namespace Runtime2;

bool Convert2DotNet(CRuntimeMap* pMap)
{
RuntimeMap^ pGcMap = gcnew RuntimeMap;
return true;
}
</code>

Had to exclude the system headers outside of the un-managed set.

All systems go!
 
H

Herby

Ok..
Given an unmanaged class is it possible for one of its methods to be
compiled as managed?

The reason being im copying member data from the source unmanaged
object to the new managed target object. I would like direct acces to
this source member data via one of its methods.
Ideally all my current un-managed classes that must take part in the
..NET converison will implement the Convert2DotNet method( preferably
via an iterface class, but lets keep it simple as possible for now)

E.g.

#pragma managed
CRuntimeMap::Convert2DotNet(void)
{
}

When i attempt to do this i get the following error

error C3295: '#pragma managed' can only be used at global or namespace
scope

Is there anyway to acheive this?
 

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