views:

57

answers:

0

I'm trying to write a MATLAB function to find the position in screen coordinates of an arbitrary window (not just a MATLAB window). The code only needs to work in the current version of MATLAB and on Windows XP.

I know I could write a MEX-file or shell out to another program, but I want to do this all with MATLAB code. It should be possible to use the MATLAB Interface to Shared Libraries to hook into the GetWindowRect Windows API function.

Sadly, loadlibrary doesn't seem to be able to parse user32.dll, even when I give it user32.h, so I'm forced to write my own prototype file. I can successfully use FindWindowA to get the window handle, but I can't seem to get the signature right for GetWindowRect.

My best attempt follows. It throws a segmentation violation.

function x = getWindowRect(name)

libName = 'getWindowRect';
if libisloaded(libName)
    unloadlibrary(libName)
end
loadlibrary('user32.dll',@userproto,'alias',libName);

h = calllib(libName,'FindWindowA',[],name);
x = calllib(libName,'GetWindowRect',h);

%===============================================================================
function [fcns,structs,enuminfo,ThunkLibName] = userproto
fcns=[]; structs=[]; enuminfo=[]; fcns.alias={}; ThunkLibName=[];

% HWND [ gle ] FindWindowA ( LPCSTR lpClassName , LPCSTR lpWindowName ); 
fcns.name{1} = 'FindWindowA';
fcns.calltype{1} = 'stdcall';
fcns.LHS{1} = 'int16Ptr';
fcns.RHS{1} = {'string','string'};

% FailOnFalse [ gle ] GetWindowRect ( HWND hWnd , [ out ] LPRECT lpRect ); 
fcns.name{2} = 'GetWindowRect';
fcns.calltype{2} = 'stdcall';
fcns.LHS{2} = 'c_struct';
fcns.RHS{2} = {'int16Ptr'};

I suspect the trouble is in this line:

fcns.LHS{2} = 'c_struct';

I'm not sure, though. I'd appreciate any help!