简体   繁体   中英

In a C# project, how can I refer to a project written in C++ and compiled with /clr?

I have a fairly large project written in unmanaged C++ and generating a DLL. I would like to be able to use the DLL in a .Net project (in this case, just a bare-bones WinForm application). I rebuilt the DLL with /clr set. It built successfully. In my WinForm project, I selected Add Reference, selected the Project tab, and selected the DLL project. It was successfully added as a reference in my WinForm project. However, I am having trouble using the DLL. I want to add a using directive for it, but I do not know what name the DLL has. I tried using the same name as the file, but it didn't work. How do I find out what its name is for use in a using directive?

Thanks very much!

RobR

The /clr switch enables you to mix .NET types ( ref class , interface class ) with the other C++ code. It doesn't magically make native C++ code usable from C#.

Among other reasons, C++ code heavily relies on object addresses remaining constant, but in .NET they do not. .NET requires that all objects have a v-table so that the garbage collector can find out what data type the object is (and therefore where within the object pointers are contained) but most native types have no v-table at all. The compiler can't make native types .NET-compatible without changing the layout and breaking compatibility with native code. Which would defeat the purpose. So while the compiler can generate metadata for the native types, it's not useful to C#. Instead it lets you include .NET-compatible classes and native types together in the same DLL and internally call between them seamlessly.

To answer your other question "How can I find out what namespaces are defined within an arbitrary .NET assembly?" use an assembly viewer/decompiler such as dotPeek, .NET Reflector, etc.

you need to write managed wrapper classes in C++. Check this msdn article .

Actually you dont need to compile the entire dll as /clr, only the managed wrappers. Not all c++ code works with /clr and there is a modarate slowdown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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