• Advertisement

Archived

This topic is now archived and is closed to further replies.

Noob C# Question

This topic is 5038 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know in c++ you could export the class like this: class _declspec(dllexport) test { public: void func(void); }; How do you export a class in C#? I looks like you dont have to define which class or function you want to export. Is just how u setup your project file? When I created my project I selected "Library" as the type. I have left all the project settings default. It builds the DLL but when I view it with the Dependencies Viewer there are no functions in the dll? Why is that? Is there a way to export your classes in C# to a dll. I believe once I get building the dll correctly I can use what I wrote In C# with C++. Using LoadLibrary() and GetProcAddress().

Share this post


Link to post
Share on other sites
Advertisement
quote:
How do you export a class in C#?

You don''t

In C# you use the access specifiers. So if a class is marked public then it is accessible by anyone who uses that assembly, which is equivalent to exporting a class in C++. If it is marked as internal then it can only be used inside by code inside of that assembly.

quote:
I believe once I get building the dll correctly I can use what I wrote In C# with C++. Using LoadLibrary() and GetProcAddress().

Nope. All you have to do is add a reference to the assembly in your project (assuming you''re using managed C++, I''m sure sure about when you''re using unmanaged C++).

Share this post


Link to post
Share on other sites
quote:
Original post by starboarder
I believe once I get building the dll correctly I can use what I wrote In C# with C++. Using LoadLibrary() and GetProcAddress().


No. C# cannot create standard DLL exports. Currently, only Managed C++ and Delphi.NET of the .NET languages can do this.

If you want your C# assembly to be accessed from unmanaged C/C++, you need to do it through COM. The tlbexp utility will take a C# assembly and create a COM typelib out of all the public classes in that assembly. Those classes can then be used in C++ as any other COM object - IUnknown comes for free, I believe, but if you want IDispatch as well you need to apply some special attributes to your assembly. Check documentation for more details. Keywords to look for are "interop" and/or "COM interop".

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share this post


Link to post
Share on other sites

  • Advertisement