转载:http://www.it1352.com/534235.html

问题:

I am writing a Windows Form application in .Net to list all running instances of a third-party CAD/CAM software (in this case CATIA) and let user to choose one of them to perform couple of automated tasks. For performing automated tasks, I need to get the specific instance of COM objects - compared to Getobject() which gives me a non-specific COM instance. Is there a way to get a specific COM instance using window handle or any other methods?

UPDATE: As Raymond said there is no single solution for all COM objects; however I managed to get CATIA COM objects using following code (Which uses ROT to fill a list with all CATIA COM Instances name):

<DllImport("user32.dll", CharSet:=CharSet.Auto)> Private Shared Sub GetClassName(ByVal hWnd As System.IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer) End Sub
<DllImport("ole32.dll", ExactSpelling:=True, PreserveSig:=False)> Private Shared Function GetRunningObjectTable(ByVal reserved As Int32) As IRunningObjectTable End Function
<DllImport("ole32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True, PreserveSig:=False)> Private Shared Function CreateItemMoniker(ByVal lpszDelim As String, ByVal lpszItem As String) As IMoniker End Function
<DllImport("ole32.dll", ExactSpelling:=True, PreserveSig:=False)> Private Shared Function CreateBindCtx(ByVal reserved As Integer) As IBindCtx End Function Try Dim ROTObject As Object = Nothing
Dim runningObjectTable As IRunningObjectTable
Dim monikerEnumerator As IEnumMoniker = Nothing
Dim monikers(1) As IMoniker runningObjectTable = GetRunningObjectTable(0)
runningObjectTable.EnumRunning(monikerEnumerator)
monikerEnumerator.Reset() Dim numFetched As IntPtr = New IntPtr()
While (monikerEnumerator.Next(1, monikers, numFetched) = 0)
Dim ctx As IBindCtx
ctx = CreateBindCtx(0) Dim runningObjectName As String = ""
monikers(0).GetDisplayName(ctx, Nothing, runningObjectName) runningObjectName = runningObjectName.ToUpper
If (Not runningObjectName.Equals("")) Then
Dim runningObjectIns As Object = Nothing
runningObjectTable.GetObject(monikers(0), runningObjectIns) 'Check if object is a Catia object
Try
Dim catiaIns As INFITF.Application = Nothing
catiaIns = DirectCast(runningObjectIns, INFITF.Application)
ListCATIA.Items.Add(catiaIns.Windows.Count)
Catch Exc As Exception
MessageBox.Show(Exc.ToString())
End Try
End If
End While Catch Exc As Exception
Throw Exc
End Try

However, all CATIA instances refer to first CATIA application loaded. No idea why, anybody?

解决方案

The "problem" in your code is that calling GetObject always returns the first active server that it finds in the Running Object Table (ROT). Enumerating the ROT doesn't change that behavior and is a little frustrating because it does show that there is more than one server in the ROT. Note that some of the items returned in the enumeration may not actually be running: GetObject returns the first active server -- not necessarily the first one returned by the enumeration.

However, in the case of CATIA in particular it is possible to get a specific instance. I suspect it is possible with many applications if you can get the particular instance of interest to run some code, on demand, before you actually get a pointer to the COM instance.

For CATIA, this is a rough outline of the process I use:

1. Make a dll with two functions:
HRESULT __stdcall CoMarshalToFile(IUnknown* punk, const char* const filePath)
/* uses `::CreateStreamOnHGlobal`, `::CoMarshalInterface`, `::CoGetMarshalSizeMax`,
and `::GetHGlobalFromStream` to marshal the IUnknown to the specified file.
*/
HRESULT __stdcall CoMarshalFromFile(IUnknown** ppunk, const char* const filePath)
/* uses `::CreateStreamOnHGlobal` and `::CoUnmarshalInterface` to marshal
from the file to an IUnknown pointer.
*/ 2. In CATIA:
Note: this only needs to be done on the development computer.
Make a new "VBA projects" macro library.
Add "declare" statements for:
"LoadLibrary" (Windows API)
"CoMarshalToFile" (DLL specified above)
Add a function
Public Function MarshalCatiaToFile _
(marshalInstanceFilePath As String, _
marshalDllFolder As String) As Long MarshalCatiaToFile calls "LoadLibrary" to load the C++ DLL
and then calls CoMarshalToFile (in DLL) to marshal the CATIA instance
to a file. Remove the macro library from CATIA's list of macro libraries. 3. Create a file:
"C:\Temp\CatiaOnTheFlyCatScripts\OnTheFlyCatScript.catvbs"
The file can be empty. 4. In CATIA:
Note: this must be done for *each* user of CATIA on *each* computer used.
It may be possible to make this available to all users without individual
setup required: it is saved in "FrameUserAliases.CATSettings"
It may also be possible to reverse engineer the settings file and set up
the needed data from outside CATIA. Add "C:\Temp\CatiaOnTheFlyCatScripts\" as a new "Directories" macro library.
Make the added library "current"
Use "Tools --> Customize --> Commands --> Macros" to assign a
"User Alias:" to the "OnTheFlyCatScript.catvbs" script file.
Name the alias "ExecuteOnTheFlyCatScript".
Remove the macro library from CATIA's list of macro libraries.
Close CATIA at this point to force the changes to be saved. 5. VB.net / C# program:
Add the DLL (from step 1) and the CatVBA macro library (from step 2) as
"Embedded Resource" to the project. During program execution:
Extract the DLL and macro library to an appropriate location.
Load the DLL into session using "LoadLibrary".
Create the file:
"C:\Temp\CatiaOnTheFlyCatScripts\OnTheFlyCatScript.catvbs" The "OnTheFlyCatScript.catvbs" will be executed in CATIA. It
uses CATIA.SystemService.ExecuteScript to execute the
"MarshalCatiaToFile" function in the CatVBA macro library.
Add method of choice to this file to indicate success/failure.
I use a dialog box with the appropriate title. To execute the "OnTheFlyCatScript.catvbs":
Using the Windows API functions, get the window handle for the
"Power Input" box at the bottom right of the "desired"
CATIA window.
Using the Windows API functions (*NOT* "SendKeys") send
"c:ExecuteOnTheFlyCatScript" + {Enter} to the "Power Input".
Wait for the "completion" signal from the script. If you used
a dialog box, use the Windows API function to close it. Assuming the script succeeded in marshaling the CATIA instance to
a file, call the DLL function CoMarshalFromFile to get the CATIA
instance.

It's a lot of work with many "moving" parts but it does allow you to automate multiple CATIA sessions "simultaneously". Works well for my purposes: automated extraction of data from a set of CATIA models and automated creation of a set of CATIA models using more than one CATIA session at a time. The bottleneck for my application is the individual CATIA session -- not CPU resources (using a dual processor 4 or 6 core per processor machine); adding more sessions improves throughput.

[转载]在VB.Net中获取COM对象的特定实例(Getting a specific instance of COM object in VB.Net)的更多相关文章

  1. 在SpringMVC中获取request对象

    1.注解法 @Autowired private  HttpServletRequest request; 2. 在web.xml中配置一个监听 <listener> <listen ...

  2. 在SpringMVC中获取request对象的几种方式

    1.最简单的方式(注解法) @Autowired private HttpServletRequest request; 2.最麻烦的方法 a. 在web.xml中配置一个监听 <listene ...

  3. js中获取事件对象的方法小结

    原文地址:http://jingyan.baidu.com/article/d8072ac4594d6cec95cefdac.html 事件对象 的获取很简单,很久前我们就知道IE中事件对象是作为全局 ...

  4. 如何在SpringMVC中获取request对象

    1.注解法 @Autowired private HttpServletRequest request; <listener> <listener-class> org.spr ...

  5. 9.Struts2在Action中获取request-session-application对象

    为避免与Servlet API耦合在一起,方便Action类做单元测试. Struts2对HttpServletRequest.HttpSession.ServletContext进行了封装,构造了三 ...

  6. 在spring中获取代理对象代理的目标对象工具类

    昨天晚上一哥们需要获取代理对象的目标对象,查找了文档发现没有相应的工具类,因此自己写了一个分享给大家.能获取JDK动态代理/CGLIB代理对象代理的目标对象. 问题描述:: 我现在遇到个棘手的问题,要 ...

  7. 如何在spring中获取request对象

    1.通过注解获取(很简单,推荐): public class Hello {@Autowired  HttpServletRequest request; //这里可以获取到request} 2.在w ...

  8. 3.jquery在js文件中获取选择器对象

    一.常用的选择器有一下几种: 1.标签选择器 2.类选择器 3.id选择器 4.并集选择器 5.层级选择器 二.如何获取选择器对象: <!DOCTYPE html> <html la ...

  9. Lucene.Net 3.0.3如何从TokenStream中获取token对象

    Lucene.Net最高版本为3.0.3,并且apache已经不再提供Lucene.Net的更新,没仔细研究过Lucene.Net的所有版本,Lucene.Net3.0.3遍历TokenStream获 ...

随机推荐

  1. I2C(smbus pmbus)和SPI分析

    2C和SPI作为两种非常常用的低速外部总线 I2C I2C是以前的飞利浦半导体制定的标准,也就是如今的NXP. I2C总线由一条数据线(SDA)和一条时钟线(SCL)组成.设备分主从,主设备提供时钟, ...

  2. POJ-2112 Optimal Milking(floyd+最大流+二分)

    题目大意: 有k个挤奶器,在牧场里有c头奶牛,每个挤奶器可以满足m个奶牛,奶牛和挤奶器都可以看成是实体,现在给出两个实体之间的距离,如果没有路径相连,则为0,现在问你在所有方案里面,这c头奶牛需要走的 ...

  3. 关于Django在写小项目的一些小注意事项

    个人常踩的坑的小问题: . 在筛选元素的时候,及时queryset里面只有一个元素,取值还是要用方法取出来 例:#当狗指定pd时候已经唯一,还是要加fir()方法,本人经常忘记了 models.Boo ...

  4. 免费的mysql数据库

    https://blog.csdn.net/kernel_/article/details/53320498

  5. 数据库迁移expdp impdp 与 OGg 搭建

    1.long 字段的无法使用OGG 同步 2.clob字段的导入导出Bug , 生产使用network-link 导入导出太慢了,本地导入导出速度会快3到4倍 .但是测试环境的情况却相反 测试环境和生 ...

  6. vue环境搭建(一)

    1.Vue依赖node npm命令执行,需要下载node 下载地址 2.安装全局vue-cli脚手架(搭建环境所需要模板),  window+ r 打开命令工具,输入cmd  ,这时显示命令行工具,输 ...

  7. 加解密---Java安全

    一.概述 1.JCA(Java Cryptography Architecture) 提供基本的加密框架(消息摘要.数字签名......) 2.JCE(Java Cryptography Extens ...

  8. JobService 7.0 定时任务不生效

    代码 // 构建JobInfo对象,传递给JobSchedulerService JobInfo.Builder builder = new JobInfo.Builder(JOB_ID,new Co ...

  9. 在SQL Server中创建用户角色及授权(使用SQL语句)

    1. 首先在 SQL Server 服务器级别,创建登陆帐户(create login) --创建登陆帐户(create login) create login dba with password=' ...

  10. Linux下判断磁盘是SSD还是HDD的几种方法

    环境介绍 Fedora release 25 (Twenty Five) 判断方法 方法一 判断cat /sys/block/*/queue/rotational的返回值(其中*为你的硬盘设备名称,例 ...