Archive: SendMessage


SendMessage
dear all,

I want to do the following:

Matlab is some sort of command-prompt mathematics program and I want to install a plugin into this program. This can only be done by Matlab itself, so you should type the command and Enter and then it's installed.
Is it possible to automate this using SendMessage? And if so, can anyone give me a link to a site where I can find more codes for possible messages the the matlabwindow, because I'm not experienced in c-programming...

thx a lot all, greetz, Hendri.


Did you try to use:
X:\whatever\matlab\matlab.exe < installplugin.txt

when installplugin.txt is the commands you need to enter to matlab?


thx, i tried, but it didn't work...
but maybe there are other possibilities for the command line, so thx for your idea, greetz,

Hendri,


well, matlab does not support command-line arguments at startup, so if anyone knows a way how to use SendMessage for this problem, this would bevery helpfull.

Thx, greetz, Hendri.


Oops... If you DID read this message before I changed it, please ignore it. It was a mistake.

And now that I think about it, you just can't do it with SendMessage. SendMessage only works on windows, and if you say that Matlab is command prompt program then you just can't do it. :(

Here is a quote from MSDN:

The SendMessage function sends the specified message to a window or windows. The function calls the window procedure for the specified window and does not return until the window procedure has processed the message.
Sorry... :(

kichik, thx for your reply...
I realize that it's indeed not possible with SendMessage. I will take a look at the website of Matlab. Maybe they can give me a hint on how to interact with Matlab....

Thx anyway, greetz, Hendri.


Mmm, but a command prompt is just another window, so it should be possible.

Try this..

Name "Test"
OutFile "test.exe"

; Send a simple command to a Command Prompt window (works in Win2K)
Section
FindWindow $R0 "" "Command Prompt"
; or, more generic -
; FindWindow $R0 "ConsoleWindowClass"
IntCmp $R0 0 NoWindow
MessageBox MB_ICONINFORMATION|MB_OK "Window found ($R0)"
Push $R0
Push "dir$\r"
Call SendChars
Return
NoWindow:
MessageBox MB_ICONEXCLAMATION|MB_OK "No window found!"
SectionEnd

; SendChars:
; Send a stream of WM_CHAR messages to a window to simulate user input
; Usage:
; Push <window-handle>
; Push <message>
; Call SendChars
Function SendChars
Exch $R0 ; Get string
Exch
Exch $R1 ; Get hwnd
Push $R2
Push $R3
StrCpy $R2 0 ; Char index
loop:
StrCpy $R3 $R0 1 $R2 ; Get next char
StrCmp $R3 "" done
Push $R3
Call CharToAscii ; Convert it to ASCII
Pop $R3
SendMessage $R1 258 $R3 0 ; Send a WM_CHAR message
IntOp $R2 $R2 + 1
Goto loop
done:
Pop $R3
Pop $R2
Pop $R1
Pop $R0
FunctionEnd

; CharToAscii:
; Convert a character to it's ascii code
; Usage:
; Push <character>
; Call CharToAscii
; Pop <ascii-code>
;
; Surely there must be a simpler way of doing this!
; Am I missing something obvious??
Function CharToAscii
Exch $R0 ; Get character
Push $R1
Push $R2
StrCpy $R1 255 ; ASCII code test counter
loop:
IntFmt $R2 "%c" $R1 ; Convert current test counter to it's char
StrCmp $R2 $R0 done ; Have we found it yet? (note - not case sensitive)
IntOp $R1 $R1 - 1 ; Next code
IntCmp $R1 0 "" "" loop
done:
Pop $R2
Exch
Pop $R0
Exch $R1
FunctionEnd
--
Dave.

Nice!
I haven't though about it like this. Good work :cool:
Will it work on full screened command prompts?


Just tried - yep, seems to :)

Hopefully this Matlab app has a unique, consistent, window title (as ConsoleWindowClass on its own could find anything!)

--
Dave.


Dave,

thx a lot for your reply, but it doesn't work yet... :(

The problem is, that my program is compatible to two Matlab versions: 5 and 6. 5 only has a window called "Matlab command window" and the program should be able to find this window, but i couldn't test this because I do not have Matlab 5. Matlab 6 has a child (see the picture). The child has a name "Command window" and the test.nsi is not able to find this window. (it can find the main Matlab though...)

Can you help me out here?

thx, Hendri.


hi dave,

I found out (at least I think), that Matlab is not really a command prompt program, but it only looks like that. This is because above functions work really well for the DOS-window, but cannot add anything to a Matlab window. Please, take a look at the picture i posted earlier, maybe this might give you a clue how to do this...

Thx a lot, Hendri.


Ok, I see what you mean.

Try this, if it doesn't work then maybe there's another window between the main window and the Command Window, but this has no name (that you can see) so you'd have to find out the window class name. To do that I use a tool which comes with Visual Studio, but I'm sure there must be freeware utilities that can do that too...

Section
; Look for Matlab 5 window
FindWindow $R0 "" "Matlab command window"
IntCmp $R0 0 "" FoundWindow FoundWindow
; Look for Matlab 6 window (main window)
FindWindow $R0 "" "MATLAB"
IntCmp $R0 0 NoWindow
; Now look for the command window (child window) inside it
FindWindow $R0 "" "Command Window" $R0
IntCmp $R0 0 NoWindow
FoundWindow:
MessageBox MB_ICONINFORMATION|MB_OK "Window found ($R0)"
Push $R0
Push "dir$\r"
Call SendChars
Return
NoWindow:
MessageBox MB_ICONEXCLAMATION|MB_OK "No window found!"
SectionEnd
Good luck! And Happy Christmas to all... :)

