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

Access .NET dlls from unmanaged programs

Is it possible to access .NET dlls from unmanaged programs, e.g. if I have a
..NET dll implemented in C# which listens on a socket for requests from a
program written in unmanaged C++ or VB? Would it require any specific details
in the implementation or would it be just like an ordinary C# dll?
May 16 '07 #1
6 1441
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:F9**********************************@microsof t.com...
Is it possible to access .NET dlls from unmanaged programs, e.g. if I have
a
.NET dll implemented in C# which listens on a socket for requests from a
program written in unmanaged C++ or VB? Would it require any specific
details
in the implementation or would it be just like an ordinary C# dll?
A .Net program an certainly be called from an unmanaged program using
some interprocess communication mechanism such as sockets, like you suggest.
However, a dll is not enough for this purpose. The dll needs to be hosted in
an .exe which "starts" it listening at the socket.

If you don't want to communicate with the dll via such a mechanism, but
rather you want to call it directly from unmanaged code, then, as far as I
know, the only way to do it is to go through COM. You would expose the .Net
dll as a COM object, and then consume it from C++ or VB6 like any other COM
object, disregarding the fact that it was written in .Net.
May 16 '07 #2
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:F9**********************************@microsof t.com...
Is it possible to access .NET dlls from unmanaged programs, e.g. if I have
a
.NET dll implemented in C# which listens on a socket for requests from a
program written in unmanaged C++ or VB? Would it require any specific
details
in the implementation or would it be just like an ordinary C# dll?


You need to use COM Interop, that is, you have to expose your managed
class(es) as COM objects (ComVisible=true), you also need to make your
objects COM friendly, that means you need to respect the rules of COM not
the rules of .NET. For more detailed info, search the MSDN docs for COM
Interop.

Willy.

May 16 '07 #3
Thank you Alberto,

Well, I have the network communication and that is the way I decided to go.
I can also make a connection to the C# dll which hosts the tcp server (which
is blocking synchronous according to MSDN documentation). But when I try to
send data it doesn't seem to go through. My C++ implementation of the client
uses non-blocking sockets. Is that the problem?

"Alberto Poblacion" wrote:
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:F9**********************************@microsof t.com...
Is it possible to access .NET dlls from unmanaged programs, e.g. if I have
a
.NET dll implemented in C# which listens on a socket for requests from a
program written in unmanaged C++ or VB? Would it require any specific
details
in the implementation or would it be just like an ordinary C# dll?

A .Net program an certainly be called from an unmanaged program using
some interprocess communication mechanism such as sockets, like you suggest.
However, a dll is not enough for this purpose. The dll needs to be hosted in
an .exe which "starts" it listening at the socket.

If you don't want to communicate with the dll via such a mechanism, but
rather you want to call it directly from unmanaged code, then, as far as I
know, the only way to do it is to go through COM. You would expose the .Net
dll as a COM object, and then consume it from C++ or VB6 like any other COM
object, disregarding the fact that it was written in .Net.
May 16 '07 #4
Do I have to use COM or is there another way? I feel the use of COM forces
the user of the dll to write so much more extra code and it is not the way
that I want it.

"Willy Denoyette [MVP]" wrote:
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:F9**********************************@microsof t.com...
Is it possible to access .NET dlls from unmanaged programs, e.g. if I have
a
.NET dll implemented in C# which listens on a socket for requests from a
program written in unmanaged C++ or VB? Would it require any specific
details
in the implementation or would it be just like an ordinary C# dll?

You need to use COM Interop, that is, you have to expose your managed
class(es) as COM objects (ComVisible=true), you also need to make your
objects COM friendly, that means you need to respect the rules of COM not
the rules of .NET. For more detailed info, search the MSDN docs for COM
Interop.

Willy.
May 17 '07 #5
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:C5**********************************@microsof t.com...
Do I have to use COM or is there another way? I feel the use of COM forces
the user of the dll to write so much more extra code and it is not the way
that I want it.
No, there is not. Talking about COM interop from VB, there is no "extra"
code to write, all you need to do is create an instance of the object and
call it's methods and properties. From C++, you have to write some extra
code, but this is more like template code,basically what you need is
initialize COM, create an instance of the object and call it's methods,
basically not more complex than calling a C++ class methods complex, really.
Sure you need to take care about data marshaling, but this is something
you'll have to do anyway when exchanging data across disparate systems.

Herewith a simple sample illustrating the process...

