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 itxxx.dll is the dll you wrote, and then if you wrote
in xxx.dllFor 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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.

OneDrive is an online cloud storage service from Microsoft. At times, you might find OneDrive fail to upload photos to the cloud. If you are on the same boat, keep reading this post from php.cn Software to get effective solutions now!

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

If you have a program called PC App Store on your computer and did not purposely install it, then your PC may be infected with the malware. This post from php.cn introduces how to remove PC App Store malware.

Is Brothers: A Tale of Two Sons Remake not launching? Encountering Brothers: A Tale of Two Sons Remake black screen? Here this post on php.cn offers you tested solutions to assist you in addressing this problem.

Chris Titus Tech has a tool called Windows Utility that can help you easily create a debloated Windows 11/10 ISO to install a clean system. php.cn offers a full guide on how to do this thing using the Chris Titus tool.

Many SurfaceBook users report that they meet the “core isolation blocked by ew_usbccgpfilter.sys” issue on Windows 11/10. This post from php.cn helps to fix the annoying issue. Keep on your reading.
