如何在C#中使用OLE自动化与另一个程序进行交互? [英] How to interact with another programs with OLE Automation in C#?

查看:460
本文介绍了如何在C#中使用OLE自动化与另一个程序进行交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个使用C#的应用程序,该应用程序可以从SQL Server读取商品数据并将其推送到名为"SLP-V Ishida Retail Scales"的秤系统.它们具有"SLP-V自动化接口"接口,该接口允许用户程序与其系统进行交互.这是SLP-V帮助页面中的注释:

I create an application with C# that can read item data from SQL Server and push it to the scale system named "SLP-V Ishida Retail Scales". They have an interface "SLP-V Automation Interface" that allows user programs to interact with their systems. This is the note from help page in SLP-V :

自动化接口(也称为"COM(公共对象模型)接口")为用户程序提供了一种访问SLP-V功能的方法.最常见的应用程序是使用VB脚本自动执行SLP-V操作,例如导入主机文件.但是,可以在支持自动化(COM)的任何编程环境中使用自动化接口,这是将SLP-V功能集成到最终用户应用程序中的首选方法.

The automation interface (also known as the "COM (common object model) interface") provides a method for user programs to access SLP-V functions. The most common application for this is the use of VB Script to automate SLP-V operations such as importing host files. However, the automation interface can be used from any programming environment that supports automation (COM), and this is the preferred method for incorporating SLP-V functions into end-user applications.

本主题提供有关SLP-V自动化对象的方法和属性的参考,并包括一些示例程序.

This topic provides a reference for the methods and properties of the SLP-V automation object and includes some sample programs.

SLP-V自动化对象

SLP-V自动化对象的名称为"Ishida.Slp.Scripting.CommonApi",类型库文件为"SlpScripting.tlb".

The SLP-V automation object name is "Ishida.Slp.Scripting.CommonApi" and the type library file is "SlpScripting.tlb".

我的问题是,C#语言是否允许我们使用OLE自动化与其他程序进行交互?如果答案是肯定的,我该如何与我的程序进行交互? 我的意思是喜欢调用他们的方法.因为我不能添加SlpScripting.tlb作为参考.它说

My question is, does the C# language allow us to interact other programs using OLE Automation? And if the answer is yes, how do I interact with my program? I mean like calling their method. Because I can't add SlpScripting.tlb as a reference. It says

无法添加对"SLP脚本接口"的引用 ActiveX类型库'SlpScripting.tlb'是从.NET程序集导出的,不能添加为引用.添加对.NET程序集的引用

A reference to 'SLP Scripting Interface ' could not be added The ActiveX type library 'SlpScripting.tlb' was exported from a .NET assembly and cannot be added as reference. Add a reference to the .NET assembly instead

我已经在Google上搜索了此内容,但没有找到答案.

And I have searched Google about this, but I didn't find the answer.

最终找到了解决方案 我不必在c#中添加引用,而不仅仅是使用:

Finally found a solution I don't have to add reference in c#, instead of just using :

System.Type objType = System.Type.GetTypeFromProgID("The name of progID that you want to call"); 
dynamic comObject = System.Activator.CreateInstance(objType);

  • 示例ProgID ="Ishida.Slp.Scripting.CommonApi".
  • 然后仅调用该对象中存在的函数/方法,例如:

    And then just call the function / method that exist in that object, for example :

    comObject.LoginToHost("localhost", 8085, username, pass);
    


    推荐答案

    OLE自动化是我们现在称为COM的旧措辞.是的,.NET可以非常轻松地访问COM(从.NET 1.0开始).

    OLE automation is old wording for what we now call COM. And yes, .NET can access COM very easily (starting with .NET 1.0).

    您有以下选择:

    方法1

    首先在开发系统上注册" COM库.查看SLP系统的文档,可能这是在安装过程中已经完成的.如果没有,通常可以使用regsvr32 XXX.DLL手动注册COM DLL.请注意32/64位问题(如果要在64位Windows中注册32位COM DLL,请使用C:\Windows\SysWOW64\regsvr32.exe.)

    First "register" the COM library on your development system. Look in the documentation of the SLP system, probably this was done already during setup. If not, normally a COM DLL can be registered manually with regsvr32 XXX.DLL. Be aware of 32/64 Bit issues (if you want to register a 32 bit COM DLL in 64 bit Windows, use C:\Windows\SysWOW64\regsvr32.exe).

    然后,如果您要访问,则应该在Visual Studio中列出您的COM DLL

    Then your COM DLL should be listed in Visual Studio if you go to

    添加参考==> COM

    Add Reference ==> COM

    为"SlpScripting类型库1.0"或类似版本.

    as "SlpScripting Type Library 1.0" or similar.

    然后,添加一个使用SLPxxxx"或类似名称(Intellisense应该显示真实姓名).

    Then, add a "using SLPxxxx" or similar (Intellisense should show the real name).

    此后,您应该能够创建COM对象的新实例.

    After this, you should be able to create a new instance of your COM object.

    要获取更多帮助,请搜索"C#COM interop",您会发现很多信息.

    For additional help, search for "C# COM interop", you will find lots of informations.

    方法2

    打开Visual Studio命令提示符,然后输入:

    Open a Visual Studio command prompt, and enter:

    tlbimp SlpScripting.tlb 
    

    将创建一个DLL,您可以将其添加为参考.

    A DLL will be created, which you can add as a reference.

    这篇关于如何在C#中使用OLE自动化与另一个程序进行交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