有没有办法将消息从 C#.NET 程序集(ActiveX)发送到 VB6 应用程序?

2024-05-19

本问答参考并可用于以下用途。目的:

  1. 通过ActiveX dll从IE浏览器发送消息到vb6应用程序
  2. 从 ActiveX dll 向 vb6 应用程序发送消息
  3. 从 C#.net (dll) 发送消息到 vb6 应用程序

我读过了本文 http://www.codeproject.com/Articles/6945/VB-NET-VB6-and-C-Interprocess-communication-via-Wi但对于我的目的来说似乎不太清楚......

我也提到过本文创建ActiveX对象 http://www.dreamincode.net/forums/topic/38890-activex-with-c#/并实现了一个 ActiveX dll,我从浏览器调用它以在客户端启动应用程序。该 dll 检查 VB6 exe(进程)是否正在运行,否则运行 exe。如果 exe 已经在运行,那么我想将参数/消息传递给 exe 应用程序。

但我无法获得解决方案或参考示例来实现从 C#.NET 程序集 (ActiveX) 向 VB6 应用程序发送消息的方法...我可以访问 .NET 和 VB6 应用程序的源代码。 ..

我尝试过使用:-

 Process[] processes = Process.GetProcessesByName("MyProgram");
    foreach (Process p in processes)
    {
        IntPtr pFoundWindow = p.MainWindowHandle;
        // how do I pass a value to MyProgram.exe ?
        //

    }

我也尝试过:使用以下链接的帮助,但这并不完全是我正在寻找的解决方案!:-e[C#] 在其他程序上读/写文本框 http://www.neowin.net/forum/topic/607715-c#-readingwriting-textbox-on-other-program/这段代码已在我的 activeX DLL 上实现,它启动了 VB6 应用程序。我通过 .NET(进程)检查 exe 是否正在运行,如果没有,则启动它。如果 exe 正在运行,那么我将使用上面的文章代码示例通过设置控件(mycase 中的文本框)向 vb6 应用程序发送消息...非常有用的代码..以下代码复制自:- c# 读取和写入文本框在其他节目上

public class ExternalWriter 
{
    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string className, string lpszWindow);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
private static extern int SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, string lParam);

private const int WM_SETTEXT = 12;

public void DoExternalWrite(string text) 
{
    IntPtr parent = FindWindow("<window class name>", "<window title">);
    IntPtr child = GetchildHandle(parent, "<class name>");

    SendMessage(child, WM_SETTEXT, IntPtr.Zero, text);
}

private IntPtr GetChildHandle(IntPtr parent, string className) 
{
    /* Here you need to perform some sort of function to obtain the child window handle, perhaps recursively
     */

    IntPtr child = FindWindowEx(parent, IntPtr.Zero, className, null);
    child = FindWnidowEx(parent, child, className, null);

    /* You can use a tool like Spy++ to discover the hierachy on the Remedy 7 form, to find how many levels you need to search
     * to get to the textbox you want */

    return child;
}

}

堆栈溢出还有一个例子,但是它是将数据从另一个应用程序读取到.NET中(对我的要求没有用)。但是,我将其放入这个答案中,以便可能想要读取数据的人应用程序以及写入应用程序将在这里找到解决方案。以下代码来自:-堆栈溢出

public class GetWindowTextExample
{
    // Example usage.
    public static void Main()
    {
        var allText = GetAllTextFromWindowByTitle("Untitled - Notepad");
        Console.WriteLine(allText);
        Console.ReadLine();
    }

    // Delegate we use to call methods when enumerating child windows.
    private delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

    [DllImport("user32")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);

    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    private static extern IntPtr FindWindowByCaption(IntPtr zeroOnly, string lpWindowName);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, [Out] StringBuilder lParam);
// Callback method used to collect a list of child windows we need to capture text from.
private static bool EnumChildWindowsCallback(IntPtr handle, IntPtr pointer)
{
    // Creates a managed GCHandle object from the pointer representing a handle to the list created in GetChildWindows.
    var gcHandle = GCHandle.FromIntPtr(pointer);

    // Casts the handle back back to a List<IntPtr>
    var list = gcHandle.Target as List<IntPtr>;

    if (list == null)
    {
        throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
    }

    // Adds the handle to the list.
    list.Add(handle);

    return true;
}

