Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Sending messages to Google Chrome from C# application

I've been searching around, and I haven't found how I would do this from C#.

I was wanting to make it so I could tell Google Chrome to go Forward, Back, Open New Tab, Close Tab, Open New Window, and Close Window from my C# application.

I did something similar with WinAmp using

[DllImport("user32", EntryPoint = "SendMessageA")]
private static extern int SendMessage(int Hwnd, int wMsg, int wParam, int lParam);

and a a few others. But I don't know what message to send or how to find what window to pass it to, or anything.

So could someone show me how I would send those 6 commands to Chrome from C#? thanks

EDIT: Ok, I'm getting voted down, so maybe I wasn't clear enough, or people are assuming I didn't try to figure this out on my own.

First off, I'm not very good with the whole DllImport stuff. I'm still learning how it all works.

I found how to do the same idea in winamp a few years ago, and I was looking at my code. I made it so I could skip a song, go back, play, pause, and stop winamp from my C# code. I started by importing:

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr FindWindow([MarshalAs(UnmanagedType.LPTStr)] string lpClassName, [MarshalAs(UnmanagedType.LPTStr)] string lpWindowName);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern int SendMessageA(IntPtr hwnd, int wMsg, int wParam, uint lParam);
    [DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    public static extern int GetWindowText(IntPtr hwnd, string lpString, int cch);
    [DllImport("user32", EntryPoint = "FindWindowExA")]
    private static extern int FindWindowEx(int hWnd1, int hWnd2, string lpsz1, string lpsz2);
    [DllImport("user32", EntryPoint = "SendMessageA")]
    private static extern int SendMessage(int Hwnd, int wMsg, int wParam, int lParam);

Then the code I found to use this used these constants for the messages I send.

    const int WM_COMMAND = 0x111;
    const int WA_NOTHING = 0;
    const int WA_PREVTRACK = 40044;
    const int WA_PLAY = 40045;
    const int WA_PAUSE = 40046;
    const int WA_STOP = 40047;
    const int WA_NEXTTRACK = 40048;
    const int WA_VOLUMEUP = 40058;
    const int WA_VOLUMEDOWN = 40059;
    const int WINAMP_FFWD5S = 40060;
    const int WINAMP_REW5S = 40061;

I would get the hwnd (the program to send the message to) by:

IntPtr hwnd = FindWindow(m_windowName, null);

then I would send a message to that program:

SendMessageA(hwnd, WM_COMMAND, WA_STOP, WA_NOTHING);

I assume that I would do something very similar to this for Google Chrome. but I don't know what some of those values should be, and I googled around trying to find the answer, but I couldn't, which is why I asked here. So my question is how do I get the values for:

m_windowName and WM_COMMAND

and then, the values for the different commands, forward, back, new tab, close tab, new window, close window?

like image 789
Joel Avatar asked Sep 29 '08 08:09

Joel


1 Answers

Start your research at http://dev.chromium.org/developers


EDIT: Sending a message to a window is only half of the work. The window has to respond to that message and act accordingly. If that window doesn't know about a message or doesn't care at all you have no chance to control it by sending window messages.

You're looking at an implementation detail on how you remote controlled Winamp. Sending messages is just one way to do it and it's the way the Winamp developers chose. Those messages you're using are user defined messages that have a specific meaning only to Winamp.

What you have to do in the first step is to find out if Chromium supports some kind of remote controlling and what those mechanisms are.

like image 117
VVS Avatar answered Sep 29 '22 20:09

VVS