简体   繁体   English

需要将基于C ++ dll / h / lib / xml / exe的SDK“包装”到COM中以在C#2.0项目中使用

[英]Need to 'wrap up' a C++ dll/h/lib/xml/exe based SDK into a COM to use in a C# 2.0 project

I just got handed an SDK made of C++ dll, lib, exe, and various .h files. 我刚刚得到了一个由C ++ dll,lib,exe和各种.h文件组成的SDK。 I presume it is meant for C++ developers. 我想它是为C ++开发人员准备的。 The sample projects they provide with their documentation are all written in C++. 他们提供的文档样本项目都是用C ++编写的。 I am able to spin them up with Visual Studio 8 (2005) and run them. 我可以使用Visual Studio 8(2005)启动它们并运行它们。 They do control the device as advertised. 他们确实按照广告宣传控制设备。 However the project this needs to be used by is in C# .Net 2.0 and that is unchangeable. 但是,需要使用的项目在C#.Net 2.0中,并且不可更改。

My boss was a C++ developer and says all I need to do is to compile it as a COM object and then import the COM object into my C# solution. 我的老板是一位C ++开发人员,他说我要做的就是将其编译为COM对象,然后将COM对象导入到我的C#解决方案中。 My boss says it should take less than an hour to "wrap" there SDK up as a COM object, even for me with no knowledge of C++ compiling. 我的老板说,即使对于不了解C ++编译的我来说,将SDK作为COM对象“包装”起来也需要不到一个小时的时间。

I've used COM objects in C# solutions before so once this is made, I can continue on from there without a problem. 在此之前,我已经在C#解决方案中使用过COM对象,因此一旦完成,就可以从那里继续进行而不会出现问题。

However, I don't know what to do to make the COM object from the 2 .dll files, 1 .exe, 1 .lib file, 1 .xml file, and the 12 .h files. 但是,我不知道该怎么做才能从2个.dll文件,1个.exe文件,1个.lib文件,1个.xml文件和12个.h文件制作COM对象。 Is there a resource available to tell me what to do to make this happen? 有没有资源可以告诉我该怎么做?

My boss was a C++ developer and says all I need to do is to compile it as a COM object and then import the COM object into my C# solution. 我的老板是一位C ++开发人员,他说我要做的就是将其编译为COM对象,然后将COM对象导入到我的C#解决方案中。

