Note that there are some explanatory texts on larger screens.

plurals
  1. POWin32 File Properties dialog not displaying in Java Swing app via JNI
    primarykey
    data
    text
    <p>I have a Java Swing application with a couple of pieces of native functionality added via JNI. One thing this application does is show a tree view of the file system. I was trying to write a little JNI so that, on Windows systems, I could provide a "Properties..." context menu item that displayed the standard file "Properties" dialog from Windows.</p> <p>I scraped the internet for the Win32 code needed to do this, and came up with this:</p> <pre><code>JNIEXPORT jboolean JNICALL Java_com_foobar_showFilePropertiesDialogImpl (JNIEnv *env, jobject obj, jlong hwnd, jstring fileName) { LPITEMIDLIST pidl; LPCITEMIDLIST pidlItem; HRESULT hr; IShellFolder *pFolder; IContextMenu *pContextMenu; CMINVOKECOMMANDINFO cmi; const wchar_t *pszFile; if (!coInitialized) { #ifdef DEBUG MessageBoxW(NULL, L"Initializing COM (should happen just once)...", L"Initializing COM...", MB_OK); #endif CoInitialize(NULL); coInitialized = true; } /* Get the name of the file. */ pszFile = (wchar_t *)env-&gt;GetStringChars(fileName, NULL); if (pszFile==NULL) { /* Exception occurred */ return JNI_FALSE; } hr = SHGetDesktopFolder(&amp;pFolder); if (FAILED(hr)) { env-&gt;ReleaseStringChars(fileName, (const jchar *)pszFile); return JNI_FALSE; } hr = pFolder-&gt;ParseDisplayName(HWND_DESKTOP, NULL, (LPTSTR)pszFile, NULL, &amp;pidl, NULL); pFolder-&gt;Release(); if (FAILED(hr)) { env-&gt;ReleaseStringChars(fileName, (const jchar *)pszFile); return JNI_FALSE; } hr = SHBindToParent(pidl, IID_IShellFolder, (void **)&amp;pFolder, &amp;pidlItem); if (FAILED(hr)) { SHFree(pidl); env-&gt;ReleaseStringChars(fileName, (const jchar *)pszFile); return JNI_FALSE; } hr = pFolder-&gt;GetUIObjectOf(HWND_DESKTOP, 1, (LPCITEMIDLIST *)&amp;pidlItem, IID_IContextMenu, NULL, (void **)&amp;pContextMenu); pFolder-&gt;Release(); if(SUCCEEDED(hr)) { ZeroMemory(&amp;cmi, sizeof(cmi)); cmi.cbSize = sizeof(cmi); if (hwnd&gt;0) { cmi.hwnd = (HWND)hwnd; } cmi.lpVerb = "properties"; cmi.nShow = SW_SHOWNORMAL; hr = pContextMenu-&gt;InvokeCommand(&amp;cmi); #ifdef DEBUG if (FAILED(hr)) { wchar_t msg[2048]; wsprintf(msg, L"InvokeCommand failed: %d - %x", SUCCEEDED(hr), hr); MessageBoxW(NULL, pszFile, msg, MB_OK); } else { MessageBoxW(NULL, L"InvokeCommand successful!", L"InvokeCommand Status", MB_OK); } #endif } pContextMenu-&gt;Release(); SHFree(pidl); env-&gt;ReleaseStringChars(fileName, (const jchar *)pszFile); return JNI_TRUE; } </code></pre> <p>From Java, I run this method from the EDT. Unfortunately, the properties dialog doesn't display <em>if</em> DEBUG is not defined. It seems that if I define DEBUG, the properties dialog will pop up after the "InvokeCommand successful!" MessageBox.</p> <p>Another thing I noticed is, when DEBUG is not defined, I call this method (and the properties dialog is not displayed), it <em>will</em> display later on in the application if I open a different "native" window. For example, I have code that opens the native Delete dialog via SHFileOperation (which works); as soon as I display that dialog, any File properties dialogs that didn't originally display will suddenly appear.</p> <p>It seems as though I'm doing stuff on the wrong thread perhaps. Is the EDT not the thread I want to display native windows from? But I'm confused because again, the delete dialog is working fine.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload