Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

communicating with other apps. in VC++

In VBA there is a way to communicate with other
applications via the SendKeys command, is it possible to
implement a similar function in Visual C++?
How about "Shell" and "AppActivate"?
  • 3
  • 2
1 Solution
C can do anything VB or VBA can do.

I don't know the SendKeys command, but I guess it simply send some WM_KEYDOWN/WM_KEYUP messages.  In c, you can call SendMessage(), which can not only send key messages but also a lot of others.

You can also use OLE, DDE, socket etc to communicate with other processes.
quillerAuthor Commented:
I should be a little more specific.  Say, I want to communicate
with an App. that has no OLE,DDE, or any other support that
I know of.  Under Unix, I used "expect".  Under VBA, there is
the "SendKeys" command.  I want to be able to send keys to
another app.  I'm basically trying to set up a batch procedure
which at one step involves calling an App. that has no msg.
support.  So, for example, I can do the following in VBA:

Sub Send_Msg()
 Dim i, Msg, x                             ' Declare variables.
 x = Shell("C:\WINNT\system32\calc.exe", 1)  ' open Calculator.
 For i = 1 To 10                        ' Set up counting loop.
  SendKeys i, True         ' Send keystrokes to the calculator.
  SendKeys "{+}", True
 Next i                               ' to add each value of I.
 AppActivate "Microsoft Access"          ' Return focus to VBA.
 Msg = "Choose OK to close Calculator."  ' Stop to see results.
 MsgBox Msg
 AppActivate "calculator"         ' Return focus to Calculator.
 SendKeys "%{F4}", True      ' Send Alt+F4 to close Calculator.
 AppActivate "Microsoft Access" ' Return focus to VBA
End Sub

So, how can I do the equivalent from Visual C++?

Use the system() command to execute programs from within vc++. Like this: system("executable.exe")
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

quillerAuthor Commented:
The system command I am aware of.  But how do I target a
specific window, as in the above example (I can target the
window by its name, "calculator"), and how do I send key strokes
to it.

First, use FindWindow() to get the window handle of calc.  Then use SendMessage() to send key strokes.

hwnd=FindWindow(NULL, "Calculator");
SendMessage(hwnd, WM_CHAR, wparam, lparam);

Refer to SDK help for wparam and lparam.
quillerAuthor Commented:


Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now