468,321 Members | 1,825 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,321 developers. It's quick & easy.

Consuming C# dll from unmanaged C++ exe; StringCollection

I have a case where a C# dll is being consumed by an unmanaged C++
executable. One of the dll methods returns a StringCollection object. What
are the requirements for the C++ executable in terms of .h, .tlb files to be
included/imported, and the syntax for consuming the StringCollection object?

TIA.
Oct 17 '06 #1
2 1985

"eSapient" <eS******@discussions.microsoft.comwrote in message
news:7F**********************************@microsof t.com...
>I have a case where a C# dll is being consumed by an unmanaged C++
executable. One of the dll methods returns a StringCollection object. What
are the requirements for the C++ executable in terms of .h, .tlb files to
be
included/imported, and the syntax for consuming the StringCollection
object?
You can use .NET via COM, see "Runtime Callable Wrapper". However that
might not be the easiest way to go.

Is the .dll a hard dependency (exe can't work without it) or a soft
dependency (lose some features but press forward)?

If it is a hard dependency, then your exe is actually dependent on the .NET
framework (exe needs dll needs framework). In that case, see if your exe
works correctly when built with /clr.
>
TIA.

Oct 20 '06 #2
I created a .NET class library with the following C# code:

using System;
using System.Runtime.InteropServices;
using System.Collections.Specialized;

namespace ClassLibrary3
{
[GuidAttribute("6F66DEAB-1BCF-4ce4-AB06-D298BA2B56CA")]
public class MyClass : IMyClass
{
public MyClass()
{}

void IMyClass.MyMethod(out StringCollection strings)
{
strings = new StringCollection();
strings.Add("First string");
strings.Add("Second string");
}
}

[GuidAttribute("F26D63B9-C3A8-4f1a-9B17-1C441DE58EAF")]
public interface IMyClass
{
void MyMethod(out StringCollection strings);
}
}

I registered this project for COM Interop.

I then created a console application in VC++, where I added a reference to
the class library project (both projects are in the same solution). I am not
very familiar with COM programming using C++. My intention is to consume the
class library above in unmanaged C++. I added the following code in the
console application, but it is incomplete since I don't know how to go about
it.

#include "stdafx.h"

#import "mscorlib.tlb"
//#import "..\ClassLibrary3\bin\Debug\ClassLibrary3.tlb"

#using <mscorlib.dll>

using namespace System;
using namespace System::Collections::Specialized;
//using namespace ClassLibrary3;

int _tmain()
{
::CoInitialize(0);
ClassLibrary3::IMyClassPtr pIMyClass(__uuidof(ClassLibrary3::MyClass));
// pIMyClass.MyMethod();

return 0;
}

Can somebody help me and show me how to consume the .NET class?

Thanks in advance.
Oct 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dan | last post: by
8 posts views Thread by Naz | last post: by
reply views Thread by Eric Twietmeyer | last post: by
15 posts views Thread by Chetan Raj | last post: by
7 posts views Thread by Tremendo | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by howard w | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.