That's true, however compiling it as a COM object is "difficult" (by which, I mean that you can't do it) unless it already implements the COM APIs (if it doesn't then you need to implement the COM APIs before you can build it as a COM object). 没错, 但是除非将其编译为COM对象是“困难的”(也就是说,您不能这样做),除非它已经实现了COM API(否则,则需要先实现COM API)您可以将其构建为COM对象)。

There are books (for example, Essential COM ) which explain how to to create COM objects using C++, but it's non-trivial (for building COM objects there may be better books than Essential COM , and better/easier tools than C++). 有一些书籍(例如, Essential COM )解释了如何使用C ++创建COM对象,但是这并非易事(对于构建COM对象,可能比Essential COM的书籍更好,而比C ++的工具更好/更容易)。

I think you and/or your boss have 3 options: 我认为您和/或您的老板有3种选择:

  1. Ask the vendor to give them to you as COM objects 要求供应商将它们作为COM对象提供给您
  2. Design a COM API that would wrap the SDK's API. 设计一个将包装SDK API的COM API。 Create a COM project (in the language of your choice) which exports this API. 创建一个COM项目(使用您选择的语言),该项目将导出此API。 Implement these APIs by invoking the underlying SDK methods. 通过调用基础SDK方法来实现这些API。 To do this you may need someone who knows C++, or be willing to spend much, much longer than "an hour" on this project. 为此,您可能需要一个了解C ++的人,或者愿意花费比“一个小时”更长的时间在这个项目上的人。
  3. Forget about using COM; 忘记使用COM了; instead, build the SDK as a DLL, and use PInvoke to invoke it from .NET code. 而是将SDK构建为DLL,然后使用PInvoke从.NET代码中调用它。

My boss says it should take less than an hour to "wrap" there SDK up as a COM object, even for me with no knowledge of C++ compiling. 我的老板说,即使对于不了解C ++编译的我来说,将SDK作为COM对象“包装”起来也需要不到一个小时的时间。

Based on what you've said I don't know of any way to make that happen. 根据您所说的,我不知道有什么办法可以实现。

Tell your boss if it would take him less than an hour to wrap it up, he should certainly do it: it would be a more efficient use of both your time. 告诉您的老板,如果要用不到一个小时的时间将其打包,他肯定应该这样做:这将更有效地利用您的时间。

I would also suggest ATL (not using attributes), but this is something which can take some time to get right if you're not experienced. 我还建议使用ATL(不使用属性),但是如果您没有经验,这可能需要一些时间才能正确。

My boss says it should take less than an hour to "wrap" there SDK up as a COM object, even for me with no knowledge of C++ compiling. 我的老板说,即使对于不了解C ++编译的我来说,将SDK作为COM对象“包装”起来也需要不到一个小时的时间。

That may be true for an experienced C++/COM developer, but not for a beginner. 对于经验丰富的C ++ / COM开发人员而言可能是正确的,但对于初学者而言并非如此。 I think your best bet is to use ATL. 我认为您最好的选择是使用ATL。 Take a look at the MSDN tutorial . 看一下MSDN教程

And do not use attributes. 并且不要使用属性。

This doesn't quite answer your question, but... 这并不能完全回答您的问题,但是...

One option instead of trying to make a COM object is to use P/Invoke and just call the methods in the DLL. 一种选择而不是尝试创建COM对象的方法是使用P / Invoke并仅调用DLL中的方法。

This thread on the MSDN Forums documents how to make a DLL to call using P/Invoke. MSDN论坛上的该线程介绍了如何使DLL使用P / Invoke进行调用。

Of course if you need access to a whole class (and make an instance of said object), this isn't going to be helpful. 当然,如果您需要访问整个类(并创建所述对象的实例),那么这将无济于事。

It's good that the code compiles and runs for you. 该代码可以为您编译并运行是件好事。 That said it's totally unfair to assume you can do this in an hour. 也就是说,假设您可以在一小时内完成此操作完全不公平。

Have you checked to see what is actually being built by Visual Studio. 您是否检查过Visual Studio实际正在构建的内容。 There could be a chance that it is already building a COM object. 它可能已经在构建一个COM对象。

Investigate how the code is being called. 研究如何调用代码。 I assume you have a .exe that you can run to test the code. 我假设您有一个可以运行以测试代码的.exe。 Step through this in the VC++ debugger (it's similar enough to debugging C# code) and try to identify any API calls that match with your docs/expectations. 在VC ++调试器中逐步完成此步骤(这与调试C#代码非常相似),并尝试识别与您的文档/期望匹配的任何API调用。 This knowledge will be helpful if you try to go the P/Invoke route. 如果您尝试采用P / Invoke路线,则此知识将有所帮助。

Something else to consider is SWIG . 另外要考虑的是SWIG This is typically used by Python developers to wrap C/C++ code and provides some support for C#. Python开发人员通常使用它包装C / C ++代码,并为C#提供一些支持。

The managed C++ route is probably more advisable for experienced C++ devs because you need to understand a lot about memory allocation, for all but the simplest code. 对于有经验的C ++开发人员来说,托管C ++路由可能更可取,因为除了最简单的代码之外,您还需要了解很多有关内存分配的知识。

I (well, really my lead and I) will first try using p/Invoke (via the DllImport feature of System.Runtime.InteropServices) against the SDK's dll the company provided. 我(嗯,确实是我和我的领导)首先将针对公司提供的SDK dll使用p / Invoke(通过System.Runtime.InteropServices的DllImport功能)。 I'll let you know how it goes. 我会让你知道怎么回事。

I think what you really want/need is C++/CLI, that way you can just build them directly into a managed assembly. 我认为您真正想要/需要的是C ++ / CLI,这样您就可以直接将它们直接构建到托管程序集中。 The idea is that you write a pretty plain wrapper that looks like kind of a cross between C# and C++ and then it does the rest. 这个想法是,您编写一个非常普通的包装,看起来像C#和C ++之间的交叉,然后其余的工作就完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM