Thursday, February 25, 2010

Robocode with C++/CLI

Recently I spotted question if it's possible to create robot in C++. Here is the answer. Ok, I know, it's not like real C++ with pointers etc, this is managed version of C++, which is safe enough to be runnable in Robocode.
  • Download VS 2008 Express C++
  • Create new CLR-> Class Library (DLL) project
  • Go to project properties, Common Properties, Add New Reference, and add robocode.dll from lib folder of your robocode installation.
  • Go to project properties, Configuration properties, General, Common Language Runtime support and set it to Safe MSIL (/clrsafe)
  • Go to AssemblyInfo.cpp and delete line with [assembly:SecurityPermission(SecurityAction::RequestMinimum, UnmanagedCode = true)];
  • Go to [myCppRobot].h and insert code below
  • Compile it, drop the DLL into Robocode and enjoy!
#pragma once
using namespace System;
using namespace Robocode;
namespace myCppR {
    public ref class MyCplusplusRobot : Robot {
    public:
        virtual void Run() override {
            while(true) {
                Ahead(100);
                TurnLeft(100);
            }
        }
    };
}

Saturday, February 13, 2010

jni4net version 0.7 released

support for delegates and events

.NET events and delegates are supported by proxygen and runtime. From Java side they could be invoked with Invoke() method which has proper signature. Delegates could be also implemented on Java side, by usual anonymous class and passed back to CLR. So Java could subscribe to .NET events.
this.button1.addClick(new system.EventHandler(){
        public void Invoke(system.Object sender, system.EventArgs e){
            button1.setText("Clicked");
        }
    });
Full code is in winforms sample, which is part of binaries.

Other changes

  • solved problem with spaces in path to dll/jar
  • assembly loading now uses java.io.File or assembly name (breaking change)
  • static class is no longer found by name, attribute parameter added (breaking change)
Download there