interacting with windows calculator

P

pipo

Hi all,

I am trying to interact with the windows calculator.
I want to open the calculator for the user so the user can work with it.
When the user is done (or during) working with calculator I need the
(displayed)value of the calcultor back in my textbox.

I have no problem starting calculator or even put values in calculator.
I just cant get the value out of the Edit class of the calculator.

Any tips or idea's for me?


Here is my code (so far)

//Opening the calculator
Process p =Process.Start("calc.exe");
//Calculator Handle
hwndCalc = p.MainWindowHandle.ToInt32();
//Get the Handle of the Edit class (textbox of the calculator)
IntPtr pntr = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, "Edit", null);
//Setting a value to the calculator
SendMessage(pntr, WM_SETTEXT, 0, "1234567");

//Buffer voor the text to be read out
StringBuilder Buff = new StringBuilder(256);
//Buffer stays empty...why???
GetWindowText(pntr.ToInt32(), Buff, Buff.Capacity);
//Dont know what to fill in for nIDDlgItem...:(
//Spy++ gives me everytime a different handle. The const 414 isnt correct
either (Buff stays empty)
//When filling in the Edit class handle then also Buff stays empty.
GetDlgItemTextA(hwndCalc, ????, Buff, Buff.Capacity);


Thank you in advance
 
L

Lasse Vågsæther Karlsen

pipo said:
Hi all,

I am trying to interact with the windows calculator.
I want to open the calculator for the user so the user can work with it.
When the user is done (or during) working with calculator I need the
(displayed)value of the calcultor back in my textbox.

I have no problem starting calculator or even put values in calculator.
I just cant get the value out of the Edit class of the calculator.

Any tips or idea's for me?


Here is my code (so far)

//Opening the calculator
Process p =Process.Start("calc.exe");
//Calculator Handle
hwndCalc = p.MainWindowHandle.ToInt32();
//Get the Handle of the Edit class (textbox of the calculator)
IntPtr pntr = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, "Edit", null);
//Setting a value to the calculator
SendMessage(pntr, WM_SETTEXT, 0, "1234567");

//Buffer voor the text to be read out
StringBuilder Buff = new StringBuilder(256);
//Buffer stays empty...why???
GetWindowText(pntr.ToInt32(), Buff, Buff.Capacity);
//Dont know what to fill in for nIDDlgItem...:(
//Spy++ gives me everytime a different handle. The const 414 isnt correct
either (Buff stays empty)
//When filling in the Edit class handle then also Buff stays empty.
GetDlgItemTextA(hwndCalc, ????, Buff, Buff.Capacity);


Thank you in advance

Why do you need to do this? I'm sorry if the question is blunt, but
unless you give us a *really* good reason for why you need to do that,
then I can't help you better.

If all you need is a simple calculator, there are probably plenty of
examples out there for you.
 
P

Pipo

Why I need it?
Because I dont want to use extra code or some other calculator while there
is a good one in windows.
My users know this calculator and are using that one extensively.
So, I(read my users) want us the one there already is and is familair.
Ooohhh and also...because the specifications require it.

So, I am not asking for code or some other calculator tool, I want to use
the windows calculator.
Is this a *really* good reason for you all(??) :s

I hope now you can address the topic I was posting for in the first place.

Thank you for that Lasse.

BTW. I solved this topic already.
Need only help with finding multiple calculatos handles.

When I call FindWindow("SciCalc", null) I always get the same handle back.
How can I enumerate through the (open) calculator forms?
I dont want to use EnumWindows and check for the handles (bit overdone I
think)

More looking for a FindWindowNext or so ;)

Does anyone know a function for this?

You maybe Lasse?
 
F

Family Tree Mike

If you launch the calculator with Process.Start, then
Process.MainWindowHandle should do what you want. If you are hoping to get
the handle to numerous user launched calculators, then it is more difficult.

Lasse's question was resonable, in my opinion. There are many things that
can go on when working with a program with no exposed API.
 
L

Lasse Vågsæther Karlsen

Pipo said:
Why I need it?
Because I dont want to use extra code or some other calculator while
there is a good one in windows.
My users know this calculator and are using that one extensively.
So, I(read my users) want us the one there already is and is familair.
Ooohhh and also...because the specifications require it.

So, I am not asking for code or some other calculator tool, I want to
use the windows calculator.
Is this a *really* good reason for you all(??) :s

I hope now you can address the topic I was posting for in the first place.

Thank you for that Lasse.

BTW. I solved this topic already.
Need only help with finding multiple calculatos handles.

When I call FindWindow("SciCalc", null) I always get the same handle back.
How can I enumerate through the (open) calculator forms?
I dont want to use EnumWindows and check for the handles (bit overdone I
think)

More looking for a FindWindowNext or so ;)

Does anyone know a function for this?

You maybe Lasse?

The reason I was asking was that I was hoping I could jolt you out your
current path. Seems I can't do that.

Unfortunately I can't help you either, but let me just point out that
there are so many things that can go wrong with your current "solution"
that whatever it is that your users want, I can assure you that they do
not want something that will break so easily as this solution will.

What if...
- the user closes the calculator before you've gotten the answer?
- opens up another calculator and expects it to behave the same way (ie.
stay in touch with your software)
- upgrades to Vista and the whole thing breaks because the calculator
isn't built the same way? what if xp sp3 changes the calculator?
- wants to use a different type of calculator that they downloaded off
the internet... I mean, it's an external program right? Surely you can
do that *too*?
- what if the user is in the wrong mode (scientific/normal), does it
behave the same? I assume yes
- what if you're running in vista with low access rights, is this
application cross-talk even allowed without a UAC popping up?
- what if it takes longer to start the calculator on some systems than
you expect it to, are you prepared to handle that?
- what if the user is running your program in citrix presentation mode,
how do you intend to talk to a program that is running on a different
machine altogether?

Now, your request for help on just finding the right window without
using EnumWindows is a good request, and I bet someone has the answer,
but if you get a few more of these "I know this will work if I can just
get past this small hurdle..." things happening down this path, I would
seriously reconsider this way of doing it.

Hope you prove me wrong :)
 
Top