How does Java call com compiled by VB through VC?

Source: Internet
Author: User

 

The following reproduced from: http://blog.csdn.net/tiger119/archive/2005/11/16/531081.aspx

 

 

A few days ago, a colleague asked me how to call com written in VB in Java. I used to know how to use native in Java and implement it through windows DLL, but I have never practiced it. I am busy today, I just tried it. The simplest example is written.
The procedure is very simple. Maybe someone may use it in the future. I will help you sort it out:

Step 1: generate the simplest COM (using vb60)

1.1: VB Code

Progid: testvbcom. clstest

Thread model: Multi-instance model of sta

Package COM. kingdee; <br/> public class testcallnative {<br/> static <br/>{< br/> system. loadlibrary ("callcomapi"); <br/>}< br/> Public native string callapi (string str1, int N); <br/> ...... <Br/>}

 

That's simple.

Step 2: Define the native method and export the output section declaration of Windows DLL (using eclipse3.1)

2.1 Java code:

Package COM. kingdee; <br/> public class testcallnative {<br/> static <br/>{< br/> system. loadlibrary ("callcomapi"); <br/>}< br/> Public native string callapi (string str1, int N); <br/> ...... <Br/>}

2.2: Export Declaration

Javah-JNI com. Kingdee. testcallnative

Obtain the com_kingdee_testcallnative.h file.
Note: This is a standard method for calling Java Virtual Machine and Windows DLL. The output name cannot be changed. In this example, the output name is:
Jniexport jstring jnicall java_com_kingdee_testcallnative_callapi
(Jnienv *, jobject, jstring, jint );

Step 3: Compile Windows DLL (using vc6.0)

First, I want to write a Windows DLL using MFC or SDK. In order to access COM more conveniently, I used MFC in my example and used part of ATL, however, you can actually use the simplest SDK method.

3.1: generate the api dll framework

A: Use the VC Wizard to generate a DLL in the MFC mode (Note: it is a conventional DLL and cannot be extended)
B: Define the def file content:
Exports
Java_com_kingdee_testcallnative_callapi @ 1

C: contains the. h file from the previous step.
D: add the necessary include directory in path or VC option.
For example, on my machine:
D:/program files/Java/jdk1.5.0/include
D:/program files/Java/jdk1.5.0/include/Win32

E: add the implementation process name.
Jniexport jstring jnicall parameters <br/> (jnienv * jenv, jobject, jstring jsinput, jint jninput) <br/>{< br/> jstring jsret; <br/> return jsret; <br/>}

3.2: code for calling COM. (Use the # import method to call standard Com)

Stdafx. h: <br/> # include <atlbase. h> <br/> # import ".. // vbcom // testvbcom. DLL "named_guids <br/> callcomapi. CPP <br/> # include "com_kingdee_testcallnative.h" <br/> void getjstring (jnienv * jenv, lpctstr lpszbuffer, jstring & JS) <br/>{< br/> uses_conversion; <br/> lpwstr PW = T2W (lpszbuffer); <br/> JS = jenv-> newstring (PW, wcslen (PW); <br/> return; <br/>}< br/> jniexport jstring jnicall java_com_kingdee_tes Tcallnative_callapi <br/> (jnienv * jenv, jobject, jstring jsinput, jint jninput) <br/>{< br/>:: coinitialize (null ); <br/> // convert input Params <br/> cstring sinput = jenv-> getstringutfchars (jsinput, 0); <br/> cstring sret; <br/> testvbcom :: _ clstest * Ptest = NULL; <br/> jstring jsret; <br/> try <br/> {<br/> // get com's interface <br/> If (failed (: cocreateinstance (testvbcom: clsid_clstest, null, c Lsctx_inproc, <br/> testvbcom: IID _ clstest, (lpvoid *) & Ptest) <br/> throw (_ T ("Get com's interface failed! "); <Br/> // call com's method <br/> _ bstr_t bstr1 (sinput); <br/> bstr1 = Ptest-> functest (bstr1, jninput); <br/> sret = bstr1.copy (); <br/> getjstring (jenv, sret, jsret); <br/>}< br/> catch (lpctstr lpszerr) <br/>{< br/> getjstring (jenv, lpszerr, jsret); <br/>}< br/> catch (...) <br/>{< br/> getjstring (jenv, _ T ("Unknown error! "), Jsret); <br/>}< br/> If (Ptest) Ptest-> release (); <br/>:: couninitialize (); <br/> return jsret; <br/>}
Step 4: compile Java testing (using eclipse 3.1)

4.1: Related paths for adding native:

The path on my machine is:
-Djava. Library. Path = E:/work/callcomforjava/example/callcomapi/release

4.2: Test code:

Public static void main (string [] ARGs) {<br/> system. out. println ("start... "); <br/> testcallnative testobj = new testcallnative (); <br/> system. out. println (testobj. callapi ("test", 1); <br/> system. out. println ("end... "); <br/>}

4.3: Run

Expected result:
Start...
Vbcom test: test1
End...

Simple, hey.

Note:

1: The debug version DLL written in vc6.0 cannot be normally called by Java. The reason is unknown and must be release.
2: the order of console output in the API is not as expected.


For example, add the following to the first end of an API:
Printf ("/napi DLL start..."); <br/> ...... <Br/> printf ("/napi dll ebd ...");

Run. The output on the console is:
Start...
Vbcom test: test1
End...
Api dll start...
Api dll end...

Stone on-11-16

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/tiger119/archive/2005/11/16/531081.aspx

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.