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

VC implementation method for writing DLL called by JAVA

PHPz
Release: 2024-01-22 16:57:16
forward
961 people have browsed it

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!

source:docexcel.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template