Visual C ++/CLI:.h与.cpp文件&更多的 [英] Visual C++/CLI : .h vs .cpp file & more

查看:58
本文介绍了Visual C ++/CLI:.h与.cpp文件&更多的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我已经用本机C ++和Visual C#编程,但这是我第一次尝试Visual C ++.我已经阅读了几篇描述C ++/CLI以及它如何从托管C ++成长的文章.激动不已,我决定尝试编写自己的练习应用程序. (实际上,我将在单独的C#应用​​程序中使用C ++/CLI中的类库.)

但是,我很快陷入了一些真正的基本问题,希望有人能帮助我. (我比要讲大声笑的人更有经验)这是我的问题:

1.当我创建类库(VS2010,文件->新项目->其他语言-> Visual C ++-> CLR->类库)时,我注意到一个MyLibrary.h文件和一个MyLibrary.cpp文件.在MyLibrary.cpp文件中,所有内容如下:

Hi all,

I have programmed in native C++ and visual C#, but this is the first time I have tried Visual C++. I have read several articles describing C++/CLI and how it has grown from managed C++. Excited, I decided to try to write my own practice application. (Actually, my own class library in C++/CLI which I will use from a separate C# application.)

However, I have quickly become stuck on a few really basic things and I was hoping someone could help me out. (I''m more experienced than I am about to sound LOL) Here''s my questions:

1. When I created the class library (VS2010, File->New Project-> Other languages->Visual C++->CLR->Class Library), I noticed a MyLibrary.h file and a MyLibrary.cpp file. Looking inside the MyLibrary.cpp file, all that was there is:

// This is the main DLL file.

#include "stdafx.h"

#include "MLibrary.h"



这似乎很基本.如果然后进入类视图,请右键单击该类并添加函数,我会注意到它已添加到.h文件中.那么,.cpp文件的目的是什么以及如何使用它?我显然明白了为什么.cpp文件在本机C ++中很重要,而对于C ++/CLI却不那么重要.例如,我无法将函数原型添加到.H文件并在.cpp文件中实现它-它会抱怨类类型为redefinition.

2.我想要的基本上是以下用例:
C#应用程序调用C ++/CLR类库.
CLR类库调用CoCreateInstance来接收COM接口并使用它.

我如何在库中公开可以执行此操作的函数,而无需将其保存在.h文件中? (为什么我不希望它包含在.h文件中?嗯,我想将它需要的#include保留在.h文件中.我也习惯于将.cpp文件中的代码)

3.最后,为什么会说:
IntelliSense:不适用于C ++/CLI"
我的意思是,这到底是什么?这是真的吗?当我喜欢像迷一样进入控制空间时,这可能会成为一个破坏者-它可以帮助我知道哪些变量&我拥有的功能,以及其他我可能不了解的功能.


基本上,我的原始问题是我发现C#不能调用特定的Windows方法(由于权限),而本机C ++可以.我不想创建本机C ++库,因为那样,所有的dllexport函数都必须是静态的,并且必须将数据从托管代码转换为非托管代码,并且如果我有托管C ++(C ++/CLI )代码库,那么,我要做的就是添加对它的引用,所有功能将立即公开.我不必为每个函数添加一堆PInvoke行.总而言之,C ++/CLI开始听起来很吸引人,但是上述问题令我感到担忧(尤其是关于IntelliSense,我担心对此没有解决方案).


感谢您的见解和帮助!



That seems awfully basic. If I then go into the class view, right-click the class and add function, I notice it gets added to the .h file. So, what is the point of the .cpp file and how to use it? I obviously get why a .cpp file is important in native C++, but not so much for C++/CLI. For instance, I can''t add a function prototype to the .H file and implement it in the .cpp file - it will complain of a class type redefinition.

2. What I want is basically the following use case:
C# application calls C++/CLR class library.
CLR class library calls CoCreateInstance to receive a COM interface and use it.

How can I expose a function in the library that would do this, without it being in the .h file? (Why don''t I want it to be in the .h file? Well, I''d like to keep the #include it is going to need out of the .h file. I''m also just accustomed to putting code in .cpp files)

3. Finally, why does it say:
IntelliSense: ''Unavailable for C++/CLI''
I mean, what the heck? Is this seriously true? This could be a dealbreaker as I like to hit control space like a junkie - it helps me know what variables & functions I have, and see others I might not have known about.


Basically, my original problem is I found out that C# cannot call a particular Windows method (due to permissions) but native C++ could. I didn''t want to have to create a native C++ library because then all the dllexport functions would have to be static, and I would have to marshal data from managed to unmanaged code, and if I had a managed C++ (C++/CLI) code library instead, then all I would have to do is add a reference to it and all of the functions would immediately be exposed. I wouldn''t have to add a bunch of PInvoke lines for each function. All in all, C++/CLI started to sound attractive but the above questions have me worried (esp. about the IntelliSense, for which I fear there is no solution).


Thanks for the insight and help!!

推荐答案

这是一堆复杂而复杂的问题.也许您不应该尝试立即解决它们.我不能保证您一次全部回答.我只想给您一个简单的想法(也不要同时解决所有问题):首先,尝试不要将C ++/CLI代码与已经在做的C ++混合使用.如果您很好地了解C ++,请从C ++开始.不要使用COM和其他旧版内容(为什么呢?).

您应该使用与C#几乎相同的方式来开发C ++/CLI代码.第一步,也许忘了.h文件.它们是C ++的产物,我不喜欢它,但是不幸的是,您确实需要它,但是稍后.要开始并理解基础知识,首先要使"ref"类或结构的所有成员都内联.请理解,程序集之间的.NET引用不像H文件那样使用任何东西,它使用的是元数据,就像在C#中一样. .h文件是C ++/CLI的内部功能,如果您在另一个类中引用一个类,则该文件可将不同的.cpp文件链接在一起.仅使用"ref" C ++/CLI类.

您的目标是在C#项目中使用C ++/CLI程序集.这与引用C#项目中的C#程序集完全相同.仅对于C ++/CLI,所有公共方法的参数都应该是引用("^"类型)或原始类型. C ++/CLI可以使用值语义,但是请不要在C ++/CLI程序集之外使用此功能,因为C#或VB.NET(F#等)项目无论如何都无法使用它们.

将来,当您决定将C ++与C ++/CLI混合使用时,请尝试将文件分成四个不同的类:1)纯C ++/CLI,2)纯本机C ++,3)内部使用C ++类型的C ++/CLI类型; 4)内部使用C ++/CLI类型的C ++类型.在现实生活中,几乎不需要在同一项目中同时使用(3)和(4).另外,在那些混合模式文件中,请避免实现除C ++和C ++/CLI之间的协作以外的任何其他功能.