// Returns an IEnumerable<IntPtr> containing the handles of all child windows of the parent window.
private static IEnumerable<IntPtr> GetChildWindows(IntPtr parent)
{
    // Create list to store child window handles.
    var result = new List<IntPtr>();

    // Allocate list handle to pass to EnumChildWindows.
    var listHandle = GCHandle.Alloc(result);

    try
    {
        // Enumerates though all the child windows of the parent represented by IntPtr parent, executing EnumChildWindowsCallback for each. 
        EnumChildWindows(parent, EnumChildWindowsCallback, GCHandle.ToIntPtr(listHandle));
    }
    finally
    {
        // Free the list handle.
        if (listHandle.IsAllocated)
            listHandle.Free();
    }

    // Return the list of child window handles.
    return result;
}

// Gets text text from a control by it's handle.
private static string GetText(IntPtr handle)
{
    const uint WM_GETTEXTLENGTH = 0x000E;
    const uint WM_GETTEXT = 0x000D;

    // Gets the text length.
    var length = (int)SendMessage(handle, WM_GETTEXTLENGTH, IntPtr.Zero, null);

    // Init the string builder to hold the text.
    var sb = new StringBuilder(length + 1);

    // Writes the text from the handle into the StringBuilder
    SendMessage(handle, WM_GETTEXT, (IntPtr)sb.Capacity, sb);

    // Return the text as a string.
    return sb.ToString();
}

// Wraps everything together. Will accept a window title and return all text in the window that matches that window title.
private static string GetAllTextFromWindowByTitle(string windowTitle)
{
    var sb = new StringBuilder();

    try
    {
        // Find the main window's handle by the title.
        var windowHWnd = FindWindowByCaption(IntPtr.Zero, windowTitle);

        // Loop though the child windows, and execute the EnumChildWindowsCallback method
        var childWindows = GetChildWindows(windowHWnd);

        // For each child handle, run GetText
        foreach (var childWindowText in childWindows.Select(GetText))
        {
            // Append the text to the string builder.
            sb.Append(childWindowText);
        }

        // Return the windows full text.
        return sb.ToString();
    }
    catch (Exception e)
    {
        Console.Write(e.Message);
    }

    return string.Empty;


  }
}

可以使用 WM_COPYDATA 和 SENDMESSAGE 来编程实现此目的的示例代码。

您可以从 .NET 应用程序发送消息。在 VB6 端,您必须创建一个消息挂钩。该钩子将使用 WNDPROC 函数循环并捕获发送给它的消息。

请参阅下文。 2 个示例代码链接:-

对于 C#.NET:C# Windows 应用程序使用 WM_COPYDATA 进行 IPC (CSSendWM_COPYDATA) http://code.msdn.microsoft.com/windowsdesktop/CSSendWMCOPYDATA-97e6644e

对于VB6:如何使用 SendMessage 在应用程序之间传递字符串数据 http://support.microsoft.com/kb/176058/en-us

以上两者都可以以这样的方式组合,您可以在 C#.NET 和 VB6 应用程序之间传递数据...

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有没有办法将消息从 C#.NET 程序集(ActiveX)发送到 VB6 应用程序? 的相关文章

