How to use SendMessageA to send WM_GETTEXT to a window
16 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I'm trying to use user32.dll SendMessageA, and I'm not able to make the following very basic example work.
My example is divided in 4 parts :
- Part 1 creates a protofile and successfully loads FindWindowA and SendMessageA from user32.dll
- Part 2 successfully finds the handle of the main Matlab window
- Part 3 fails at getting the title of the window. LResult should contain the number of characters copied into the buffer, but it's null, and my buffer is unchanged. My understanding is that 0x000D is the constant value of WM_GETTEXT
- Part 4 unload the libraries and delete the protofile
I'm not sure if my protofile is correct, if the way I'm using pointers is correct, and if the way i'm using SendMessageA is correct.
Below is my code :
%%%%Part 1
clear all classes
global fcns structs enums; structs = []; enums = []; fcns.alias = {''};
% Create protofile
% HWND _stdcall FindWindowA(LPCSTR,LPCSTR);
fcns.name{1} = 'FindWindowA';
fcns.calltype{1} = 'cdecl';
fcns.LHS{1} = 'voidPtr';
fcns.RHS{1} = {'int8Ptr', 'string'};
% LRESULT _stdcall SendMessageA(HWND,UINT,WPARAM,LPARAM);
fcns.name{2} = 'SendMessageA';
fcns.calltype{2} = 'cdecl';
fcns.LHS{2} = 'doublePtr';
fcns.RHS{2} = {'voidPtr', 'uint32', 'uint32', 'voidPtr'};
% Save protofile
save('LoadLib.mat', 'enums', 'fcns', 'structs');
% Load user32.dll
if ~libisloaded('WinUser32Lib'), loadlibrary('user32.dll','LoadLib.mat','alias','WinUser32Lib');
end
%%%%Part 2
% Find MatLab Window Handle...
WHand = calllib('WinUser32Lib', 'FindWindowA', [], char('MATLAB R2017b'));
WHand.isNull %False means it worked
%%%%Part 3
% Create text buffer
BuffSize = 99;
BuffPt = libpointer('voidPtr',[int8(repmat('x',1,BuffSize)) 0]);
% Send message
LResult = calllib('WinUser32Lib', 'SendMessageA', WHand, hex2dec('000D'), BuffSize+1, BuffPt);
LResult.isNull % True means it didn't worked...
char(BuffPt.Value) % Value has not been changed by SendMessage
%%%%Part 4
% Unload user32.dll
if libisloaded('WinUser32Lib'), unloadlibrary 'WinUser32Lib'; end
if exist('LoadLib.mat', 'file')==2, try delete('LoadLib.mat'); end %#ok<TRYNC>
end
1 commentaire
Eddie Irvine
le 8 Mai 2020
Modifié(e) : Eddie Irvine
le 17 Sep 2020
Hello Gauthier Abrial,
I just copy-pasted and your code works with my configuration:
Windows 10
Windows SDK 10
Matlab 2020a
After replacing the the code segment in part 2
WHand = calllib('WinUser32Lib', 'FindWindowA', [], char('MATLAB R2017b'));
with
WHand = calllib('WinUser32Lib', 'FindWindowA', [], char('MATLAB R2020a'));
The output is
'MATLAB R2020a xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx '
That is, your code seems as working to me...
With kind regards,
EI
Réponses (0)
Voir également
Catégories
En savoir plus sur Call C from MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!