473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

managed c++ and c#

I am porting code from c++ and would like to use managed c++ to turn it into
an assembly for c#. The c++ code uses templates heavily ( via custom STL
containers ) and from what I read so does managed c++. How can I expose a
templated class for use in C#, or is it just an imposibility?

Jason
Nov 17 '05 #1
2 1128
> How can I expose a
templated class for use in C#, or is it just an imposibility?

This problem here is that templated types must be resolved at compile time. Templated types in assemblies are not possible and it just wont work.

I have attached some code which demostrates how you can consume a C++ dll from a C# but by putting template over the function it wont work.
// C++ dll
// cl /clr /LD template.cpp

using namespace System;
//template<typename T>
extern "C" __declspec(dllexport) void func(int value)
{
Console::WriteLine("Entering func");
}

// t.cs
// csc t.cs

using System;
using System.Runtime.InteropServices;

public class A
{

[DllImport("template.dll")]
// template<typename T>
static extern void func(int value);

static void Main()
{
func(1);
}
}
"Jason" wrote:
I am porting code from c++ and would like to use managed c++ to turn it into
an assembly for c#. The c++ code uses templates heavily ( via custom STL
containers ) and from what I read so does managed c++. How can I expose a
templated class for use in C#, or is it just an imposibility?

Jason

Nov 17 '05 #2
One thing you could do is (if possible) specialise the templates - if you're
only using them for a small number of types this might work. It wouldn't
entirely negate the point of templates, since you might have loads of
internal logic that you only need to write once. We've used this technique
when exposing a clas that coped with various bit-depths of image, for
example - wrote wrappers that exposed it for 1-, 8- and 16-bit images.
Obviously this sort of thing won't work if you're trying to expose bits of
the STL or more general code. In that case, you might be best off sticking
to managed C++.

Steve

"kkhosla" <kk*****@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
How can I expose a
templated class for use in C#, or is it just an imposibility?

This problem here is that templated types must be resolved at compile

time. Templated types in assemblies are not possible and it just wont work.
I have attached some code which demostrates how you can consume a C++ dll from a C# but by putting template over the function it wont work. // C++ dll
// cl /clr /LD template.cpp

using namespace System;
//template<typename T>
extern "C" __declspec(dllexport) void func(int value)
{
Console::WriteLine("Entering func");
}

// t.cs
// csc t.cs

using System;
using System.Runtime.InteropServices;

public class A
{

[DllImport("template.dll")]
// template<typename T>
static extern void func(int value);

static void Main()
{
func(1);
}
}
"Jason" wrote:
I am porting code from c++ and would like to use managed c++ to turn it into an assembly for c#. The c++ code uses templates heavily ( via custom STL
containers ) and from what I read so does managed c++. How can I expose a templated class for use in C#, or is it just an imposibility?

Jason

Nov 17 '05 #3

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

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
1
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
2
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to...
2
by: asanford | last post by:
We use StackWalk(StackWalk64) from dbghelp.dll to walk our callstacksas needed, using the various Sym* methods (SymGetSymFromAddr, SymGetLineFromAddr) to resolve source file, function name, and...
4
by: William F. Kinsley | last post by:
My understanding is that when I re-compile a existing MFC application with the /clr switch, that the code generated is managed(with some exceptions) but that the data isn't, i.e. not garbage...
9
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for...
12
by: DaTurk | last post by:
Hi, I have a rather interesting problem. I have a unmanged c++ class which needs to communicate information to managed c++ via callbacks, with a layer of c# on top of the managed c++ ultimatley...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
8
by: Varangian | last post by:
Hello, was wondering of how to dispose of managed resources? or referencing every member of a class to null will release resources...? http://www.marcclifton.com/tabid/79/Default.aspx...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.