// interface
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("...................................")]
public interface IFoo{
void Run();
}

// C#
// class implementing IFoo
[ClassInterface(ClassInterfaceType.None)]
[Guid("............................................ ....")]
[ProgId("someNamespace.Foo")]
[ComVisible(true)]
public sealed class Foo: IFoo {
public Foo() {}
public void Run()
{
....
}
}

// C++ client
// Import the typelib produced by running the regasm utility on your .NET
assembly.
#import "myDotNetLib.tlb" no_namespace
// Init COM for mutithreaded access ...
HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
...
// Create an instance of Foo, using VC++ COM smart pointer support
IFooPtr m_ptrFoo(__uuidof(Foo));
// call it's meyhods
m_ptrFoo->Run();
....
// unitialize COM when done with it
CoUninitialize();
Willy.

May 17 '07 #6
Thank you Willy for clarifying things for me!

Regards,
Joachim

"Willy Denoyette [MVP]" wrote:
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:C5**********************************@microsof t.com...
Do I have to use COM or is there another way? I feel the use of COM forces
the user of the dll to write so much more extra code and it is not the way
that I want it.

No, there is not. Talking about COM interop from VB, there is no "extra"
code to write, all you need to do is create an instance of the object and
call it's methods and properties. From C++, you have to write some extra
code, but this is more like template code,basically what you need is
initialize COM, create an instance of the object and call it's methods,
basically not more complex than calling a C++ class methods complex, really.
Sure you need to take care about data marshaling, but this is something
you'll have to do anyway when exchanging data across disparate systems.

Herewith a simple sample illustrating the process...

// interface
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("...................................")]
public interface IFoo{
void Run();
}

// C#
// class implementing IFoo
[ClassInterface(ClassInterfaceType.None)]
[Guid("............................................ ....")]
[ProgId("someNamespace.Foo")]
[ComVisible(true)]
public sealed class Foo: IFoo {
public Foo() {}
public void Run()
{
....
}
}

// C++ client
// Import the typelib produced by running the regasm utility on your .NET
assembly.
#import "myDotNetLib.tlb" no_namespace
// Init COM for mutithreaded access ...
HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
...
// Create an instance of Foo, using VC++ COM smart pointer support
IFooPtr m_ptrFoo(__uuidof(Foo));
// call it's meyhods
m_ptrFoo->Run();
....
// unitialize COM when done with it
CoUninitialize();
Willy.

May 17 '07 #7

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

Similar topics

4
by: I.P. Freely | last post by:
Greetings my dear friend, Let's say I wanted to write an application that talks directly to a network card. For example, say I'm designing a firewall or packet sniffer. Do I need to use...
0
by: Neil Sargent | last post by:
This is a general posting of how I fixed a problem created by using the Access 97 ODE Setup Wizard on a Windows XP machine. I hope it helps anyone who comes across the problem saves them the 3 days...
2
by: kinghuangz | last post by:
Everyone: I want to use a DLL(FileName:PubFunction.Dll) built with VS.Net(C#) in a unmanaged Program .But there was a Dll just has the same filename,it was built with VC++(6.0).The unmanaged was...
0
by: Frank Lopez | last post by:
My program structure is: 1. 2. 3. => manually does the crt-init and crt-terminate calls 4. -- this is accessed by the unmanaged C++ classes in (3) using LoadLibrary and FreeLibrary
6
by: _R | last post by:
I've had to write a lot of code to interface C# to older Win32 DLLs. Basically, an unmanaged C++ class talks directly to the Win32 DLL. A managed C++ class encloses the unmanaged C++ class. C#...
1
by: Ken Allen | last post by:
I am in the process of upgrading an existing product code base from C++ to C#, converting the use of a named pipe for the client/server communication to using .Net remoting via C# classes. Things...
29
by: DFS | last post by:
From an Access 2003 module: v = Shell("ftp.exe -s:D:\ftpCommands.txt") Contents of D:\ftpCommands.txt ---------------------------------- open ftp.destination.com user password send sourceFile...
3
by: gopal | last post by:
I am developing an application in CSharp - windows forms based, which copies the DLLs both unmanaged and managed DLLs from a shared folder and will overwrite the existing versions of managed &...
1
by: rwf_20 | last post by:
My setup consists of two DLLs and an EXE, all C++. The EXE is unmanaged, and calls an unmanaged DLL. The unmanaged DLL calls a managed DLL. If I build all three in the same version of VS,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
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...

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.