search
HomeComputer TutorialsComputer KnowledgeVC implementation method for writing DLL called by JAVA

VC implementation method for writing DLL called by JAVA

How to use VC to write a DLL for JAVA call

}(2) Compile the java file, and then compile to generate the header file (for vc use) javac GB2PY.java //Compile to generate class(3) Create the com/bw/gb2py folder in the current folder, and add GB2PY .class into the folder, return to the current folder, and call the javah command to generate the header file. javah com.bw.gb2py.GB2PY //Generate a header file, the file name is com_bw_gb2py_GB2PY.h. Note that the content in the header file cannot be changed, otherwise the generated dll cannot be used (4) Create a new Win32 dll project named GB2PY in VC, Copy com_bw_gb2py_GB2PY.h to the project directory and add it to the project. Then copy %JAVA_HOME%/include/jni.h and %JAVA_HOME%/include/win32/jni_md.h to the project directory and add it to the project. (5) Implement the method declared in com_bw_gb2py_GB2PY.h: Java_com_bw_gb2py_GB2PY_GetPY. Pay attention to the conversion of java string and char in c. Java's strings are encoded in unicode (double-byte), while char is single-byte. int (*GetPY)(char* szGBString,PY &oPY);JNIEXPORT jstring JNICALL Java_com_bw_gb2py_GB2PY_GetPY

(JNIEnv * env, jclass, jstring name){static HMODULE hModle = LoadLibrary("PYconvert.dll"); //Third-party dll

GetPY = (LPFUN)GetProcAddress(hModle,"GBToPY");int len;char charName[128];

char charPY[512];

len = env->GetStringLength(name) * 2 1; //Double-byte length is converted to single-byte length, adding 1 is to add the terminator

memset(charName, 0, len);

const wchar_t * w_buffer = env->GetStringChars(name, 0);

int wlen = wcslen(w_buffer);

len = WideCharToMultiByte(CP_ACP, 0, w_buffer, wcslen(w_buffer) 1, charName, len, NULL, NULL);

env->ReleaseStringChars(name, w_buffer); //The above code copies the contents of java string name to the char array charName... //Omit some function codes and convert Chinese characters by calling a third-party dll function is Pinyin, stored in the charPY array int slen = strlen(charPY);

jchar * buffer = new jchar[slen];

len = MultiByteToWideChar(CP_ACP, 0, charPY, strlen(charPY), buffer, slen);

if (len > 0 & len

buffer[len] = 0; //The above code converts char type string to jchar type string jstring js = env->NewString(buffer, len);

delete [] buffer;return js;}(6) Compile and generate a dll file, which can be called in java (you need to use the class generated in the second step)! :)

How to write a DLL file for the EXE file written by VC and let this EXE call the DLL

You mean how to write dll? , or how to call the dll you wrote?

How to write dll:vc 6.0, when creating a new project, you usually choose any of the following:

MFC AppWizard[dll] can use the dynamic link library of the MFC framework

Win32 Dynamic-link Library window32-bit standard dynamic library

Then select the framework you need, and then complete. The basic dll is just like this.

If you have vs2003 and above (vs2010 has not been used, so I won’t mention it), create a new

The following two types are commonly used in projects:

MFC->MFC Dll

Win32->Any one->Select the Dll option in the pop-up dialog box

Other options depend on your needs. Then it's done, and the basic dll is ready.

Call the dll you wrote: Use LoadLibrary("xxx.dll");

where you need to call it

xxx.dll is the dll you wrote, and then if you wrote

in xxx.dll

For interfaces, you can use the following methods to call methods in dll:

1. Include the interface header file of the dll in your exe project. For example, the interface function is void FunName(int a);

2. typedef void(*Func)(int); //Define interface

3. Use where needed:

........................

HMOUDLE hMyDll = LoadLibrary("xxx.dll");

if (hMyDll != NULL)

