Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8621859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:54:47+00:00 2026-06-12T06:54:47+00:00

I have a Java Swing application with a couple of pieces of native functionality

  • 0

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.

I scraped the internet for the Win32 code needed to do this, and came up with this:

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->GetStringChars(fileName, NULL);
    if (pszFile==NULL) { /* Exception occurred */
        return JNI_FALSE;
    }

    hr = SHGetDesktopFolder(&pFolder);
    if (FAILED(hr))
    {
        env->ReleaseStringChars(fileName, (const jchar *)pszFile);
        return JNI_FALSE;
    }

    hr = pFolder->ParseDisplayName(HWND_DESKTOP, NULL, (LPTSTR)pszFile, NULL, &pidl, NULL);
    pFolder->Release();
    if (FAILED(hr))
    {
        env->ReleaseStringChars(fileName, (const jchar *)pszFile);
        return JNI_FALSE;
    }

    hr = SHBindToParent(pidl, IID_IShellFolder, (void **)&pFolder, &pidlItem);
    if (FAILED(hr))
    {
        SHFree(pidl);
        env->ReleaseStringChars(fileName, (const jchar *)pszFile);
        return JNI_FALSE;
    }

    hr = pFolder->GetUIObjectOf(HWND_DESKTOP, 1, (LPCITEMIDLIST *)&pidlItem, IID_IContextMenu, NULL, (void **)&pContextMenu);
    pFolder->Release();
    if(SUCCEEDED(hr))
    {
        ZeroMemory(&cmi, sizeof(cmi));
        cmi.cbSize = sizeof(cmi);
        if (hwnd>0)
        {
            cmi.hwnd = (HWND)hwnd;
        }
        cmi.lpVerb = "properties";
        cmi.nShow = SW_SHOWNORMAL;
        hr = pContextMenu->InvokeCommand(&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->Release();
    SHFree(pidl);
    env->ReleaseStringChars(fileName, (const jchar *)pszFile);

    return JNI_TRUE;
}

From Java, I run this method from the EDT. Unfortunately, the properties dialog doesn’t display if DEBUG is not defined. It seems that if I define DEBUG, the properties dialog will pop up after the “InvokeCommand successful!” MessageBox.

Another thing I noticed is, when DEBUG is not defined, I call this method (and the properties dialog is not displayed), it will 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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:54:48+00:00Added an answer on June 12, 2026 at 6:54 am

    Instead of digging through the Shell interfaces manually, try using ShellExecute() instead and let the OS do the heavy work for you:

    JNIEXPORT jboolean JNICALL Java_com_foobar_showFilePropertiesDialogImpl 
      (JNIEnv *env, jobject obj, jlong hwnd, jstring fileName) 
    { 
        /* Get the name of the file. */ 
        const wchar_t *pszFile = (wchar_t *)env->GetStringChars(fileName, NULL); 
        if (pszFile==NULL) { /* Exception occurred */ 
            return JNI_FALSE; 
        } 
    
        int iErr = (int) ShellExecuteW((HWND)hwnd, L"properties", pszFile, NULL, NULL, SW_SHOWNORMAL);
        env->ReleaseStringChars(fileName, (const jchar *)pszFile); 
    
        /*
        wchar_t msg[36];
        if (iErr > 32)
            wsprintfW(msg, L"ShellExecute successful!"); 
        else 
            wsprintfW(msg, L"ShellExecute failed: %d", iErr); 
        MessageBoxW((HWND)hwnd, pszFile, msg, MB_OK); 
        */
    
        return JNI_TRUE; 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created one java-swing application. The application runs perfectly runs perfect on my
The scenario is this: I have Java swing application with JFrame. There is textarea
I have this java swing application that I intend to sell over the internet.
I have already developed a JAVA SWING Application . I want to install this
I have this Java Swing application that starts a new thread that uses a
I have a Java Swing application that is being used as a cluster application.
I have a Java swing application with a button that produces a popup window
So I have a strange problem, I have a java swing application that has
I've creating a Java Swing application and I realized that I have many many
I would like to create extend a Java Swing application to have a look

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.