473,379 Members | 1,539 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,379 software developers and data experts.

Calling unmanaged C++ dll from C# app

Background:

We have developed an C# MDI (Multi-Document Interface) application which
loads different assemblies/forms, so that the user can have many "application
windows" running within the same container. Some of these child forms need
to use legacy code from a mature product written in C, and we have achieved
this by compiling our C code into a DLL, and using DllImport directive to
reference the functions that we need. The C# applications are able to use
the functions perfectly, but we are now facing another challenge.
The Challenge:

The C code uses caching to reduce database access delays, and those caches
are simple linked lists or arrays that are global/static. While this
assumption worked well for our legacy application (which is comprised of
numerous stand alone executables that naturally have their own process
space), it doesn't work well for the MDI C# model. That is, two child forms
running inside the MDI would share those caches, since all the
assemblies/forms (and DLLs) share the same process space. However, for our
product, it is unacceptable for two MDI child forms to share the same data
cache (for example, the cached data may have been derived based on user
preferences that are specific to each child form).
Where We're Stuck:

We have been trying to load the DLL more than once into the MDI process
space, where each copy will have it's own static/global variables, and
therefore different cached data, but we were unsuccessful. We attempted
loading them through different Application Domains, but it looks like the
external reference is resolved only once per DLL and reused (i.e. all app
domains reference the same copy of the DLL in the MDI process space).

Of course, we could rewrite the C code to eliminate the global/static memory
assumptions, but this isn't feasible due to the amount/complexity of the
legacy code.

We've also considered managing multiple copies of the dll to overcome this
problem (we've tested this and it works), but we don't want to resort to a
hack like this unless we have no other choice.
The Question:

Does anybody know of a way to load multiple instances of the same unmanaged
C++ dll into the process space of a single C# application (using application
domains or some other technique)?

--
Chris

Nov 16 '05 #1
7 2097
Hi,

Based on my experience, the dll is loaded perprocess and that is why it is
called dynamical link library.
Anyway I will keep research the issue and give you a reply ASAP.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 16 '05 #2
Hi

So far I think we have another approach is wrap the C++ dll in a COM EXE
out of proc server, so that every mdi children frame can create its own com
exe server.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 16 '05 #3
Hi,

Thanks for the feedback.

MSDN documentation says "Access to in-process objects is much faster than to
out-of-process server objects because Automation does not need to make remote
procedure calls across the process boundary."

So we'd like to avoid this if there is an in-process technique that will
achieve the desired effect.

Thanks,

Chris
""Peter Huang" [MSFT]" wrote:
Hi

So far I think we have another approach is wrap the C++ dll in a COM EXE
out of proc server, so that every mdi children frame can create its own com
exe server.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 16 '05 #4
Hi

I have confirmed with senior engineer that so far we may use COM
Server(EXE), because the dll is loaded per process.
Or I think we have change the dll design which will not be an good
alternative.

If you still have any concern, please feel free to post here.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 16 '05 #5
Hi Peter,

Thanks for the reply and thanks for asking around. Your time/help is
greatly appreciated.

Concerning the COM server idea, we'd like to avoid an out-of-proc solution
as I mentioned in my last note.

What do you mean when you refer to changing the dll design?

Thanks,

Chris

""Peter Huang" [MSFT]" wrote:
Hi

I have confirmed with senior engineer that so far we may use COM
Server(EXE), because the dll is loaded per process.
Or I think we have change the dll design which will not be an good
alternative.

If you still have any concern, please feel free to post here.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 16 '05 #6
Hi

I am sorry I mean we can use a class member not global memeory to do the
data cache, so that we can make the dll a inproc com server, and each
instacne of the com object can have its own private data.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 16 '05 #7
Hi Peter,

Thanks again for your help.

As you might appreciate, we're trying to get to a point where have all of
the information necessary to make an educated (management level) decision on
what path to take.

From your replies, it seems clear that we either need to change our code to
eliminate the global variables or we need to use an out-of-proc strategy to
be able to load multiple instances of the same dll.

In other words, there doesn't appear to be a way to load multiple instances
of the same dll within the context of a single process.

Before we close the book on the investigative stage, however, we're
wondering whether it would be worthwhile to officially raise this question
through MSDN technical support. Do you think that there's any chance that we
could learn more about the issue by doing this?

Thanks,

Chris

""Peter Huang" [MSFT]" wrote:
Hi

I am sorry I mean we can use a class member not global memeory to do the
data cache, so that we can make the dll a inproc com server, and each
instacne of the com object can have its own private data.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 16 '05 #8

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

Similar topics

4
by: jarmopy | last post by:
Hi, I have made a service with C# and calling that service class from another C# program with remoting. (Referendes from the calling program) That service class is configured so that garpage...
15
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
1
by: Jesse McGrew | last post by:
Hi all, I'm trying to make a plugin DLL for a third-party application, using VC++ .NET 2003. This DLL acts as a bridge between the C++ plugin API of the application, and my actual plugin code...
1
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
0
by: monika.saxena | last post by:
Hi all, In one of my projects which is a web based application in asp.net, a third party tool - "Frontline Solver DLL" (It is an unmanaged DLL and ..NET is calling it using the PInvoke) is used....
1
by: Leftie | last post by:
Folks, I'm trying to call an unmanaged function from VB.NET and keep getting "Object reference not set to an instance of an object" error. The code that i wrote can be found at:...
1
by: MC-Advantica | last post by:
Does anyone have a simple "Hello World" like application that demonstrates unmanaged C++ calling managed C++ developed in VS2005? I'm confused by many posts as they discuss managed extensions from...
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...
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...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.