Home Computer Tutorials Computer Knowledge VC implementation method for writing DLL called by JAVA

VC implementation method for writing DLL called by JAVA

Jan 22, 2024 pm 04:57 PM

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Discover How to Fix Drive Health Warning in Windows Settings Discover How to Fix Drive Health Warning in Windows Settings Mar 19, 2025 am 11:10 AM

What does the drive health warning in Windows Settings mean and what should you do when you receive the disk warning? Read this php.cn tutorial to get step-by-step instructions to cope with this situation.

How do I edit the Registry? (Warning: Use with caution!) How do I edit the Registry? (Warning: Use with caution!) Mar 21, 2025 pm 07:46 PM

Article discusses editing Windows Registry, precautions, backup methods, and potential issues from incorrect edits. Main issue: risks of system instability and data loss from improper changes.

How do I manage services in Windows? How do I manage services in Windows? Mar 21, 2025 pm 07:52 PM

Article discusses managing Windows services for system health, including starting, stopping, restarting services, and best practices for stability.

How to Fix the Steam Cloud Error? Try These Methods How to Fix the Steam Cloud Error? Try These Methods Apr 04, 2025 am 01:51 AM

The Steam Cloud error can be caused by many reasons. To play a game smoothly, you need to take some measures to remove this error before you launch the game. php.cn Software introduces some best ways as well as more useful information in this post.

Windows Metadata and Internet Services Problem: How to Fix It? Windows Metadata and Internet Services Problem: How to Fix It? Apr 02, 2025 pm 03:57 PM

You may see the “A connection to the Windows Metadata and Internet Services (WMIS) could not be established.” error on Event Viewer. This post from php.cn introduces how to remove the Windows Metadata and Internet Services problem.

How do I change the default app for a file type? How do I change the default app for a file type? Mar 21, 2025 pm 07:48 PM

Article discusses changing default apps for file types on Windows, including reverting and bulk changes. Main issue: no built-in bulk change option.

How to Resolve the KB5035942 Update Issues – Crashing System How to Resolve the KB5035942 Update Issues – Crashing System Apr 02, 2025 pm 04:16 PM

KB5035942 update issues - crashing system commonly happens to users. Inflicted people hope to find a way out of the kind of trouble, such as crashing system, installation, or sound issues. Targeting these situations, this post published by php.cn wil

How do I use the Group Policy Editor (gpedit.msc)? How do I use the Group Policy Editor (gpedit.msc)? Mar 21, 2025 pm 07:48 PM

The article explains how to use the Group Policy Editor (gpedit.msc) in Windows for managing system settings, highlighting common configurations and troubleshooting methods. It notes that gpedit.msc is unavailable in Windows Home editions, suggesting

See all articles