Linking Mixed Mode and Managed Assemblies

I

ignhenry

I have a managed C++ project and two C# projects. All are class library
projects. The C++ project links with native C++ static libraries and
references to one C# project. The projects structure goes something like this.

Proj2_MCPP --(references)--> Proj1_CSharp
Proj3_CSharp --(references)--> Proj2_MCPP and Proj1_CSharp

My objective is to link the DLLs produced by the 3 projects into a single DLL.

I tried the following scenario.
1. csc Proj1_CSharp into a netmodule
2. cl Proj2_MCPP with /LN and /clr:blush:ldSyntax switch to produce .obj files
3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
as input
4. Link 1, 2 and 3 into a dll.

and I also tried this.
1. csc Proj1_CSharp into a netmodule
2. Build Proj2_MCPP from VS.NET 2005 (based on
http://support.microsoft.com/kb/309805) to produce .netmodule and obj .files.
3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
as input
4. Link 1, 2 and 3 into a dll.

I never managed to reach step 4. I just stuck with error messages resulted
from step 3.

Am I doing the right thing? Or it is just not possible?

Thanks - Henry
 
I

ignhenry

Thanks, Peter. I read somewhere that ILMerge, and Al.exe, only work for pure
managed assemblies, but don't support mixed mode DLLs containing both managed
and unmanaged code. I dropped them from my list.

Right now I am looking at using CSharp compiler and VC++ linker. I've seen
in internet newsgroups some people got it working successfully. My CSharp
compiler is complaining that some classes are defined multiple times.
Obviously, I am still missing something - probably simple.

-- Henry

Peter Ritchie said:
Class library projects are DLLs. You can't directly link DLLs together into
a new DLL.

You could try a utility called ILMerge [1]; but I haven't tried it with
mixed-mode DLLs.

[1] http://research.microsoft.com/~mbarnett/ILMerge.aspx

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


ignhenry said:
I have a managed C++ project and two C# projects. All are class library
projects. The C++ project links with native C++ static libraries and
references to one C# project. The projects structure goes something like this.

Proj2_MCPP --(references)--> Proj1_CSharp
Proj3_CSharp --(references)--> Proj2_MCPP and Proj1_CSharp

My objective is to link the DLLs produced by the 3 projects into a single DLL.

I tried the following scenario.
1. csc Proj1_CSharp into a netmodule
2. cl Proj2_MCPP with /LN and /clr:blush:ldSyntax switch to produce .obj files
3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
as input
4. Link 1, 2 and 3 into a dll.

and I also tried this.
1. csc Proj1_CSharp into a netmodule
2. Build Proj2_MCPP from VS.NET 2005 (based on
http://support.microsoft.com/kb/309805) to produce .netmodule and obj .files.
3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
as input
4. Link 1, 2 and 3 into a dll.

I never managed to reach step 4. I just stuck with error messages resulted
from step 3.

Am I doing the right thing? Or it is just not possible?

Thanks - Henry
 
P

Peter Ritchie [C# MVP]

Visual Studio doesn't support them, but compiling to netmodules could then be
linked together as an assembly using Assembly Linker (al.exe).

I'm not sure how to get c++ compiler to generate netmodules...

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


ignhenry said:
Thanks, Peter. I read somewhere that ILMerge, and Al.exe, only work for pure
managed assemblies, but don't support mixed mode DLLs containing both managed
and unmanaged code. I dropped them from my list.

Right now I am looking at using CSharp compiler and VC++ linker. I've seen
in internet newsgroups some people got it working successfully. My CSharp
compiler is complaining that some classes are defined multiple times.
Obviously, I am still missing something - probably simple.

-- Henry

Peter Ritchie said:
Class library projects are DLLs. You can't directly link DLLs together into
a new DLL.

You could try a utility called ILMerge [1]; but I haven't tried it with
mixed-mode DLLs.

[1] http://research.microsoft.com/~mbarnett/ILMerge.aspx

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


ignhenry said:
I have a managed C++ project and two C# projects. All are class library
projects. The C++ project links with native C++ static libraries and
references to one C# project. The projects structure goes something like this.

Proj2_MCPP --(references)--> Proj1_CSharp
Proj3_CSharp --(references)--> Proj2_MCPP and Proj1_CSharp

My objective is to link the DLLs produced by the 3 projects into a single DLL.

I tried the following scenario.
1. csc Proj1_CSharp into a netmodule
2. cl Proj2_MCPP with /LN and /clr:blush:ldSyntax switch to produce .obj files
3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
as input
4. Link 1, 2 and 3 into a dll.

and I also tried this.
1. csc Proj1_CSharp into a netmodule
2. Build Proj2_MCPP from VS.NET 2005 (based on
http://support.microsoft.com/kb/309805) to produce .netmodule and obj .files.
3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
as input
4. Link 1, 2 and 3 into a dll.

I never managed to reach step 4. I just stuck with error messages resulted
from step 3.

Am I doing the right thing? Or it is just not possible?

Thanks - Henry
 
P

Peter Ritchie [C# MVP]

Here's what I've been able to do:

csc /target:module cstest.cs
cl /clr:pure /FUSystem.dll /LN cpptest.cpp
al /platform:x86 /t:lib /out:test.dll cpptest.netmodule cstest.netmodule

This generates a dll (test.dll) that contains the two netmodules. When I
added to another C# project as a reference I was able to add managed types
from both netmodules.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


ignhenry said:
Thanks, Peter. I read somewhere that ILMerge, and Al.exe, only work for pure
managed assemblies, but don't support mixed mode DLLs containing both managed
and unmanaged code. I dropped them from my list.

Right now I am looking at using CSharp compiler and VC++ linker. I've seen
in internet newsgroups some people got it working successfully. My CSharp
compiler is complaining that some classes are defined multiple times.
Obviously, I am still missing something - probably simple.

-- Henry

Peter Ritchie said:
Class library projects are DLLs. You can't directly link DLLs together into
a new DLL.

You could try a utility called ILMerge [1]; but I haven't tried it with
mixed-mode DLLs.

[1] http://research.microsoft.com/~mbarnett/ILMerge.aspx

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


ignhenry said:
I have a managed C++ project and two C# projects. All are class library
projects. The C++ project links with native C++ static libraries and
references to one C# project. The projects structure goes something like this.

Proj2_MCPP --(references)--> Proj1_CSharp
Proj3_CSharp --(references)--> Proj2_MCPP and Proj1_CSharp

My objective is to link the DLLs produced by the 3 projects into a single DLL.

I tried the following scenario.
1. csc Proj1_CSharp into a netmodule
2. cl Proj2_MCPP with /LN and /clr:blush:ldSyntax switch to produce .obj files
3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
as input
4. Link 1, 2 and 3 into a dll.

and I also tried this.
1. csc Proj1_CSharp into a netmodule
2. Build Proj2_MCPP from VS.NET 2005 (based on
http://support.microsoft.com/kb/309805) to produce .netmodule and obj .files.
3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
as input
4. Link 1, 2 and 3 into a dll.

I never managed to reach step 4. I just stuck with error messages resulted
from step 3.

Am I doing the right thing? Or it is just not possible?

Thanks - Henry
 
J

Jeffrey Tan[MSFT]

Hi Henry,

I have compiled the following files in a single .NET assembly (which is not
the same as a single file as you know that a .NET assembly is a logical
grouping of .NET modules):

csc /t:module /out:proj1_CSharp.net Proj1_CSharp.cs
cl -clr -LN Proj2_MCPP.cpp
csc /t:module /addmodule:proj1_CSharp.net /addmodule:proj2_MCPP.netmodule
Proj3_CSharp.cs
al /out:single.dll /platform:x86 Proj1_CSharp.net Proj2_MCPP.netmodule
Proj3_CSharp.netmodule

Proj3_CSharp.cs:

public class Proj3_CSharp
{
public void f(Proj2_MCPP x, Proj1_CSharp y)
{
}
}

Proj2_MCPP.cpp:

#using "Proj1_CSharp.net"

public ref class Proj2_MCPP
{
public:
void g(Proj1_CSharp x) {}
};

Proj1_CSharp.cs:

public class Proj1_CSharp
{
public void h(){}
}

single.dll is a manifest-only file of a .NET assembly with 3 .NET modules
Proj1_CSharp.net, Proj2_MCPP.netmodule and Proj3_CSharp.netmodule.
Depending on what you have in the C++ code -most likely- you will need a
manifest file for the .EXE consuming types defined in the
Proj2_MCPP.netmodule.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
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.
 
I

ignhenry

Hi Jeffrey, thanks for the reply. I tried the examples and it works.

I finally managed to do the linking using VC++ linker. With it, I am able to
get smaller number of assemblies (One DLL and 2 .NET modules). I just can't
figure out how to achieve that with Al.exe (If you have any ideas, please let
me know).

In essence:

1. csc /out:proj1_CSharp.netmodule /target:module
/recurse:..\src\Proj1_CSharp\*.cs
2. cl /clr /LN /MD /D "WITH_ATLAS" ..\src\Proj2_MCPP\*.cpp libf77blas.lib
libatlas.lib /I "../External/ATLAS" /AI "../External/ATLAS" /link
/LIBPATH:"../External/ATLAS" /OUT:"Kernel.netmodule"
3. csc /addmodule:proj1_CSharp.netmodule;Kernel.netmodule
/out:Numeric.netmodule /target:module /recurse:..\src\Numeric\*.cs
4. link /LTCG /CLRIMAGETYPE:IJW /NOENTRY /DLL
/ASSEMBLYMODULE:proj1_CSharp.netmodule /ASSEMBLYMODULE:Kernel.netmodule
/OUT:proj3_CSharp.dll Numeric.netmodule /LIBPATH:"C:\Program Files\Microsoft
Visual Studio 8\VC\" /LIBPATH:"../External/ATLAS"

---> output 1 dll and 2 netmodules

Henry


"Jeffrey Tan[MSFT]" said:
Hi Henry,

I have compiled the following files in a single .NET assembly (which is not
the same as a single file as you know that a .NET assembly is a logical
grouping of .NET modules):

csc /t:module /out:proj1_CSharp.net Proj1_CSharp.cs
cl -clr -LN Proj2_MCPP.cpp
csc /t:module /addmodule:proj1_CSharp.net /addmodule:proj2_MCPP.netmodule
Proj3_CSharp.cs
al /out:single.dll /platform:x86 Proj1_CSharp.net Proj2_MCPP.netmodule
Proj3_CSharp.netmodule

Proj3_CSharp.cs:

public class Proj3_CSharp
{
public void f(Proj2_MCPP x, Proj1_CSharp y)
{
}
}

Proj2_MCPP.cpp:

#using "Proj1_CSharp.net"

public ref class Proj2_MCPP
{
public:
void g(Proj1_CSharp x) {}
};

Proj1_CSharp.cs:

public class Proj1_CSharp
{
public void h(){}
}

single.dll is a manifest-only file of a .NET assembly with 3 .NET modules
Proj1_CSharp.net, Proj2_MCPP.netmodule and Proj3_CSharp.netmodule.
Depending on what you have in the C++ code -most likely- you will need a
manifest file for the .EXE consuming types defined in the
Proj2_MCPP.netmodule.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
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]

Hi Henry,

Thanks for your feedback.

Oh, your original statement is correct. As our product manager Steve
confirmed in the article below, the al.exe/ilmerge approaches only work
well for purely managed scenarios, but they don't support linking native
code. This makes sense, since only the C++/CLI supports the single assembly
internal interop. We need the C++/CLI link.exe to get this done.

Steve demonstrated this walkthrough in the article below(which is similar
as what you have done):
"Linking native C++ into C# applications"
http://blogs.msdn.com/texblog/archive/2007/04/05/linking-native-c-into-c-app
lications.aspx

Does this meet your need? If not, please feel free to tell me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

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

Jeffrey Tan[MSFT]

Hi Henry,

Have you reviewed my last reply to you? Does using link.exe following the
steps in that MSDN blog meet your need? If you still need any help or have
any concern, please feel free to tell me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

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

ignhenry

Hi Jeffrey

Sorry for late reply.

Steve's article indeed helped me solve the problem.

Thanks!
 
I

ignhenry

Hi Jeffrey

Sorry for late reply.

Steve's article indeed helped me solve the problem.

Thanks!
 
J

Jeffrey Tan[MSFT]

Hi Henry,

Thank you for the confirmation.

Ok, if you need any further help, please feel free to post, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

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