随机推荐

  • 从 Azure 事件中心获取事件后,我是否应该将其放入队列中?

    我目前正在开发一个托管在 Azure 上 使用 Azure 事件中心的应用程序 基本上 我从 Web API 向事件中心发送消息 或者应该说事件 并且我有两个侦听器 用于实时分析的流分析任务 标准辅助角色 根据接收到的事件计算一些内容 然后
  • Bootstrap 5 是否删除了行之间的间距?

    我开始使用 bootstrap 5 并注意到行之间没有空格 我们是否必须明确使用spacing https getbootstrap com docs 5 0 utilities spacing 实用程序喜欢mb 3 or mb 2等等试图
  • java定时器任务调度

    通过阅读 Stack Overflow 我发现很多人不建议使用 Timer Task 嗯 但我已经实现了这个 我有这个代码 detectionHandlerTimer schedule myTimerTask 60 1000 60 1000
  • Airflow:网络服务器未找到新的 DAG

    在 Airflow 中 我应该如何处理错误 此 DAG 在网络服务器 DagBag 对象中不可用 它显示在此列表中 因为调度程序将其在元数据数据库中标记为活动状态 我已将新的 DAG 复制到 Airflow 服务器 并尝试过 取消暂停并刷新
  • 如何在jquery中每4秒添加和删除一个类

    由于某种原因 这并不是每 4 秒在具有 post 类的元素上添加和删除一个新类 jquery 正确加载 就像这样 chrome 显示代码没有错误 document ready function post addClass display d
  • 由于 Chrome 修订,Firebase puppeteer PDF 功能超时

    我有一个 Firebase 函数来创建 PDF 文件 最近 由于 Chrome 修订版 而超时 我既不明白错误消息 也不明白出了什么问题 当我在 MacOS 下将其本地部署时 该功能有效 TimeoutError Timed out aft
  • 如何将 getTime() 转换为 'YYYY-MM-DD HH24:MI:SS' [重复]

    这个问题在这里已经有答案了 我的应用程序中有以下代码 System out println rec getDateTrami getTime 我需要转换以下格式 我想它们是秒 43782000 29382000 29382000 到一个格式
  • sapply - 保留列名称

    我试图总结数据集中许多不同列 变量 的平均值 标准差等 我已经编写了自己的汇总函数 以准确返回我需要和正在使用的内容sapply立即将此函数应用于所有变量 它工作正常 但是返回的数据帧没有列名 我似乎甚至无法使用列号引用重命名它们 也就是说
  • LESS CSS 语法对现代化很有用

    通常我使用现代化 http modernizr com 了解浏览器的功能 同时 我用LESS CSS http lesscss org 使我的CSS更具可读性和可维护性 使用 LESS 嵌套规则的常见样式如下所示 header color
  • XSLT:选择与其他示例不同但略有不同的

    我有以下 XML a b b a
  • JAXB 不会解组接口列表

    看来 JAXB 无法读取它所写的内容 考虑以下代码 interface IFoo void jump XmlRootElement class Bar implements IFoo XmlElement public String y p
  • 确保应用程序独立于用户的屏幕分辨率

    有没有简单的方法可以在任何不同的 PC 上运行在 Visual Studio 2005 上用 C 创建的应用程序 无论其屏幕分辨率如何 屏幕分辨率 NET 2 0 中的 Windows 窗体具有一些处理不同 DPI 的机制 并且具有比 NE
  • 如何删除 MySQL 数据库?

    你可能从我的上一个问题中注意到一个问题引发了更多的问题 在 MySQL 监视器中阅读 MySQL 手册 https stackoverflow com questions 1081399 我的数据库现在无法使用 部分原因是我想破坏东西并且无
  • 声明时初始化和构造函数中初始化之间的区别[重复]

    这个问题在这里已经有答案了 以下两者有什么区别 哪个更优选 public class foo int i 2 public class foo int i foo i 2 在您的示例中 行为语义没有区别 在Java中 所有实例字段初始值设定
  • 如何为 Windows 构建静态 Qt 库并将其与 Qt Creator 一起使用

    我已经下载了以下 Qt 源 http download qt nokia com qt source qt everywhere opensource src 4 7 3 zip http download qt nokia com qt
  • getItem 与 getItemAtPosition

    有两种方法可以获取列表视图中的选定项目 list getAdapter getItem position list getItemAtPosition position 我的问题是 哪一种是首选的做法 我见过人们同时使用这两种方法 您可以使
  • 调整ArrayBuffer的大小

    如果我想创建一个数组缓冲区 我会写 var buff new ArrayBuffer size 但是如何调整现有缓冲区的大小呢 我的意思是 在缓冲区末尾添加更多字节 ArrayBuffer 本身没有设置 有set https develop
  • Laravel - 急切加载 Eloquent 模型的方法(而不是关系)

    就像我们可以急切加载 Eloquent 模型的关系一样 有没有办法急切加载不是 Eloquent 模型的关系方法的方法 例如 我有一个 Eloquent 模型GradeReport它有以下方法 public function totalSc
  • Linux内核container_of宏和C90中的通用容器

    是否有可能实施容器的 http lxr linux no linux tools perf util include linux kernel h L18纯C90中的宏 我不确定如何做到这一点 因为内核实现取决于海湾合作委员会黑客 http
  • 有没有办法将消息从 C#.NET 程序集(ActiveX)发送到 VB6 应用程序?

    本问答参考并可用于以下用途 目的 通过ActiveX dll从IE浏览器发送消息到vb6应用程序 从 ActiveX dll 向 vb6 应用程序发送消息 从 C net dll 发送消息到 vb6 应用程序 我读过了本文 http www