--
Dave.

Dave,

thx a lot for your reply and the code, but it's still not able to find the Matlab command window using Matlab 6. In Matlab 5 it finds the window, but does not add anything behind the command prompt...

So I guess Matlab uses some strange unknown construction for it's windows...

And I guess that I will have to think about another solution on how to do the thing i mentioned above. Or construct erverything in a completely other way...

I also posted this thread at www.mathworks.com (Matlab web-site) but they didn't even give a response....

So thx a lot all (especially Dave!!!), if you know another way of doing this, please let me know, thx, greetz,

Hendri.


You might be interested in downloading AutoIt. It allows you to write small scripts that activate windows, send keystrokes, mouse clicks, all sorts of stuff, and it allows you to compile your scripts into small EXE files.

My favourite utility before I discovered NSIS!

Sure, it's not the most practical solution, but it's better than nothing.

Feel free to PM me if you need help writing a script for it.


Petersa,

thx for your reply, I just downloaded the program and I will certainly try it.

Have a nice christmas time, greetz,
Hendri.


Petersa,

I tried AutoIt, but I couldn't get it to work. Even the example program's (after translating the neccessary command's to Dutch :)) didn't work (eg: adding lines to notepad). I also tried to apply this to Matlab but did also not work unfortunately....

I've given up, I think... :(

Thx all anyway. I will be considering your inputs, maybe, I can get some of it to work later on.

THX, greetz, Hendri.


Well, as I see it all you need to do is to discover Matlab command window class name. After you do that Dave's code will work.

To discover the class name of the command window use Microsoft Spy++ (included with Visual C++) or Spy (included with Microsoft SDK). I am sure there is a freeware that displays this kind of information for all windows somewhere out there but I don't know it. Don't give up, search for it if you don't have Visual C++ or you don't want to download Microsoft SDK.

KiCHiK


kichik, thx for your idea!
I'll start a search for Spy for I do not have C++

If you know some page, please tell me...

Thx a lot, greetz, Hendri.


I have found just the prog for you. It tells you the class name of the window you are pointing with the mouse. It doesn't shows all messages sent to all windows like Spy++ but it should do the trick.

WinSpy

Enjoy! :D


Kichik,

thx for this URL, it indeed is the program I was looking for. So I found out that Matlab 6 has a SunAwtFrame class window with Caption "Matlab", my installer found the window (!!!) but it's not able to send some characters to it... (I use SendChars posted as a reply to my first thread)... Do you know what's missing now :( ???

Thx, greetz, Hendri.


Well I mean, (i hope this was clear...) that nothing happens to the window, no characters appear in the window, nothing is being executed...

thx, Hendri.


Attached is Spy from Microsoft Platform SDK (it's a free download but a long one with all the other stuff).

In the Spy menu click on Select window and select the window needed from Matlab. Then click Start!, type something in Matlab and notice what messages it gets.

You should use the exact same messages in your NSIS script.

If you get too many messages start filtering them with Options->Messages.


"Cannot load HOOK.DLL" is the message I get when clicking 'Start!'

Can you also post this dll (and maybe other files the spy uses???)

Thx a lot, greetz, Hendri.


Oops... :rolleyes:

Well I have reupped it in the same reply (2 up) with hook.dll. I also checked that it doesn't need anything else but hook.dll.

KiCHiK


thx kichik,

i already gave it a try using notepad but i got indeed a lot of messages from one window. I will try to sort this out using the options menu...

Thanks a lot, greetz, Hendri.


Hi Hendri,

How's it going, working yet?

If not, maybe what Matlab's after are messages to fool it into thinking it has keyboard focus...

Function SendChars
Exch $R0 ; Get string
Exch
Exch $R1 ; Get hwnd
Push $R2
Push $R3
SendMessage $R1 ${WM_SETFOCUS} 0 0
StrCpy $R2 0 ; Char index
loop:
StrCpy $R3 $R0 1 $R2 ; Get next char
StrCmp $R3 "" done
Push $R3
Call CharToAscii ; Convert it to ASCII
Pop $R3
SendMessage $R1 ${WM_CHAR} $R3 0
IntOp $R2 $R2 + 1
Goto loop
done:
SendMessage $R1 ${WM_KILLFOCUS} 0 0
Pop $R3
Pop $R2
Pop $R1
Pop $R0
FunctionEnd
btw, see KiCHik's Windows Messages thread for the header file for the !defines used. Very handy!

Other than that, I'm not sure what else to suggest :( Maybe experiment with other windows messages, described at MSDN.

Good luck!
Dave.

Hi Dave,

thx i'll try. I indeed downloaded Kichik's package. I also used some spy to determine what messages to send to Matlab. But I do not understand this output. I press for example 'd' and one time the spy tells me that this is in fact produced by the messages key_down, char and key_up and another time it's produced by char, char and key_up...

So I can't figure out what to send...

But I tried to send 'dir\n' using SendChar and using the first construction (key_down, char, key_up) for each character and Matlab interpreted these messages as:
49[Enter]

So, again really stuck. Is this a strange thing of Matlab, you think, or am I just doing the wrong thing????

Thx in advance, i didn't try above yet, i'll do it right away, thx,

greetz, Hendri.


From MSDN:

The WM_CHAR message is posted to the window with the keyboard focus when a WM_KEYDOWN message is translated by the TranslateMessage function. WM_CHAR contains the character code of the key that was pressed.
Correct me if I am wrong but this means that you don't need to send WM_KEYDOWN and WM_KEYUP. All that it does is to send to the window WM_CHAR so you can skip it and just send WM_CHAR.

And if you already got it to send 49[Enter] keep trying, you are on the right track :up:.

KiCHiK

PS
I am glad you like my header file :)

Kichik,

I think i should use Key_down and up...
I first tried without, but that resulted in just no response from Matlab, so i think i should use it...

Indeed i'm on the right track, i hope, so i'll keep trying, maybe someone has another idea what's going on with Matlab, because in a simple Dos-Window, there is no problem with just sending WM_CHAR...

Thx, greetz, Hendri.


Bye the way, trying to send 'dir\n' resulted in '49[Enter]' so on the right track... I don't know. There seems to be something wrong...

Hendri.


Possible source of problem!
> Matlab 6 has a SunAwtFrame class window with Caption "Matlab"

"SunAwtFrame" means that MatLab written in Java! It means, that it will not (most likely) react to Windows messages.


Smile2Me, have you tried sending the lParam and wParam as shown on Spy?

And does [Enter] means a line break or does it actually show [Enter]?


Kichik,

[Enter] means a line break indeed, but also that the command entered will be executed. In Matlab 49[Enter] means that it will assign the value 49 to the variable 'ans'...

But I think I replicated the messages given by Spy completely, but I'll check on this again, thx...

Happy NewYear to you all!!!

Greetz, Hendri.


I still think you are on the right track.

As you can see by the "table" below the values you sent with WM_KEYUP/DOWN/CHAR were just shifted some to the digits zone. The difference between d and i is the same as 4 and 9. All we are missing now is the r :)


4 5678 9
d efgh i


Did you try to use spy to see what messages are being sent from the installer and then compare them to the messages you sent manually?

KiCHiK

Kichik,

bright point!!!
I'll check the function CharToAscii as it seems that there is something wrong there...

Yeah, i spied the messages from installler to Matlab and everything looked ok to me... (just the messages i send where reported, nothing strange...)

But i also discovered some functions in Matlab to do the job i posted this thread for (in the end, I wanted to protect my M-files (just textfiles with commands) from being viewed or changed). Matlab offers a service for that. So finding some dll's of Matlab give me the opportunity to code my files...

So the goal is achieved!!!

But i'll keep trying to get the messages to work because i think i'll need it later on for other installer features...

So at this point thx all for the helping replies!!!

Kichik, i'll work on it, and i'll post my result here!!!

THX ALL, greetz, Hendri.


Some old topic information:

I gave up, but did the thing in another way, as you might have read...

But I've got a new question on SendMessage.

Please help me at:
http://forums.winamp.com/showthread....threadid=71924

Thx all, greetz, Hendri.