使用系统Windows自动化发送WM_COPYDATA消息

时间:2019-01-08 17:06:36

标签: c# vb.net winapi automation

我正在使用以下代码将消息/命令发送到另一个窗口/进程。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace AvroSwitch
{
    public class clsSwitch
    {
        [DllImport("User32.dll")]
        private static extern int RegisterWindowMessage(string lpString);

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public static extern Int32 FindWindow(String lpClassName, String lpWindowName);

        //For use with WM_COPYDATA and COPYDATASTRUCT
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);

        public const int WM_COPYDATA = 0x4A;

        //Used for WM_COPYDATA for string messages
        public struct COPYDATASTRUCT
        {
            public IntPtr dwData;
            public int cbData;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpData;
        }

        private static int sendWindowsStringMessage(int hWnd, int wParam, string msg)
        {
            int result = -1;

            if (hWnd > 0)
            {
                byte[] sarr = System.Text.Encoding.Unicode.GetBytes(msg);
                sarr = System.Text.Encoding.Convert(ASCIIEncoding.ASCII, UnicodeEncoding.Unicode, sarr);
                int len = sarr.Length;
                COPYDATASTRUCT cds;
                cds.dwData = (IntPtr)0;
                cds.lpData = System.Text.Encoding.Unicode.GetString(sarr);
                cds.cbData = len + 1;
                result = SendMessage(hWnd, WM_COPYDATA, wParam, ref cds);
            }

            return result;
        }

        private static int getWindowId(string className, string windowName)
        {
            return FindWindow(className, windowName);
        }

        public static int toBangla()
        {
            return sendWindowsStringMessage(getWindowId("TAvroMainForm1", null), 0, "bn");
        }

        public static int toEnglish()
        {
            return sendWindowsStringMessage(getWindowId("TAvroMainForm1", null), 0, "sys");
        }
    }
}

我正在使用2函数toBangle()和toEnglish(),但是它们都不起作用。

,但以上代码不起作用。我认为可以使用系统窗口自动机轻松实现。

更新1:

这是来自delphi的第三方应用程序的代码,用于将确切的命令发送到主应用程序/窗口。

Function SendCommand(cmd: String): boolean;
Var
     copyDataStruct           : TCopyDataStruct;
     receiverHandle           : THandle;
Begin
     result := false;
     copyDataStruct.dwData := 0;        //0=string
     copyDataStruct.cbData := 1 + Length(cmd);
     copyDataStruct.lpData := PChar(cmd);

     receiverHandle := FindWindow(PChar('TAvroMainForm1'), Nil);
     If receiverHandle = 0 Then
          Exit;


     SendMessage(receiverHandle, WM_COPYDATA, 0, Integer(@copyDataStruct));
     Result := True;
End;

更新2:

这是处理wm命令的代码形式的主窗口/应用程序:

Procedure TAvroMainForm1.WMCopyData(Var Msg: TWMCopyData);
Var
         cmd: String;
Begin
         cmd := PChar(Msg.CopyDataStruct.lpData);
         cmd := Lowercase(cmd);

         If cmd = 'bn' Then Begin
                    KeyLayout.BanglaMode;
                    // Send something back
                    Msg.result := 21;
         End;

         If cmd = 'sys' Then Begin
                    KeyLayout.SysMode;

                    // Send something back
                    Msg.result := 21;
         End;
End;

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试以下方法传递ANSI文本

//Script to email selected location about submissions on the Classroom Collection Form using trigger onFormSubmit
var sheet = SpreadsheetApp.getActive();

function initialize() {
  var triggers = ScriptApp.getProjectTriggers();
  for(var i in triggers) {
    ScriptApp.deleteTrigger(triggers[i]);
  }
ScriptApp.newTrigger("email")
  .forSpreadsheet(sheet)
  .onFormSubmit()
  .create();
}

function email() {
  var row = sheet.getActiveRange().getRowIndex();
  var recipient;
  var branchChoice = sheet.getRange("K"+row).getValue();
  if (branchChoice == "Fairfield Inside"||"Fairfield Drive Thru"){recipient = "user1@lane.org, user2@lane.org, user7@lane.org"}
  else if (branchChoice == "Hamilton Inside"||"Hamilton Drive Thru"){recipient = "user3@lane.org, user4@lane.org, user7@lane.org"}
  else if (branchChoice == "Oxford Inside"||"Oxford Drive Thru"){recipient = "user5@lane.org, user7@lane.org"}
  else if (branchChoice == "Bookmobile Drop Off"){recipient = "user6@lane.org, user7@lane.org"}
  var subject = "New Classroom Collection Request";
  var message = "<HTML><BODY>"
                  + "<P><B>New Request Information Recieved - </B>"+ sheet.getRange("A"+row).getValue()
                  + "<P><B>Teacher Name: </B>" + sheet.getRange ("B"+row).getValue()
                  + "<P><B>School: </B>" + sheet.getRange ("C"+row).getValue()
                  + "<P><B>Teacher's Email: </B>" + sheet.getRange ("D"+row).getValue()
                  + "<P><B>Phone: </B>" + sheet.getRange ("E"+row).getValue()
                  + "<P><B>Teacher Barcode: </B>" + sheet.getRange ("F"+row).getValue()
                  + "<P><B>Grade: </B>" + sheet.getRange ("G"+row).getValue()
                  + "<P><B>Room Number: </B>" + sheet.getRange ("H"+row).getValue()
                  + "<P><B>Pick-Up Month: </B>" + sheet.getRange ("I"+row).getValue()
                  + "<P><B>Pick-Up Day: </B>" + sheet.getRange ("J"+row).getValue()
                  + "<P><B>Branch Pick-Up Location: </B>" + sheet.getRange ("K"+row).getValue()
                  + "<P><B>Subjects: </B>" + sheet.getRange ("L"+row).getValue()
                  + "<P><B>Collection Should Include: </B>" + sheet.getRange ("M"+row).getValue()
                  + "<P><B>Specific Titles Requested: </B>" + sheet.getRange ("N"+row).getValue()
                  + "<P><B>Reading Levels: </B>" + sheet.getRange ("O"+row).getValue()
                  + "<P><B>Number of Items Needed: </B>" + sheet.getRange ("P"+row).getValue()
                  + "</HTML></BODY>";

                  MailApp.sendEmail(recipient, subject, "", {htmlBody: message});
}

这应该可行,因为string msg = "bn"; //or "sys" COPYDATASTRUCT cds; cds.dwData = (IntPtr)0; cds.lpData = msg; cds.cbData = Encoding.Default.GetBytes(msg).Length; SendMessage(hwnd, WM_COPYDATA, wParam, ref cds); 已被封送

lpData