—SA
This is a bunch of mixed and convoluted problem. Perhaps you should not try to solve them at once. I cannot promise you to answer them all at once. I just want to give you one simple idea (also about not addressing all things at once): at first, try not to mix C++/CLI code with C++ which you are doing already. If you know C++ reasonable well, start with C++. Don''t use COM and other legacy stuff (why, be the way?).

You should develop C++/CLI code pretty much the same way you do it with C#. For a fist step, perhaps forget about .h files. They are the artifact of C++, which I don''t like, but unfortunately, you will really need it, but a bit later. To start and understand the basics, start with having all members of a "ref" class or a structure inline. Please understand, .NET referencing between assemblies do not use anything like H files, it uses meta-data, exactly as in C#. The .h files is the internal feature of C++/CLI used to link together different .cpp files if you reference one class in another. Use only "ref" C++/CLI classes.

You goal is to use C++/CLI assemblies in C# projects. This is done in exact same way as referencing C# assemblies in C# project. Only with C++/CLI the parameters of all public method should be references (''^'' types) or primitive types. C++/CLI can use value semantic, but don''t use this feature outside C++/CLI assembly, as C# or VB.NET (F#, etc.) project would not be able to use them anyway.

In future, when you decide to mix C++ with C++/CLI, try to segregate files in four distinct classes: 1) pure C++/CLI, 2) pure native C++, 3) C++/CLI types using C++ types internally; 4) C++ types using C++/CLI type internally. In real life, you will hardly need to use both (3) and (4) in the same project. Also, in those mixed-mode files, avoid implementing any other functionality except collaboration between C++ and C++/CLI.

—SA


这是一个不错的查询-基本但不错.

请在以下位置找到一些不错的论点:

1. http://stackoverflow.com/questions/1305947/why- dos-c-need-a-separate-header-file [ http://social.msdn.microsoft. com/Forums/zh-CN/vclanguage/thread/e08ce967-b5d8-4457-93a9-f549d37b1807 [
This is a nice query - basic but a good one.

Please find some nice arguments regarding this at:

1. http://stackoverflow.com/questions/1305947/why-does-c-need-a-separate-header-file[^]

2. http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e08ce967-b5d8-4457-93a9-f549d37b1807[^]


我想我可能还不清楚.我知道为什么在C或常规C ++中需要.h文件,但这通常仅用于函数原型和内联.在我的C ++/CLI项目中,好像.cpp文件不再是一个文件了(顺便说一句,我一直觉得对原型的需求很愚蠢-编译器应该能够找到它出来,就像Java(或C#)一样.我想知道.cpp的用途是什么(如果您尝试创建Windows Forms CLI应用程序,它甚至没有.cpp文件!所有代码都放在.h中).同样,通过在c#项目中包含对C ++ \ CLI程序集的引用,可以轻松地验证您可以调用C ++ \ CLI方法.所以我很好.只是我发现它们在.h文件中,很奇怪.如果实现是在.h文件中,并且不需要原型,那么.cpp文件有什么用?

还有关于Intellisense的信息吗?

我需要使用COM,因为我正在调用几乎专门使用它的Windows API.我当时在想,关于C ++ \ CLI的好处是,我可以从C#应用程序中调用类的方法,而不是本机C ++中的静态函数.
I think I may not have been clear. I understand why .h files are necessary in C or regular C++, but that''s usually for function prototypes and inline''s only. In my C++/CLI project, it''s like the .cpp file is the one who isn''t necessary anymore (btw, I''ve always felt the need for prototypes is dumb - the compiler should be able to figure it out, as in Java (or C#)). I''m wondering what the use of the .cpp is for therefore (if you try to create a Windows Forms CLI application, it doesn''t even HAVE a .cpp file! All code goes in the .h). Also, it''s easy to verify that by including a reference to the C++\CLI assembly in the c# project, that you can call the C++\CLI methods. So I''m good with that. It''s just that I find it strange they are in .h files. If the implementation is in the .h file, and a prototype''s unnecessary, then what good is the .cpp file?

Also, and about the Intellisense?

I need to use COM because I''m calling a Windows API that uses it almost exclusively. I was thinking, the nice thing about C++\CLI is that from C# app I could call into a method of a class, vs a static function in native C++.


这篇关于Visual C ++/CLI:.h与.cpp文件&更多的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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