Routines in mixed (managed and native) assemblies

B

Bob Altman

Hi all,

I have an unmanaged DLL. The entry points in the DLL are declared via a
..def file (which is syntactically easier for me to deal with than the arcane
#pragma gibberish).

Now I want some of the functions in this DLL to interact with a managed DLL.
For performance reasons, I need to be absolutely certain where the
transitions are between unmanaged and managed code and vice versa. I've
stared long and hard at the MSDN documentation, but I haven't found a clear
description of this, so I figured I'd ask in this NG.

From what I've read, it looks like all I need to do is:

1. Add the /clr option to my project (Property Pages->Configuration
Properties->General, "Common Language Runtime Support" field)

2. Include "#using <mscorlib.dll>" after the precompiled header directive in
any files that contain functions that want to call into managed code. I
believe (but I'm not certain) that *only* these files will be compiled as
managed code (unless I fiddle with #pragma managed/unmanaged). Further, if
I include the name of a function defined in one of these files in the .def
file, an unmanaged entry point will be exported from the DLL. When that
entry point is called, the unmanaged caller will magically call into the
managed code.

3. I also need to make sure managed and unmanaged code use different
precompiled header files, or disable precompiled headers alltogether.

Is this all correct?
 
J

Jeffrey Tan[MSFT]

Hi Bob,

1. Instead of adding it to the entire project, add /clr only on those files
that contain functions that need to be managed. Put another way, you
should decide which functions need to be managed and ensure that only those
are built /clr.
2. No, any file compiled /clr will be managed, regardless of whether it
contains #using<mscorlib.dll>. You should avoid #pragma managed/unmanaged.
Your understanding of DLL exports is correct, with the caveat that
__clrcall functions (including functions containing managed types in the
signature) cannot be exported.
3. Yes. It's preferable to have them use different precompiled headers,
since it will improve build speed.

Actually, I was a bit confused with the "#pragma managed/unmanaged". Then,
by discussing with VC++ team, I was told that we may run into many
situations where these pragmas caused unexpected interactions with
templates and compiler generated functions. The primary use for these
pragmas, we found, was to control managed/native pathways to fix loader
lock problems. However, it turns out that separating code into /clr and
native compilands does the job a lot better.

Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffrey Tan[MSFT]

You are welcome.

If you need further help, please feel free to post, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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