jna CoCreateInstance -
am trying create link in windows 7 has icon. using jna library this. having problem phone call cocreateinstance. returns error. first not sure if guid ishelllink right ( not windows programer). below code far. 1 time pointer ishelllink need fill paramaters link (target,icon, description, etc). modeling phone call c code below found @ link (http://www.codeproject.com/articles/11467/how-to-create-short-cuts-link-files). know utilize mklink command have alternative add together icon , description.
private void createlink(){ pointer reserved = null; //must null int dwcoinit = 0; hresult oleinitresult = ole32.instance.coinitializeex(reserved,dwcoinit); if(oleinitresult.equals(w32errors.s_ok)){ guid rclsid = ole32util.getguidfromstring("{e82a2d71-5b2f-43a0-97b8-81be15854de8}"); //shell object guid riid = ole32util.getguidfromstring("{000214ee-0000-0000-c000-000000000046}"); //clsid of ishelllink object pointerbyreference ppv = new pointerbyreference(); hresult olecreateresult = ole32.instance.cocreateinstance(rclsid,null,objbase.clsctx_inproc,riid,ppv); if(olecreateresult.equals(w32errors.s_ok)){ }else{ system.out.println("failed create link error "+olecreateresult.intvalue()); } } ole32.instance.couninitialize(); }
###################### c++ sample code /* -------------------------------------------------------------------------------- description: creates actual 'lnk' file (assumes com has been initialized). parameters: psztargetfile - file name of link's target, must non-empty string. psztargetargs - command line arguments passed link's target, may empty string. pszlinkfile - file name of actual link file, must non-empty string. pszdescription - description of linked item. if empty string description not set. ishowmode - showwindow() constant link's target. utilize 1 of: 1 (sw_shownormal) = normal window. 3 (sw_showmaximized) = maximized. 7 (sw_showminnoactive) = minimized. if 0 showmode not set. pszcurdir - working directory of active link. if empty string directory not set. psziconfile - file name of icon file used link. if empty string icon not set. iiconindex - index of icon in icon file. if < 0 icon not set. returns: hresult value >= 0 success, < 0 failure. -------------------------------------------------------------------------------- */ static hresult createshortcut(lpstr psztargetfile, lpstr psztargetargs, lpstr pszlinkfile, lpstr pszdescription, int ishowmode, lpstr pszcurdir, lpstr psziconfile, int iiconindex) { hresult hres; /* returned com result code */ ishelllink* pshelllink; /* ishelllink object pointer */ ipersistfile* ppersistfile; /* ipersistfile object pointer */ word wszlinkfile[max_path]; /* pszlinkfile unicode string */ int iwidecharswritten; /* number of wide characters written */ hres = e_invalidarg; if ( (psztargetfile != null) && (strlen(psztargetfile) > 0) && (psztargetargs != null) && (pszlinkfile != null) && (strlen(pszlinkfile) > 0) && (pszdescription != null) && (ishowmode >= 0) && (pszcurdir != null) && (psziconfile != null) && (iiconindex >= 0) ) { hres = cocreateinstance(&clsid_shelllink, /* pre-defined clsid of ishelllink object */ null, /* pointer parent interface if part of aggregate */ clsctx_inproc_server, /* caller , called code in same process */ &iid_ishelllink, /* pre-defined interface of ishelllink object */ &pshelllink); /* returns pointer ishelllink object */ if (succeeded(hres)) { /* set fields in ishelllink object */ hres = pshelllink->lpvtbl->setpath(pshelllink, psztargetfile); hres = pshelllink->lpvtbl->setarguments(pshelllink, psztargetargs); if (strlen(pszdescription) > 0) { hres = pshelllink->lpvtbl->setdescription(pshelllink, pszdescription); } if (ishowmode > 0) { hres = pshelllink->lpvtbl->setshowcmd(pshelllink, ishowmode); } if (strlen(pszcurdir) > 0) { hres = pshelllink->lpvtbl->setworkingdirectory(pshelllink, pszcurdir); } if (strlen(psziconfile) > 0 && iiconindex >= 0) { hres = pshelllink->lpvtbl->seticonlocation(pshelllink, psziconfile, iiconindex); } /* utilize ipersistfile object save shell link */ hres = pshelllink->lpvtbl->queryinterface(pshelllink, /* existing ishelllink object */ &iid_ipersistfile, /* pre-defined interface of ipersistfile object */ &ppersistfile); /* returns pointer ipersistfile object */ if (succeeded(hres)) { iwidecharswritten = multibytetowidechar(cp_acp, 0, pszlinkfile, -1, wszlinkfile, max_path); hres = ppersistfile->lpvtbl->save(ppersistfile, wszlinkfile, true); ppersistfile->lpvtbl->release(ppersistfile); } pshelllink->lpvtbl->release(pshelllink); } } homecoming (hres); }
it turn out simple. if nail page save couple days next instructions: generate mappings shell32.dll per instructions in com4j site: http://com4j.java.net/tutorial.html phone call next create icons. simple 1 time know.
iwshshell3 shelllink = classfactory.createwshshell(); com4jobject obj = shelllink.createshortcut(linkpath.getabsolutepath()); iwshshortcut link = obj.queryinterface(iwshshortcut.class); link.workingdirectory(desktoppath.getabsolutepath()); link.description("my link"); link.arguments(" hello "); link.iconlocation(iconpath.getabsolutepath()); link.targetpath(javawspath.getabsolutepath()); link.setname("test link"); link.save();
jna
No comments:
Post a Comment