How to run Java (a JAR file) from C#? - c#

I want to create a wrapper for this client for a game but it's in Java. How would I run the .jar file in C#?

Have you looked into IKVM.NET?

What about using Exec to run the appropriate java -jar file.jar command? You might want to add some fancy logic to try and ensure a java is indeed available on the system, but that shouldn't be too hard...

One option is JuggerNET by CodeMesh, which generates .NET wrappers for Java APIs.

The wrapper is called wrapper because it wraps the jar file and starts it using a virtual machine. There are also some wrappers that already contain the VM in the exe file.
You do not need to write such a wrapper by yourself, these are already. One powerful and free wrapper is launch4j.

Related

C++ project using C#

I have a problem with two projects. My main project is in C++. Another one is in C# and it is measuring current network bandwidth (updated every second). What I need to have is that C++ project can get those values.
Mt first thought was to let the C# export calculated values to .txt file. Another project could read those values. But problem is that it would mean that both would use that file at the same moment what seems to be impossible (or maybe I could synchronize it somehow?)
I was reading a lot about creating and using library, but it looks complicated to me.
Are there any other ways to do that?
Please, I need help...
The simplest way is using stdin / stdout for this purpose. Just write the values to Console from the C# program and read it from pipe in the C++ program.
Or maybe you'd like to extend your project to C++/CLI (.NET-based C++ extension) and directly reference your C# library.
As well as the other options outlined by the others, you also have the option of making the functionality of your c# code into a DLL and then calling that from your C++ code to allow you to use the functionality of the DLL to get network information. I have a how-to type link here on how to create a C# DLL. You can then reference said DLL in C++ and utilise its functionality as needed.
Hope this helps, and let me know if you need any further information:)

How to use open source net-snmp library in C#

I want to use net-snmp library in C#, do I need to write some wrappers around this C++ library or there are any ready made wrappers available?
I'm not sure if that particular library has a C# wrapper, but you can try SnmpSharpNet.

Calling UNIX and Linux shared object file .so from c#

Is there a way for a Shared Object file written in C and built on Unix to be called from C# P/Invoke?
Or do I need to use Java or something like that?
Mono has the ability to integrate with native libraries from within C# built on top of dlopen(3). You just have to use the DllImport statement with the name of the library (i.e. 'libform.so.5'), then wrap the native code and data types with a friendly C# class that takes care of all the low-level stuff. This page has a good overview with lots of information on how to deal with marshaling pointers and other unsafe types.
Once you've got your wrapper class written, you can just use that without worrying about the fact that it's using a native shared library underneath.
I would say at the least there's likely to be no easy way, especially if you mean C# on Windows. In that case you would need something that would be able to decode the shared object and get to the code in it, sort of a re-implementation of the ABI for GNU/linux. Also, any other libraries would have to be present and usable as well, such as the C runtime library and the like. This would likely be a very significant effort.
As for doing it directly under linux/Mono, see this answer: Calling UNIX and Linux shared object file .so from c# .
You could also try to see if what open office does, http://packages.debian.org/lenny/cli-uno-bridge could be helpful; but this is more of an interface rather than directly linking the two together.

Call Python from .NET

I have some code written in Python which can not be transferred to a .NET language. I need to call one of these functions from my .NET WinForms application.
Now, I do it by starting the Python script as a separate process and pass parameters to it as command line arguments. It works, but I don't really like this solution. I'd like to improve it to a better one.
Is there any better way to call a function of a .py script from a .NET application? What is the best way to do it?
Note: IronPython is NOT an option for this Python script
This might be a lot more work than launching the Python process, but here's an alternate solution.
You can embed Python into another program. The API is for C and Interop from .NET will probably be a major pain. If you're into a bit of a safer way to handle the native Python API, you can look into Boost.Python, which, among its less advertised features, has support for embedding.
With these tools, you can write a C++ managed DLL that uses Boost.Python to load the Python interpreter and execute any Python script. Thus, you can execute any Python code directly in the hosting process, eliminating the use of an external process and any form of IPC.
Edit: AFAIK, all you have to add to your installation procedure is the deployment of the Python DLL and Boost.Python DLL.
Besides the COM option, you could make your Python script instantiate a xmlrpc server -
it is quite transparent and you never have to deal with "xml" on your own code.
Then, on .net side, you simply connect to your python app via xmlrpc - if there is no suitable way to do that in C#, just write a client function in IronPython.
The SimpleXMLRPCServer example on Python documentation is enough for that:
http://docs.python.org/library/simplexmlrpcserver.html
I think you need to re-evaluate Carlos' answer.
See the section Implementing COM Objects with Python in Mark Hammond's book Python Programming on Win32.
You should be able to create a COM object, then have .Net interact with it.
From the book the following will create a COM server with a single method.
# SimpleCOMServer.py - A sample COM server - almost as small as they come!
#
# We expose a single method in a Python COM object.
class PythonUtilities:
_public_methods_ = [ 'SplitString' ]
_reg_progid_ = "PythonDemos.Utilities"
# NEVER copy the following ID
# Use "print pythoncom.CreateGuid()" to make a new one.
_reg_clsid_ = "{41E24E95-D45A-11D2-852C-204C4F4F5020}"
def SplitString(self, val, item=None):
import string
if item != None: item = str(item)
return string.split(str(val), item)
# Add code so that when this script is run by
# Python.exe, it self-registers.
if __name__=='__main__':
print "Registering COM server..."
import win32com.server.register
win32com.server.register.UseCommandLine(PythonUtilities)
The book goes on to say ".. you can do this by executing the code as a normal Python script. The easiest way to do this is to open the source file in PythonWin and use the Run command from the File menu. "
I think you need the ActivePython distribution from Activestate to do it.
See this question Consuming Python COM Server from .NET
It works, but I don't really like this solution, I'd like to improve it to a better one.
No, AFAIK there isn't a better solution, especially if IronPython is a no-no for you. So you could still keep this as a temporary workaround while waiting for the script to be migrated to .NET or until you find that someone already wrote a library on .NET that provides you with similar functionality.
Create a COM .dll from a .py script and use Interop in your .NET code.
Have a look here: http://docs.python.org/faq/windows.html
PythonNet should help with this one. It enables you to call python code from C#.
A cleaner way is to expose the python script via a Flask REST API and consume that from your .NET Application. Don't forget to put proper authentication in place.

How to use C# code in C++ project

I have some code in C# which I want to use in other project (coded in C++).
From what I researched, I need to create a .lib but MSVS only creates .dll (I think..). I think is possible to use the .dll by using LoadLibrary() over C++ but seems not very friendly.
1 - Can I create the .lib in MSVS? If not, how can I create it.
2 - What is the best way to integrate the code? By the .lib or using .dll + LoadLibrary()?
The easiest option, honestly, is to use C++/CLI. That lets you use both object systems (.NET, and traditional C++ with its standard template library).
Is it managed C++ ? If so you can directly add a reference to the C# dll and use it.
What you need is a com compliant class in c#:
http://en.allexperts.com/q/C-3307/2008/2/Using-C-class-C.htm
http://blogs.msdn.com/b/deeptanshuv/archive/2005/06/26/432870.aspx
One possibility is to make your C# code Managed COM compliant. Then use the standard COM api's (QueryInterface etc) to call the C# COM code.
The codeproject sample may be useful
http://www.codeproject.com/KB/cs/ManagedCOM.aspx

Categories

Resources