File Exchange

image thumbnail

getHWnd - retrieve the underlying Windows handle for Matlab figure(s)

version (4.96 KB) by Yair Altman
Enable C++ GUI integration by retrieving the HWnd handle of matlab figure(s)


Updated 28 Jun 2018

View License

hWnd = gethwnd(hFig)

HWnd = GETHWND retrieves the current figure (gcf)'s underlying Windows handle, thus enabling integration of Matlab GUI in C/C++/C# programs.
HWnd = GETHWND(hFig) retrieves a specific figure's underlying Windows handle. hFig is a Matlab handle, or a list of handles (not necessarily figure handle(s) - the handles' containing figure is used).

Windows-only (duh!); Java must be enabled

This code heavily relies on undocumented and unsupported Matlab functionality. It works on Matlab 7+, but use at your own risk!

Bugs and suggestions:
Please send to Yair Altman (altmany at gmail dot com)

Cite As

Yair Altman (2020). getHWnd - retrieve the underlying Windows handle for Matlab figure(s) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (7)

Ann Cherry

I have been using this function successfully for years. With R2019b I now get the a warning that the JavaFrame figure property will be removed in a future release.

Yair Altman

@Raym - no


Hi, Yair Altman
Is it possible to get handle to any window such as a word/pdf/older/text editor window... outside matlab with little modification of this function?

Yair Altman

@Adam - I haven't updated this utility since 2011 so when MathWorks updated the entire GUI infrastructure in R2014b (HG2), this utility stopped working for new figures. Nobody complained until you did today, so I never noticed that this utility needs to be updated.

Anyway, I now uploaded a fixed version.


Seems to need a

jframe = jf.fHG2Client.getWindow;

option for supporting the post R2014b HG2 graphics now, but with that added in it seems to work fine for me. Haven't actually used the value it returns with this yet to see if it is correct, but it returns a value instead of erroring.

Alexander H

Pablo Tacconi


Is it possible to interact with a Window Handle (HWnd) directly from Matlab.
get(HWnd) does not return anything, as Matlab is not recognizing HWnd as an object.

This would be very useful in several cases like when trying to control an external application using the COM interfaces. A good case is controlling the Google Earth Window (see



Fix for R2014b onward (HG2)

Fix for R2011b

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux