Passing a SortedList from C# to MC++

A

abintom

hi,

i am writing a dll in MC++ for using from C#. For the dll function say
'TestFunc', i need to get a SortedList<string,string> as an argument.
I am not very clear about using SortedList in MC++. Please guide me on
this.

thanks in advance,
Abin
 
W

Willy Denoyette [MVP]

| hi,
|
| i am writing a dll in MC++ for using from C#. For the dll function say
| 'TestFunc', i need to get a SortedList<string,string> as an argument.
| I am not very clear about using SortedList in MC++. Please guide me on
| this.
|
| thanks in advance,
| Abin
|

You can't pass a generic type to a MC++ function, generics require C++/CLI.

Willy.
 
A

Aby

Can i port an mc++ app to c++/cli, so that generics can b used.
sorry if i sound stupid !

thanks once again,
Abin
 
A

Andreas Mueller

Aby said:
Can i port an mc++ app to c++/cli, so that generics can b used.
sorry if i sound stupid !

thanks once again,
Abin

Hi Abin,

Yes you can, but at some time you have to make the transition to a
"unmanaged data structure".

What kind of container does your c++ dll expect? If it is something
native (e.g. an array of PODs), I would copy your sorted list into a c#
array and call your dll through interop.

If it is something else, e.g. std::map or a MFC container, I see 3
possibilities:
1) Extend the interface of the c++ dll with a function that takes an
array of PODs. Then you can call it through interop.

2) Extend the interface of the c++ dll with a managed function that
takes a SortedList<string,string>. Then call it like any other managed
assembly. However, I guess that you will have to deploy the framework
along with your c++ dll

3) Write a thin COM wrapper for the needed functionality which you can
access very easy from c#

HTH,
Andy
 
W

Willy Denoyette [MVP]

| Aby wrote:
| > Can i port an mc++ app to c++/cli, so that generics can b used.
| > sorry if i sound stupid !
| >
| > thanks once again,
| > Abin
| >
|
| Hi Abin,
|
| Yes you can, but at some time you have to make the transition to a
| "unmanaged data structure".
|
| What kind of container does your c++ dll expect? If it is something
| native (e.g. an array of PODs), I would copy your sorted list into a c#
| array and call your dll through interop.
|
| If it is something else, e.g. std::map or a MFC container, I see 3
| possibilities:
| 1) Extend the interface of the c++ dll with a function that takes an
| array of PODs. Then you can call it through interop.
|
| 2) Extend the interface of the c++ dll with a managed function that
| takes a SortedList<string,string>. Then call it like any other managed
| assembly. However, I guess that you will have to deploy the framework
| along with your c++ dll
|
| 3) Write a thin COM wrapper for the needed functionality which you can
| access very easy from c#
|
| HTH,
| Andy
|

C++/CLI is managed C++ and is capable to consume and handle generic types,
guess you are confusing C++/CLI with C++/ISO.

Willy.
 
W

Willy Denoyette [MVP]

|
| Can i port an mc++ app to c++/cli, so that generics can b used.
| sorry if i sound stupid !
|
| thanks once again,
| Abin
|

Sure, only problem is that no tool is available to port your MC++ source to
C++/CLI, so you'll have to do it manually.
Anyway here is a small sample t give you an idea what it looks like on the
interface level...

// C++/CLI DLL
// Compile with: cl /LD /clr:safe gensub.cpp
#using <system.dll>
using namespace System;
using namespace System::Collections::Generic;

public ref class GenList
{
public:
void TestFunc(SortedList<String^, String^> ^sl)
{
for each(KeyValuePair<String^, String^> kvp in sl)
{
Console::WriteLine(kvp.Value);
}
}

};

C# snip calling a C++ function that accepts a SortedList generic type

SortedList<string, string> sl = new SortedList<string, string>();
sl.Add("Hello", "world");
GenList gl = new GenList();
gl.TestFunc(sl);