{

Func *pDllFun=(Func *)::GetProcAddress(hMyDll,"FunName");//FunName is the interface function in your Dll

if (pDllFun != NULL)

{

pDllFun(1); //Call the function in Dll

}

}

4. Uninstall the Dll when it is no longer needed. FreeLibrary(hMyDll);

/////////////////////////////////////////////////// ///////

Has your Dll been generated in the directory where the exe is located?

The above is the detailed content of VC implementation method for writing DLL called by JAVA. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:Excel办公网. If there is any infringement, please contact admin@php.cn delete
Fixes for ExpressVPN Not Connecting on Windows PC/Mac/iPhone - MiniToolFixes for ExpressVPN Not Connecting on Windows PC/Mac/iPhone - MiniToolMay 01, 2025 am 12:51 AM

Is ExpressVPN not connecting on iPhone, Android phone, Mac, or Windows PC? What should you do if ExpressVPN won’t connect or work? Take it easy and go to find solutions from this post. Here, php.cn collects multiple ways to solve this internet issue.

Targeted Solutions for 100% GPU Usage on Windows 10/11Targeted Solutions for 100% GPU Usage on Windows 10/11May 01, 2025 am 12:50 AM

Is 100% GPU usage bad? How to fix 100% GPU usage in Windows 10? Don’t fret. php.cn Website provides you with this article to work out high GPU issues. Those methods are worth a try if you are undergoing 100% GPU usage.

How to Download/Install/Add/Remove/Disable Add-ins in Word - MiniToolHow to Download/Install/Add/Remove/Disable Add-ins in Word - MiniToolMay 01, 2025 am 12:49 AM

You can use add-ins in Microsoft Word to get more features. This post introduces how to download, install, add or remove add-ins in Microsoft Word. A free file recovery method is also provided to help you recover deleted/lost Word or any other files.

Fix the System Backup Error Codes 0x807800A1 & 0X800423F3 - MiniToolFix the System Backup Error Codes 0x807800A1 & 0X800423F3 - MiniToolMay 01, 2025 am 12:48 AM

Some people find the system backup error codes 0x807800A1 & 0X800423F3 when they attempt to create a system backup. These codes will prevent you from doing any backup tasks. Don’t worry! This article on php.cn Website will teach you how to fix th

Windows 10 Enterprise ISO Download/Install, Easy Guide to LearnWindows 10 Enterprise ISO Download/Install, Easy Guide to LearnMay 01, 2025 am 12:47 AM

Want to get an ISO file of Windows 10 Enterprise to install it for business usage? Windows 10 Enterprise ISO download & install are easy and php.cn will show you how to download Windows 10 Enterprise ISO 20H2, 21H1, or 21H2 via some direct downlo

NordVPN Not Working on Windows 11: Here Are Easy Fixes - MiniToolNordVPN Not Working on Windows 11: Here Are Easy Fixes - MiniToolMay 01, 2025 am 12:46 AM

NordVPN not connecting or working on your Windows 11 computer? Do you know the reasons for this issue? If you want to solve this problem, do you know what you should do? If you have no idea, you come to the right place. In this post, php.cn Software

Is Twitter Down? How to Check It? How to Fix It? Read This Post! - MiniToolIs Twitter Down? How to Check It? How to Fix It? Read This Post! - MiniToolMay 01, 2025 am 12:45 AM

Nowadays, Twitter becomes more and more popular all over the world. However, sometimes you may find it is not working properly. Is Twitter down? How to check it? How to fix it? This post from php.cn provides details for you.

Redfall Release Date, Trailers, Pre-Order, and Latest News - MiniToolRedfall Release Date, Trailers, Pre-Order, and Latest News - MiniToolMay 01, 2025 am 12:44 AM

When does Redfall come to market? Redfall fans can’t wait to try this game. fortunately, the release date has been clarified and more information we know about Redfall will be all disclosed in this article on php.cn Website, so if you are interested,

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function