资源描述:
《java调用c 语言生成的dll(java calls the dll generated by the c language)》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、java调用c++语言生成的dll(JavacallstheDLLgeneratedbythec++language)Javahowtocallc++towritetheDLL(DynamicLinkLibrary)SimpletouseJNIAsoneofthemostsimpleHelloWorldprogramtointroducethemostbasicuseofJNI:1)thefirsttohaveaHelloWorld.java.Thisisthemainfile,whichincludestheloca
2、lmethodJavastatement,amainfunction,andastaticcode,usedtoimportneededdynamiclinklibrary(windows.Dll).Thefollowingcode://HelloWorld.javaClass{HelloWorldPublicnativevoid(displayHelloWorld);//notethatthekeywordnative,whichshowsthismethodisrealizedbythelocalmethod.St
3、atic{//staticcodeintothehello.dllsection.System.loadLibrary("hello");}Publicstaticvoidmain(String[]args){//callsthedisplayHelloWorldmethodofthisclass(ofcourse,themethodisactuallyusingtheClanguage)New(HelloWorld)(.DisplayHelloWorld);}}2)compileHelloWorld.java.Use
4、statement:,JavacHelloWorld.java3)usethejavahcommandtogeneratea.Hfile.Usestatement:,JavahHelloWorldThisistherealizationofthedisplayHelloWorld(c)filemethodheaderfile.ThefilenamefortheHelloWorld.hCode:DONOTEDITTHIS/FILE-itismachinegenerated.#includeHeaderfor
5、classHelloWorld/**/#ifndef_Included_HelloWorld#define_Included_HelloWorld#ifdef__cplusplusExtern{"C"#endif**Class:HelloWorld*Method:displayHelloWorld*Signature:(V)*/JNIEXPORTvoidJNICALLJava_HelloWorld_displayHelloWorld(JNIEnv*jobject);#ifdef__cplusplus}#endif#en
6、difAsyoucansee,thisfileisthemainmethoddeclarationmethodsneedtobeimplementedintheCfileinsidethe.ThisstatementandthejavafileHelloWorld.javahasalittledifference,theoriginalmethodwithnoparameters,butnowwithtwoparameters.Thetwoisalocalmethodtosomeparameters.Thefirstp
7、arameterisJNIEnv*,whichisusedtoconnecttothelocalobjectparametersandmethodsinyourJavaapplications.Thesecondparameterisajobject,whichpointstothecurrentobject,youcanalsounderstanditasaJavainsidethethisvariable.Foralocalinstancemethod,suchasinthecaseofthedisplayHell
8、oWorldmethod,thejobjectparameterisanobjectinthecurrentinstance.Forlocalclass,thisparameterisamethodoftheclass.Theuseofthesetwoparametersisnotrequiredinthiscase.Anothe