473,506 Members | 14,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linking Mixed Mode and Managed Assemblies

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:oldSyntax 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
Jun 27 '08 #1
11 2314
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" wrote:
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:oldSyntax 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
Jun 27 '08 #2
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 [C# MVP]" wrote:
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" wrote:
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:oldSyntax 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
Jun 27 '08 #3
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" wrote:
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 [C# MVP]" wrote:
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" wrote:
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:oldSyntax 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
Jun 27 '08 #4
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" wrote:
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 [C# MVP]" wrote:
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" wrote:
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:oldSyntax 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
Jun 27 '08 #5
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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '08 #6
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]"" wrote:
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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '08 #7
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/archiv...e-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:
ms****@microsoft.com.

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

Jun 27 '08 #8
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:
ms****@microsoft.com.

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

Jun 27 '08 #9
Hi Jeffrey

Sorry for late reply.

Steve's article indeed helped me solve the problem.

Thanks!
""Jeffrey Tan[MSFT]"" wrote:
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/archiv...e-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:
ms****@microsoft.com.

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

Jun 27 '08 #10
Hi Jeffrey

Sorry for late reply.

Steve's article indeed helped me solve the problem.

Thanks!

""Jeffrey Tan[MSFT]"" wrote:
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/archiv...e-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:
ms****@microsoft.com.

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

Jun 27 '08 #11
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:
ms****@microsoft.com.

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

Jun 27 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2569
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
8
3485
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
8
1971
by: Nadav | last post by:
Hi, I am writing a performence critical application, this require me to stick to unmanaged C++ as performance is much better using unmanaged C++ ( about 33% better ), Still, I am trying to avoid...
0
1051
by: Drew | last post by:
I am attempting to clean up some code by breaking a large mixed-mode Managed C++ library into two. I cannot get the darn thing to compile or link (depending on what I do). I have two...
7
7379
by: Lee Crabtree | last post by:
I'm starting work on what will eventually be a very, very LARGE project. A lot of the project involves taking C/C++ class libraries and wrapping them with managed C++. I'd like to minimize the...
7
2095
by: Boni | last post by:
Dear all, I have a global variable in the native code. I was assuming that I create a IJW CLI wrapper class and create an instance of this class in other appdomain, then the global variables will...
2
1495
by: Ubergeek | last post by:
I recently came accross an article that stated that there were unresolved issued with calling native (i.e. "unmanaged" C++) from managed C++. Does anyone know what the precise details of this...
8
2299
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR...
0
2982
by: emu | last post by:
Hi All, I have an unmanaged C++ application that references a mixed mode image DLL (mixed managed and unmanaged). Under .NET 1.1 we could trust the dll (the mixed mode dll) by running the...
5
2783
by: =?Utf-8?B?aWduaGVucnk=?= | last post by:
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...
0
7105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.