One of Jacob Technology Discussion

zhaozj2021-02-17  54

Recently, I came to the mailbox. I found that there were many netizens who were asking me about Jacob. However, for the sake of livelihood, trivial things, business, emotions, family, I thought it was really difficult, here I will slowly share my understanding and application for Jacob. At the same time, I apologize for those who fail to reply.

First, if we need to use Jacob to have some basic understanding of JNI, some JNI applications, so we don't even understand Jacob's running mechanism. If we are familiar with C / C , we can enhance our understanding. The next simple COM object is created, and then demonstrates how to call in C , understand this, and we combine the C method to call this simple COM object through one intermediate C method.

1. Create a COM object, as follows, simply call the MessageBox pop-up message box.

// HelloWorld.cpp: Implementation of Chelloworld

#include "stdafx.h"

#include "cpluscom.h"

#include "HelloWorld.h"

/

// chelloworld

STDMETHODIMP Chelloworld :: SayHelloworld ()

{

// Todo: Add Your Implementation Code Here

MessageBox (NULL, "Hello World", "CPluscom.HelloWorld", 1);

Return S_OK;

}

This project creates a simple COM in VC6.0, only this interface method is added.

2. Then, we write a console's C program to call this COM object (Client.cpp)

#include

#include "cpluscom.h"

Const IID IID_IHELLOWORLD = {0x87A437D2, 0X1210, 0X4Cec, {0x99, 0x67, 0xB2, 0X75, 0X23, 0X4C, 0X3B, 0X64}}

Const CLSID CLSID_HELLOWORLD = {0xCB85B082, 0XB040, 0X4865, {0x9A, 0xEF, 0x61, 0XCE, 0x0E, 0xE1, 0XAC, 0XB1}}

// These two constants are copied in the IIDS and CLSIDS declaration file generated by MIDL.

INT Main (int Argc, char * argv []) {

Coinitialize (NULL); // init com subsystem

Ihelloworld * phelloworld;

HRESULT HR = CoCreateInstance (CLSID_HELLOWORLD, NULL, CLSCTX_ALL, IID_ID_IHELLOWORLD, (Void **) & Phelloworld);

IF (succeededed (hr)) {

Phelloworld-> SayhelloWorld (); // use the object

}

Phelloworld-> Release (); // free the Object

Couninitialize ();

Return 0;

}

Compile CL Client.cpp / Link Ole32.lib

Generate client.exe, execute Client.exe, pop up the message window.

3. Write a Java application to call this COM, here you need to use the second technology. First we write a Java class that contains local methods.

Public class jclient {static {

System.loadLibrary ("CPLUSCOMBRIDGE");

}

Static native void cplusMessagebox ();

Public static void main (String [] args) {

CPlusMessageBox ();

}

}

Compile, then generate a local method file with Javah, then implement the local method, and implement the COM object in the local method.

#include "jclient.h"

#include "cpluscom.h"

#include

Const IID IID_IHELLOWORLD = {0x87A437D2, 0X1210, 0X4Cec, {0x99, 0x67, 0xB2, 0X75, 0X23, 0X4C, 0X3B, 0X64}}

Const CLSID CLSID_HELLOWORLD = {0xCB85B082, 0XB040, 0X4865, {0x9A, 0xEF, 0x61, 0XCE, 0x0E, 0xE1, 0XAC, 0XB1}}

/ *

* Class: JClient

* Method: CPlusMessageBox

* Signature: () V

* /

JNIEXPORT VOID JNICALL JAVA_JCLIENT_CPLUSMESSAGEBOX

(JNIENV * ENV, JCLASS CLS) {

Coinitialize (NULL); // init com subsystem

Ihelloworld * phelloworld;

HRESULT HR = CoCreateInstance (CLSID_HELLOWORLD, NULL, CLSCTX_ALL, IID_ID_IHELLOWORLD, (Void **) & Phelloworld);

IF (succeededed (hr)) {

Phelloworld-> SayhelloWorld (); // use the object

}

Phelloworld-> Release (); // free the Object

Couninitialize ();

}

This inside method body is exactly the same as the second step. Compilation intermediate local method

CL jclient.cpp -ie: /j2sdk1.4.2/include -ie: /j2sdk1.4.2/include/win32 / ld / link ole32.lib /out:cpluscombridge.dll

Finally, we can run a job from Java to call this COM.

This is the process of the entire Java call COM. In the following article, let 's discuss Jacob in actual applications.

转载请注明原文地址:https://www.9cbs.com/read-29329.html

New Post(0)