Java calls C#

Since the project adopts Hybrid hot update and adopts pure C# development, the current battle is from the client to the server (the client provides the dll), and the server is responsible for calling, so it is necessary to use Java to adjust the C# dll logic.

1. JNI: does not support generics (pb) and cannot be converted to C++ successfully

2. JNA: Pro test is available. Principle Java calls C++, C++ calls C#

Steps:

        1. Generate C#dll and put it in the jdk and bin directory used by the java project

using System.Collections.Generic;
using System.IO;
using System;
using System.Collections;
using System.Linq;
using System.Reflection;
using Proto;

namespace CScriptDll
{
    public class JCSTest
    {
        public int Sum(int _x, int _y)
        {
            return _x + _y;
        }
        
        public int GetInt()
        {
            return new TestClass().Str;
        }
        public static byte[] GetBytes()
        {
            return File.ReadAllBytes("GlobalConfig.bytes");
        }
        public float GetGobal()
        {
            //此处通过pb读取表
            GlobalConfigMgr.LockLoadTable();
            return GlobalConfigMgr.GetFloatVal(EGlobal.Advertisements);
        }
    }
    public class TestClass
    {
        public int Str = 1996;
    }

}

         2. Create C++ dll.

        Add a reference, refer to the exported C# dll file, and then output the code as follows:

#include "pch.h"

#ifdef MYLIBAPI 
#else 
#define  MYLIBAPI  extern "C" __declspec(dllimport)     
#endif 
MYLIBAPI int add(int a, int b); //添加函数声明
MYLIBAPI int GetInt();
MYLIBAPI float GetGobal();

using namespace System;
using namespace CScriptDll;

int add(int a, int b) {
    JCSTest^ test = gcnew JCSTest();
    return test->Sum(a, b);
}

int GetInt() {
    JCSTest^ test = gcnew JCSTest();
    return test->GetInt();
}
float GetGobal() {
    JCSTest^ test = gcnew JCSTest();
    return test->GetGobal();
}

Note: Properties -> General -> Configuration Type (Dynamic Library.dll)

        Properties -> Advanced -> Common Language Runtime Support (/clr)

       Error: Severity Code Description Project File Line Suppressed Status Error C7681 C++/CLI or C++/CX does not support two-phase name lookup; use /Zc:twoPhase-  

        Getting the above error: Properties -> C/C++/Language -> Conforms to Pattern (No)

       3. JAVA call: Import two jar packages and add them to the library. jna address:

GitHub - java-native-access/jna: Java Native Access

package org.example;

import java.io.IOException;
import com.sun.jna.Library;
import com.sun.jna.Native;
public class Main {
    public static void main(String[] args) throws IOException {
        JavaToCS clib = JavaToCS.Instance;
        System.out.println("测试返回结果:"+clib.add(9965, 13));
        System.out.println("测试返回结果getInt :"+clib.GetInt());
        System.out.println("测试返回结果getInt :"+clib.GetGobal());

    }
}
interface JavaToCS extends Library {
    JavaToCS Instance = (JavaToCS) Native.loadLibrary("E:\\VSProject\\JavaToCPP\\x64\\Debug\\JavaToCPP.dll",JavaToCS.class);

    /*需要调用的方法,方法名与c++方法名相同*/
   int add(int a,int b);
    int GetInt();
    float GetGobal();
}

 operation result:

        

Guess you like

Origin blog.csdn.net/LM514104/article/details/130713045