How to invoke dll method on a different machine? - c#

Let's say I have a C dll with functions like:
void beep();
char* foo(char** whatever);
Now I want to be able to put this dll on one machine (server) and call it from a different machine (client). The client can be C# but the dll (and proxy server) needs to be regular C. The ideal would be something like this on the client:
MyDllAccess remote = ProxyLib.GetRemoteDll("192.168.1.10:12100", "mydll.dll");
remote.execute("beep"); // other machine beeps (uses GetProcAddress to find function)
I know I'm reinventing COM, etc, but..... is there a way? If not, why not and what is the simplest way to do what I want since I already have the dll?
EDIT: It also needs to work with Linux shared library, e.g. ProxyLib.GetRemote("192.168.10.12100", "mything.so")

If you need it to work across different OSes, you will have hard time doing it in a pretty generic manner. You can search for "platform independent APIs" in this wikipedia article: Inter-process communication to get an idea. Most of what you will find is heavyweight.
One simple way would be to write an ad-hoc HTTP REST server (possibly with JSON or XML as the interchange format). It has the advantage of being simple, and you will be able to access it using any OS with a decent HTTP stack (including mobiles).

Related

Call Java function using .Net(C#)

is it possible to invoke function which is written in Java using WCF or any class application written in C# .net
Can it be possible by using webOrb..
i can't find enough information about Java to .Net remoting..
If you want to communicate between C# and Java you have a couple of options.
The cleanest: Build a service.
This assumes you have access to the source code of both your C# component and your Java component. In the case that you want to call a method within Java, you can build a service that allows a connection from your C# client, to your Java service, and the service then executes the desired functionality, and returns a value back to the C# client. Some easy ways to do this is by building a RESTful service or using Thrift. I recommend you choose a solution similar to this one.
The most complex: Corba
Corba is a standard defined to communicate amongst different computer languages. Most mature languages have support for it, but it is a bit unusual, and the use of it has declined in favor of building service. This also assumes access to both source codes.
You'd have to independently look for the information regarding how to use Corba on both Java and C#. I would really advice against this.
The dirtiest but quickest: Execute as process and parse output
I really do NOT recommend you to do it this way unless you really have no choice. This would entail executing a Java program from within C#. This is only a good choice when you have no other option, because all you have is an executable. If that were the case, you can use the Process class to execute the external program, sending it parameters, and then reading the output. See the example mentioned here:
How do I start a process from C#?
This has many downsides though, as you'll have to think of every exceptional cause, determine the output for those cases, and then determine how to parse that output. If the program has any level of complexity, before you know it, you'll end up with hard to maintain code.
Conclusion: Build a Service
That's probably your best bet. Build a service that exposes an API that the C# client can call on.
We are using JCOBridge package: it is able to create a bidirectional invocation of Java API from C# (.NET Core/6/Framework).
The templates available on Templates was our good starting point for the needs we had. We reach the goal in few lines of code.
UPDATE 2022: the JNet project on GitHub can be used as a starting point. Another project is KNet, hosted on GitHub and based on JNet, that is a gateway for Apache Kafka Java API.

Deploying and Executing code on a remote machine via Remoting/WCF

We're building a C# application that allows loading custom plugin DLLs and executing them.
Each DLL contains some task, and we'd like that task to be transparently executed either locally or on some remote server.
I have examined various solutions for this, and so far the best solution that was proposed was to use WCF.
I'd like to understand, since i'm currently only through basic tutorials of WCF, if it is at all possible to dynamically deploy new code using WCF to be executed remotely?
The way i see it, i have 2 different scenarios:
Remote machine has a base "execution" library deployed.
Remote machine has no WCF service installed on it currently.
With option #1, i guess i could have some functionality to send across the DLL or something, and execute it remotely, since the execution library knows how to do that already.
With option #2, i would need to basically deploy everything (somehow) from scratch, and then send a command to run it.
Is this scenario possible at all? do you have any tips to perform this kind of task?
Also, if you have any good WCF tutorials (i'm currently reading up on MSDN).
Thanks!
The important thing to remember here is that WCF can be used to transfer data, but not to tranfer execution logic. You can send the result of an addition from one end to the other, but you cannot send some arbitary instruction (like adding or whatever) and let the other end magically execute it.
In other words, if you have a WCF client on the remote end, you could send it your DLL file (as a binary data), and the client could then dynamically load and execute it using reflection (but what's without mentioning all the obvious security and compatibility concerns this would raise).
Another maybe easier option would be to send scripts instead of compiled code, and execute them with some interpreter on the server side. But whatever trick you use, you'll need to do a lot of work outside of WCF as sending instructions is not the objective of WCF.

How to consume in-process WCF services from unmanaged C++

I have some WCF services running over HTTP and a C++ client using gSOAP to consume them. This works, but we are considering running the service host in the same process as the client, to create a fully local stack.
What is the best way to allow the C++ client to consume these services? Do we still need to use HTTP binding? Or will something like named pipes or NullTransport work? Preferably something that will work with gSOAP or something that we can replace gSOAP with.
if both are in the same process then have them "talk" to each other via direct means. i.e. accessing objects directly. otherwise going over a comms layer is extremely expensive. the same can be said for accessing files on your hard drive via a network share when really going file say c:\foo\something.txt is more productive.
c++ allows you to construct CLR types that are both native code but also CLR-aware. using this technique allows your c++.NET type so to speak from .NET proper. your .NET types will have no idea that they are invoking c++ or vice versa.
have a look in your c++ compiler settings for CLR

how to share a System.Object in memory between two or more programs?

is there any library, code sample, open source project, etc helping me share objects(a dataset , a collection, ...) between two or more programs with direct or indirect memory access.
if there is no way to do that, please show me other ways to make my app work.
language:c# .net
platform:vista x64
You want to look at WCF - Microsoft's consolidated WebServices and Remoting framework. It provides a number of options for inter process communication, from XML over http to binary over TCP/IP or Named Pipes.
Some further articles:
http://www.infoq.com/news/2008/01/wcf-comm-options
http://en.wikipedia.org/wiki/Windows_Communication_Foundation
I believe your best and safest bet would be to check out Named Pipes via WCF. Straight from MSDN:
A named pipe is an object in the
Windows operating system kernel, such
as a section of shared memory that
processes can use for communication. A
named pipe has a name, and can be used
for one-way or duplex communication
between processes on a single machine.
I am not very sure about how this can be accomplished but It might be possible if the assemblies are both in same domain. If one of the assembly loads the other assembly and gives it a pointer/reference to through which the second assembly can call into the first one then it might be possible to call a method inside the first assembly with parameters.
In one of my projects which is a COM addin written in C#, a COM dll instantiates two different classes from an assembly and they can share static data.
I'm not aware of any so I'll leave someone else to answer. If it's not possible, and depending on whether both applications need to be aware of changes in real time you could potentially serialize your class to a file (e.g. XML) to share the current state information.
you can use a file in a certain way or a database or any other persistent technology, messaging, WCF (or lower interprocess communication protocols)
you generally can't access one program memory address space from another without special hacks, definitely not in c#.

Filter TCP Packets in C#

I am writing an application where all the request to the internet should go from it like in firewall. so that i can block the request for a particular website. In my case the program will be running on the same machine. I have tried the promiscous method but using that we can only capture all the packets comming and going from the machine,
The easiest way to do it is probably to write a Layered Service Provider (LSP). There is an example in the Microsoft SDK on developing LSPs as well. Not as secure as a driver type firewall setup, but a lot easier to implement.
There's "probably" a way to do it with C#, but I have never tried it. Something to look into. If not then just create a native DLL with C/C++ that implements the LSP then have it communicate with your app.
You have to insert your code in the TCP/IP stack, which, if I understand correctly, requires a windows driver.
C# cannot compile native windows drivers, so you'll need to use a library or DLL to implement at least part of your functionality. Look for solutions using C++.
-Adam

Categories