Willy.
 
A

Andreas Mueller

Willy Denoyette [MVP] wrote:

C++/CLI is managed C++ and is capable to consume and handle generic types,
guess you are confusing C++/CLI with C++/ISO.

No I didn't. My point was:
If he wants to make his dll consume generic containers, he either has to
convert the whole DLL to use C++/CLI or provide a gateway from managed
to unmanaged C++ inside the dll. Both convert the dll into a managed dll
and may have impact on other users of it, e.g. different deployment.

Therefore I outlined other posibilities:
-extension of the dll interface using PODs and arrays to enable simple
interop
-COM wrapper that acts as a facade.

Both leave the c++ dll unmanaged and don't have much impact on the
alrady existing code.

Cheers,
Andy
 
A

Aby

hi,
huh! now i understand what i need. My managed dll was written in
VC++2003 and change it to use C++/CLI will take many days. So i am
going for a managed function that accepts SortedList<string,string>
(soln 2 suggested by Andy). I added the function

void TestFunc( SortedList <String,String> *a_list );

--but this gives an error 'System::Collections::SortedList' : cannot
use this type here without a top-level '*'

can u show the correct way ?

thanks,
Abin
 
W

Willy Denoyette [MVP]

| Willy Denoyette [MVP] wrote:
|
| <snip />
|
| > C++/CLI is managed C++ and is capable to consume and handle generic
types,
| > guess you are confusing C++/CLI with C++/ISO.
|
| No I didn't. My point was:
| If he wants to make his dll consume generic containers, he either has to
| convert the whole DLL to use C++/CLI or provide a gateway from managed
| to unmanaged C++ inside the dll. Both convert the dll into a managed dll
| and may have impact on other users of it, e.g. different deployment.
|
| Therefore I outlined other posibilities:
| -extension of the dll interface using PODs and arrays to enable simple
| interop
| -COM wrapper that acts as a facade.
|
| Both leave the c++ dll unmanaged and don't have much impact on the
| alrady existing code.
|
| Cheers,
| Andy
|

Sorry but the OP never mentioned unmanage C++ as target, he is talking about
a DLL built with MC++ which produces managed code without generics support,
where did you see "unmanaged" in his original posting?

Willy.
@gmxNOSPAm.netNOSPAm
 
A

Andreas Mueller

Willy said:
Sorry but the OP never mentioned unmanage C++ as target, he is talking about
a DLL built with MC++ which produces managed code without generics support,
where did you see "unmanaged" in his original posting?

Thats's correct, I read over the "M" and assumed VSC++. May bad!


Cheers,
Andy
 
A

Aby

hi,
huh! now i understand what i need. My managed dll was written in
VC++2003 and change it to use C++/CLI will take many days. So i am
going for a managed function that accepts SortedList<string,string>
(soln 2 suggested by Andy). I added the function

void TestFunc( SortedList <String,String> *a_list );

--but this gives an error 'System::Collections::SortedList' :
cannot
use this type here without a top-level '*'

can u show the correct way ?

thanks,
Abin
 
W

Willy Denoyette [MVP]

| hi,
| huh! now i understand what i need. My managed dll was written in
| VC++2003 and change it to use C++/CLI will take many days. So i am
| going for a managed function that accepts SortedList<string,string>
| (soln 2 suggested by Andy). I added the function
|
| void TestFunc( SortedList <String,String> *a_list );
|
| --but this gives an error 'System::Collections::SortedList' :
| cannot
| use this type here without a top-level '*'
|
| can u show the correct way ?
|
| thanks,
| Abin
|

You can't use SortedList which is a generic type in MC++ (vs2003) as was I
said before, only option you have is to port to C++/CLI, or pass a non
generic container type. Also you should definitely consider porting to
C++/CLI, MC++ is dead-end (IMO since it was released).
Please see my other reply for an example using C++/CLI.


Willy.
 

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