Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Friday, August 3, 2012

Invoking native C/C++ lib from C# and mono

So as a continuation of the last post http://orphanware.blogspot.com/2012/08/making-c-available-through-native-calls.html......here is how u connect using c# and mono (haven't tried .net windows but I'm guessing it's the same)

this one is super ez. here is the full example
using System;
using System.Runtime.InteropServices;

public class HelloWorld
{

        static public void Main( string[] args )
        {
                
MYLIB_helloWorld();

        }

        [DllImport("libmylib.so")]
        private static extern void MYLIB_helloWorld();




}

then run with
gmcs HelloWorld.cs
mono HelloWorld.exe

Monday, July 16, 2012

packit4me.com (2D/3D Bin Packing)

(UPDATE 06/04/2013) This project is now available @ http://www.packit4me.com/
3D/2D bin packing. Given multiple bins and items to be packed into the bins, the algorithm attempts to find a good solutions of how to pack. I got so many requests for this that I made it into a real life project. Check out the site. It has comprehensive documentation on how to call the API with all the popular languages (Java, php, C#, ruby, python). I hope it works for you! If not just email me or make some posts here. I'll try to setup some ticket system soon to take requests.

Friday, October 14, 2011

Creating a WCF Stand Alone Console App

So I had a problem the other day that really annoyed me. I realized that I couldn't spawn processes directly from IIS. The solution was simple, build a wcf stand alone service that IIS app would call to spawn another process...the result was actually really easy. wcf is almost like remoting, in fact it reminds me a lot of java RMI. It's super ez to setup though...

First you have to provide an interface just like java RMI to serve as a contract between the two processes:

using System.ServiceModel;

[ServiceContract]
    public interface IJSXAdapter
    {
        [OperationContract]
        byte[] GetProcessedImageBytes(string jsxTemplatePath, string[] args);
    }

notice the ServiceContract and the OperationContract attributes

you then of course specify the concrete class (you don't need the attributes in the concrete implementation)


here is the server code:

class Program
        {
            static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(typeof(JSXAdapter), new Uri[]{
                        new Uri("http://localhost:8000"),
                        new Uri("net.pipe://localhost")
                    }))
                {
                    host.AddServiceEndpoint(typeof(IJSXAdapter),
                      new BasicHttpBinding(),
                      "Reverse");

                    host.AddServiceEndpoint(typeof(IJSXAdapter),
                      new NetNamedPipeBinding(),
                      "PipeReverse");

                    host.Open();

                    Console.WriteLine("Service is available. " +
                      "Press  to exit.");
                    Console.ReadLine();

                    host.Close();
                }
            }
        }



That's it for the server. In this example I make not one but two ServiceEndpoints or listeners. One http and another net.pipe. In your client you can connect with either..

Now for the client. You specify the same interface (you could potentially share a dll:


[ServiceContract]
    public interface IJSXAdapter
    {
        [OperationContract]
        byte[] GetProcessedImageBytes(string jsxTemplatePath, string[] args);
    }



and the client code (copied from a method)
 XmlDictionaryReaderQuotas readerQuotas = new XmlDictionaryReaderQuotas();
            readerQuotas.MaxArrayLength = 2147483647;



            BasicHttpBinding httpBinding = new BasicHttpBinding();
            httpBinding.MaxReceivedMessageSize = 2147483647;
            httpBinding.MaxBufferSize = 2147483647;
            httpBinding.ReaderQuotas = readerQuotas;
            ChannelFactory httpFactory = new ChannelFactory(httpBinding,
                                                                                      new EndpointAddress("http://localhost:8000/Reverse"));
            NetNamedPipeBinding pipeBinding = new NetNamedPipeBinding();




            pipeBinding.MaxReceivedMessageSize = 2147483647;
            pipeBinding.MaxBufferSize = 2147483647;            
            pipeBinding.ReaderQuotas = readerQuotas;
            ChannelFactory pipeFactory = new ChannelFactory(pipeBinding,
                                                                                      new EndpointAddress("net.pipe://localhost/PipeReverse"));




            IJSXAdapter httpProxy = httpFactory.CreateChannel();
            IJSXAdapter pipeProxy = pipeFactory.CreateChannel();

            
            
            string[] args = new string[1];
            args[0] = "some string";



            byte[] processedImageBytesFromHttp = httpProxy.GetProcessedImageBytes("some string", args);

            byte[] processedImageBytesFromPipe = pipeProxy.GetProcessedImageBytes("some string", args);
            





Though that looks like a lot most of it is extending the size of how much data can come back. Also keep in mind that this is an example of both end points. in practice you would only connect to one...

Thursday, October 21, 2010

Goodbye MS Visual Studio & .NET, Hello Mono Stack + Lucid Lynx!

I'm originally from the MS world. Don't fault me it was the time that I grew up. My generation had pretty much grown up eating up MS products without any knowledge of anything else. So naturally while doing a CS undergrad I learned classic ASP and out of school started with .NET 1.1.

The last two or so years I've had the pleasure of working at a company that is solely open source. At first I hated it. Everything seemed soooo much harder. You had to configure this and configure that and I absolutely hated the linux command terminal. Little did I know it was my ignorance that made me hateful. Not before long, I fell in love with the terminal. Then I fell in love with Java and JEE. Open source simply fits the personalities of true developers better. I would of told present day me off 2 years ago, but it's true. MS creates inferior developers.

Anyway, recently I got a project that was .NET. I had heard of Mono before but I actually got a chance to work with it. Let me tell you that it's awesome!!! Sure it doesn't have all the bloat that VS has but that bloat is unnecessary anyway for most things I work on. It's faster than VS2008. NUnit hooks in beatifully, unlike the MS Test project that is super bloated and slow. It has all types of engineering goodies like LOC, Cyclomatic Complexity, etc.

Anyway for the full stack you gotta get:

1. Mono - Runtime
2. MonoDevelop - IDE
3. MonoDevelop-Nunit - NUnit integration for IDE
4. Nunit - Command line NUnit
5. NAnt - Build tool
6. XSP2 - Mini Web/App server for asp.net

If your on Ubuntu 10.04 follow these steps:

1. Add deb
http://badgerports.org lucid main
to
/etc/apt/sources.list
2. Run following commands

gpg --keyserver http://badgerports.org/directhex.ppa.asc --recv-keys 0E1FAD0C
gpg --export --armor 0E1FAD0C | sudo apt-key add -
sudo aptitude update
sudo aptitude install monodevelop
sudo aptitude install monodevelop-nunit
sudo aptitude install nunit
sudo aptitude install nant
sudo aptitude install mono-xsp2

That's it. I guarantee it'll be installed before you get through the first 5-10 minutes of VS2008 if you have good bandwidth.