モノトーンの伝説日記

Apex Legends, Splatoon, Programming, and so on...

<mini> BridJ、動かした話。

 BridJ、2 か月以内ぐらいに変更があるのでどうやらまたきちんと管理されているらしい。

 とりあえず最新版の 0.7 PRE 使ってみました。BridJ エラーで動かないなぁ、って思ってたら、DLL を作る方法を忘れていた、だけでした。

 Windows では __declspec( dllexport ) つけねーといけないんだわ!

 これで結構悩みました。最後にコード貼っておきます (自動生成も含めて)

Main.java

package com.mntone;

import test.TestClass;

public class Main
{
    public static void main( String[] args )
    {
        final TestClass nativeTestClass = new TestClass();
        nativeTestClass.run();
    }
}

TestLibrary.java

package test;

import org.bridj.BridJ;
import org.bridj.ann.Library;
import org.bridj.ann.Runtime;
import org.bridj.cpp.CPPRuntime;

/**
 * Wrapper for library <b>test</b><br>
 * This file was autogenerated by <a href="http://jnaerator.googlecode.com/">JNAerator</a>,<br>
 * a tool written by <a href="http://ochafik.com/">Olivier Chafik</a> that <a href="http://code.google.com/p/jnaerator/wiki/CreditsAndLicense">uses a few opensource projects.</a>.<br>
 * For help, please visit <a href="http://nativelibs4java.googlecode.com/">NativeLibs4Java</a> or <a href="http://bridj.googlecode.com/">BridJ</a> .
 */
@Library( "test" )
@Runtime( CPPRuntime.class )
public class TestLibrary
{
    static
    {
        BridJ.register();
    }
}

TestClass.java

package test;

import org.bridj.BridJ;
import org.bridj.Pointer;
import org.bridj.ann.Library;
import org.bridj.ann.Name;
import org.bridj.ann.Namespace;
import org.bridj.cpp.CPPObject;

/**
 * <i>native declaration : line 5</i><br>
 * This file was autogenerated by <a href="http://jnaerator.googlecode.com/">JNAerator</a>,<br>
 * a tool written by <a href="http://ochafik.com/">Olivier Chafik</a> that <a href="http://code.google.com/p/jnaerator/wiki/CreditsAndLicense">uses a few opensource projects.</a>.<br>
 * For help, please visit <a href="http://nativelibs4java.googlecode.com/">NativeLibs4Java</a> or <a href="http://bridj.googlecode.com/">BridJ</a> .
 */
@Name( "test_class" )
@Namespace( "test" )
@Library( "test" )
public class TestClass extends CPPObject
{
    static
    {
        BridJ.register();
    }

    /**
    * Original signature : <code>void run()</code><br>
    * <i>native declaration : line 10</i>
    */
    public native void run();

    public TestClass()
    {
        super();
    }

    public TestClass( Pointer pointer )
    {
        super( pointer );
    }
}

stdafx.h

#pragma once

#define WIN32_LEAN_AND_MEAN
#define NO_MINMAX

#include <windows.h>

stdafx.cpp

#include "stdafx.h"

dllmain.cpp

#include "stdafx.h"

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved )
{
    switch( ul_reason_for_call )
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

test_class.hpp

#pragma once

namespace test {

    class test_class
    {
    public:
        __declspec( dllexport ) test_class();

        __declspec( dllexport ) void run();
    };

}

test_class.cpp

#include "stdafx.h"
#include "test_class.hpp"
#include <iostream>

using namespace std;
using namespace test;

test_class::test_class()
{
    cout << "initializing..." << endl;
}

void test_class::run()
{
    cout << "run" << endl;
}

 以上。