Article directory
Steps (ideas)
1. Create a c++ dynamic link library to encapsulate some functions, such as scanning devices, and use it as a third-party library. 2.
Create a c++ middleware (essentially also a DLL) to call and further encapsulate third-party libraries and add some services. Logic, etc.
3. Create a java project to call the functions of the C++ dynamic link library (third-party library)
1. Create a C++ dynamic link library (DLL)
1.1 Create a new dynamic link library
1. Open visual studio, create a new project, select the dynamic link library (DLL), and click Next
2. Modify the project name and location, and click Create
1.2 Configure dynamic link library
3. Right-click the source file and select Add->New Item
4. Select the C++ class named WindowsHidUsb and click Add
5. Write pch.h, WindowsHidUsb.h and WindowsHidUsb.cpp files
pch.h
#ifndef PCH_H
#define PCH_H
// 添加要在此处预编译的标头
#include "framework.h"
#define API_DLL _declspec(dllexport)
#endif //PCH_H
WindowsHidUsb.h
#pragma once
#include "pch.h"
API_DLL int scanDevice();
WindowsHidUsb.cpp
#include "WindowsHidUsb.h"
#include "pch.h"
API_DLL int scanDevice() {
return 2;
}
6. Right-click the project and click Generate
2. Create C++ middleware (dynamic link library)
2.1 Create middleware
The creation steps are the same as above. The project name is MtkHidUsbBridge. Add a cpp file named MtkHidUsbBridge.
2.2Configuring middleware
1. Right-click the project and click Properties
2. VC++ Directory-Include Directory-Edit
3. Click Add to add the include, include\win32 and directory of the local java jdk
4. Right-click the header file and add the WindowsHidUsb.h header file written before to the header file of the project folder and write the MtkHidUsbBridge.cpp file and the WindowsHidUsb.h header file
(1) MtkHidUsbBridge.cpp
#include "pch.h"
#include "com_mtk_api_usb_MtkUsbJni.h"
#include "WindowsHidUsb.h"
JNIEXPORT jint JNICALL Java_com_mtk_api_usb_MtkUsbJni_scanDevice
(JNIEnv*, jobject) {
return scanDevice();
}
(2) WindowsHidUsb.h header file, just replace the relative path with your own relative path
#pragma once
#if defined(WIN32)
#if defined(DEBUG)
#pragma comment(lib,"../../MtkHidUsb/x64/Debug/MtkHidUsb.lib")
#pragma message("完成与MtkHidUsb.lib的连接")
#else
#pragma comment(lib,"../../MtkHidUsb/x64/Release/MtkHidUsb.lib")
#pragma message("完成与MtkHidUsb.dll的连接")
#endif
#endif // defined(WIN32)
int scanDevice();
5. Right-click the project and click Generate. The generated directory is as follows. Remember first, it will be used in the next step.
3.Java calls C++ dynamic link library (third-party library)
3.1 Create a new empty project (omitted)
The creation process is as simple as following without any brain (just have hands)
3.2 Configure Java project
1. In the com.mtk.api.usb folder, right-click to create a new MtkUsbJNI.java class, as follows
package com.mtk.api.usb;//与自己的文件夹结构对应
class MtkUsbJni {
native int scanDevice();
}
2. Open the dos window, find the src directory of the java project, enter cmd in the address bar and press Enter, enter the following command
javah -jni -encoding UTF-8 com.mtk.api.usb.MtkUsbJNI
Among them, com.mtk.api.usb.MtkUsbJNI is the actual package path of the MtkUsbJNI class in your java project.
3. Add the generated com_mtk_api_usb_MtkUsbJni.h header file to the header file directory of MtkHidUsbBridge,
right-click the header file - Add - Existing Item -Add it after finding it, and configure the project again (add the storage directory of the header file to the VC++ directory-include directory)
4. Add the generated MtkHidUsbBridge.dll to the root directory of the java project
5. In com.mtk.api Under the .usb package, create the following MtkUsb class
package com.mtk.api.usb;
public class MtkUsb {
private static final MtkUsb mtkUsb = new MtkUsb();
public static MtkUsb getInstance(){
return mtkUsb;
}
private static final MtkUsbJni mtkUsbJni = new MtkUsbJni();
public int scanDevice(){
return mtkUsbJni.scanDevice();
}
}
6. Create a new test class under the com.mtk.test package
package com.mtk.test;
import com.mtk.api.usb.MtkUsb;
public class MtkHidUsbTest {
public static void main(String[] args) {
System.loadLibrary("MtkHidUsb");
System.loadLibrary("MtkHidUsbBridge");
System.out.println("Device Count=" + MtkUsb.getInstance().scanDevice());
}
}
6. Copy the two dynamic link libraries generated above, MtkHidUsbBridge.dll and MtkHidUsb.dll to the root directory of java, right-click and run.