473,672 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Consuming C# dll from unmanaged C++ exe; StringCollectio n

I have a case where a C# dll is being consumed by an unmanaged C++
executable. One of the dll methods returns a StringCollectio n 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 StringCollectio n object?

TIA.
Oct 17 '06 #1
2 2171

"eSapient" <eS******@discu ssions.microsof t.comwrote in message
news:7F******** *************** ***********@mic rosoft.com...
>I have a case where a C# dll is being consumed by an unmanaged C++
executable. One of the dll methods returns a StringCollectio n 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 StringCollectio n
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.Collecti ons.Specialized ;

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

void IMyClass.MyMeth od(out StringCollectio n strings)
{
strings = new StringCollectio n();
strings.Add("Fi rst string");
strings.Add("Se cond string");
}
}

[GuidAttribute(" F26D63B9-C3A8-4f1a-9B17-1C441DE58EAF")]
public interface IMyClass
{
void MyMethod(out StringCollectio n 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.t lb"
//#import "..\ClassLibrar y3\bin\Debug\Cl assLibrary3.tlb "

#using <mscorlib.dll >

using namespace System;
using namespace System::Collect ions::Specializ ed;
//using namespace ClassLibrary3;

int _tmain()
{
::CoInitialize( 0);
ClassLibrary3:: IMyClassPtr pIMyClass(__uui dof(ClassLibrar y3::MyClass));
// pIMyClass.MyMet hod();

return 0;
}

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

Thanks in advance.
Oct 29 '06 #3

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

Similar topics

2
2384
by: Dan | last post by:
Hi, Is it possible to iterate through a StringCollection ? I have the following code but "row" always seems to contain the values of rowValues2 Thanks for any input, Danny
1
4079
by: Sin | last post by:
Hello everyone, I'm currently trying to migrate a MSVC6 project to .NET... The straight conversion (ie : unmanaged) was extremely straightforward. Now I'm trying to compile and hopefully run the project as managed to estimate the kind of performance difference there will be for the kind of applications we do here. The project is composed of a EXE and 5 different DLLs. One of these DLL is unmanaged (VC6) and can hopefully stay that way...
8
15793
by: Naz | last post by:
Hi, I am new in C# and converting a program from delphi to C#. I have to store a list of strings in memory and also need to save the strings to disk at the end. I use StringCollection and my program looks like this... stringCollection.Add("line1"); stringCollection.Add("line2"); stringCollection.Add("line3");
0
1464
by: Eric Twietmeyer | last post by:
Hello, Am I mistaken in thinking that if I have a mc++ dll that I should be able to link against it (so long as it has exported symbols via declspec(dllexport)) in an unmanaged application? I am currently testing this and it fails. I have a native, unmanaged test application that links against my mc++ dll with a function entry point. Linking works fine, no errors. When I try to run in the debugger I see the output below (abreviated...
15
2790
by: Chetan Raj | last post by:
Hi All, We have a web-application in asp.net that interacts with legacy code written in COM. The memory usage in aspnet_wp.exe increases every sec and never reduces. Using the .NET performance counters, we found that unmanaged memory was 90% of the total private bytes of aspnet_wp.exe. We suspected that the COM code has memory leaks. So we made it as a COM+ Service running as dllhost.exe. Surprisingly, there was no memory increase in...
2
2977
by: interX | last post by:
Hi, I have a little problem with managed/unmanaged in Visual Studio 2005 (Compiler setting /clr). I need to overhand several function pointers from managed to unmanaged. These function pointers are stored in an unmanaged struct and this struct is then overhanded by reference to the unmanaged code (See code pieces below). I thought the way to go is to create a delegate, pin it via GCHandel::Alloc and use the function
1
2248
by: Arne Adams | last post by:
Hi, I try to use a C# Dialog in a legacy MFC application. The problem seems to boil down to the following: from an unmanaged console application I can call any function of the managed bridge - if I try to do the same from my mfc application the application won't start 'SeaMain.exe': 'E:\WINDOWS\system32\mscoree.dll' geladen, Keine Symbole geladen. 'SeaMain.exe': 'E:\WINDOWS\system32\msvcr71d.dll' geladen, Symbole geladen.
7
6076
by: Tremendo | last post by:
Hi, I need to consume an unmanaged DLL from managed C#. The DLL is "ae766.dll". I have problems with one function in it. Who developed the DLL, provided the following information, regarding that function: ----------------------------- Declaration ----------------------------- extern "C" long __stdcall GetAnalyzerTraceData(LPSAFEARRAY FAR *);
4
2067
by: Mau | last post by:
Hi, I have a managed dll which I am accessing from an unmanaged exe using Com Interop. I installed everything on computer 1 and run the unmanaged exe and from exe called my managed dll and everything works fine. Now from a second machine 2 on same network, I called this exe from Computer 1. Unmanaged exe run properly but when I called my managed dll, get error "OLE error 8013150A, ClassID: 4AD092B-26F2-4598-9B7B-A4654C9ACB76". But if I copy...
0
8407
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8831
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8611
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8683
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7449
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4420
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2821
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 we have to send another system
2
2064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1819
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.