在C#在Windows API编写COM服务器,从哪里开始? [英] Writing COM servers for the windows API in C#, where to begin?

查看:148
本文介绍了在C#在Windows API编写COM服务器,从哪里开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一些插件用的终端服务会话Broker的插件接口。我非常流利的C#,但自上世纪90年代我没有做过任何C ++。对于我写的插件,我计划与数据库通信,我会preFER使用 System.Data.SqlClient的谈它,因为我知道它的插件和奏得相当好。我有 Windows SDK的已提供我与的.idl文件的界面( tssbx.idl )。该SDK还提供了一个C头文件( tssbx.h )和C源文件( tssbx_i.c )。

我从来没有写过COM服务器,我一直有很多很难找到学习如何阅读IDL文件,并把它在C#中的资源。一切我觉得说使用TlbImport 但是这需要搞什么块是在IDL其中 tssbx.idl 不(或其家属)执行。

什么是我最好的选择:

  1. 找到一个工具,C#也就是相当于的 MIDL 在一个cs文件解析.idl文件。
  2. 在学习IDL(我一直无法找到很好的指导学习它),并用手C#编写了整个事情。
  3. 使用所提供的C文件撰写助手DLL,并让该呼叫在我的C#DLL我的.NET部分。
  4. 重新学习C ++,使用所提供的.h和.c文件,并使用CLR,使我的.NET电话。
  5. 在我还没有想到其他的一些选项。
解决方案

做你想要做的是对IDL定义转换为C#接口,然后实现这些接口配置为C#类的方式。您应用适当的属​​性(主要是标记有ComVisible特性, <一href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.classinterfaceattribute.aspx"相对=nofollow> ClassInterface 和的ProgId )到要暴露给COM,并使用 regasm 工具程序集注册为COM服务器类。

IDL翻译成C#,其实并不复杂;在大多数情况下它直接映射pretty的从IDL关键字C#关键字和/或的MarshalAs 属性。我有一系列的关于如何做COM互操作瓦特/博客文章了 TLBIMP ,其中包括上的如何阅读IDL 。我不知道任何工具,特别是做了一个很好的工作,但如果Windows SDK的一部分,它应经常检查pinvoke.net第一,以防别人这样做是为了你。

至于你的其他选择,3和4这两个数额同样的事情。您可以直接调用从非托管code管理code,除非它通过COM互操作或混合模式C ++库完成。在第一种情况下,你仍然必须解决所有获得注册的COM您的C DLL调用你的C#程序集的问题,所以你不妨跳过中间人。对于第二个,你基本上是在做手工了运行时的互操作code为你做同样的事情,并使用你少带引导熟悉的语言,这似乎是一个净损失给我。

请注意,虽然,加载.NET程序集到一个非托管的环境并不总是可能的;例如,管理外壳扩展明确不能在Windows 2008支持,我不知道,如果TSSBX界面将允许您加载托管程序集作为COM对象或没有,所以你必须要知道这种可能性。如果不能,那么所有你的选择是去工作,你就必须避免使用.NET Framework在所有和使用其他数据库访问技术,并写在非托管C ++整个项目。

I am trying to write some plugins to work with the Terminal Services Session Broker Plugin Interface. I am very fluent in C# but I have not done any C++ since the late 90's. For the plugin I am writing I plan on communicating with a database and I would prefer to use System.Data.SqlClient to talk to it as I know it's ins and outs fairly well. I have the Windows SDK which has provided my with the .idl file for the interface (tssbx.idl). The SDK also provides a C header file (tssbx.h) and a C source file (tssbx_i.c).

I have never written a COM server before, and I have been having a lot of trouble finding resources on learning how to read a IDL file and turn it in to C#. Everything I find says "Use TlbImport" but that requires things like the block library to be in the IDL which tssbx.idl does not (nor its dependents) implement.

What would be my best option:

  1. Find a tool for C# that is the equivalent to MIDL for parsing a .idl file in to a .cs file.
  2. Learn IDL (I have been having trouble finding good guides to learn it) and write the whole thing in C# by hand.
  3. Write a helper dll using the C files provided and have that call in to my C# dll for my .NET parts.
  4. Re-learn C++, use the provided .h and .c files, and use the CLR to make my .NET calls.
  5. Some other option I have not thought of.

解决方案

The way to do what you're trying to do is to translate the IDL definitions into C# interfaces, then implement those interfaces as C# classes. You apply the appropriate attributes (mostly ComVisible, ClassInterface, and ProgId) to the classes you want to expose to COM, and use the regasm tool to register your assembly as a COM server.

Translating IDL into C# is actually not that complex; for the most part it maps pretty directly from IDL keywords to C# keywords and/or MarshalAs attributes. I have a series of blog posts on how to do COM interop w/out tlbimp, including one on how to read IDL. I don't know of any tools, specifically, that do a good job of this, but if its part of the Windows SDK you should always check pinvoke.net first in case someone else did it for you.

As far as your other options, 3 and 4 both amount to about the same thing. You cannot call managed code directly from unmanaged code unless it's done via COM Interop or a mixed-mode C++ library. In the first case, you'd still have to solve all of the problems of getting your C# assembly registered with COM for your C dll to call, so you may as well skip the middle-man. For the second, you are basically doing manually the same things that the runtime's interop code does for you, and using a language you're less familiar with to boot, which seems like a net loss to me.

Be aware, though, that loading .NET assemblies into an unmanaged context isn't always possible; for example, managed shell extensions are explicitly not support in Windows 2008. I don't know if the TSSBX interface will allow you to load managed assemblies as COM objects or not, so you'll have to be aware of that possibility. If you can't, then none of your options are going to work, and you'll have to avoid using the .NET Framework at all and use some other database access technology and write the entire project in unmanaged C++.

这篇关于在C#在Windows API编写COM服务器,从哪里开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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