Problem creating COM assembly in C# 2005 - c#

I am kind of new to C#. I built a class library in order to create a DLL which I need to reference from a VB.Net application.
I followed all the steps detailed in this this article in order to generate the COM assembly based on my C# class library.
All the process ran smoothly (create the key, run gacutil.exe to add the assembly to the cache), but I still can't see the DLL from my main project in order to add it as a reference.
What am I doing wrong? Any help will be appreciated

There's some pretty bad advice in that article, the GAC is a deployment detail and has no relevance to the task of writing and using an assembly on your dev machine. The entire process also has nothing to do with COM at all.
Get ahead by opening your VB.NET solution in Visual Studio. Right-click the solution in the Solution Explorer window, Add, New Project. Pick "Class Library" from the C# node. Now right-click your VB.NET project, Add Reference, Project tab and select your C# project. Any of the public C# classes you write are now available in your VB.NET code.
You might not see the solution if it contains only one VB.NET project. Fix with Tools + Options, Project and Solutions, General, tick "Always show solution".

Related

Self-built C# Library not found

I have an issue including a self-built library to a C#-project. I have created an own class library called ClassLibrary1 just to learn how to add libraries on Visual Studio 2019.
So I have written some simple code in a newly created .NET-class library project and have clicked on "create new solution" (directly translated from my german IDE-language. Maybe it's called slightly different) after writing the code. Back in the C#-project, I have selected the dll-file from bin/Debug/ of the class library's project folder.
After I have set the checkmark, the dll-file was shown in the solution-explorer under Assemblys like expected. But the issue I now have is that I still cannot use the ClassLibrary1.dll-file in the cs-file in this very project as I expected via the command "using ClassLibrary1;". It only shows me the error message "type- or namespacename "ClassLibrary1" not found" when trying to compile the C#-project and I don't get, why this is the case.
It seems like it has to be a very obvious problem but after some research on the internet and trying some things by myself still nothing has changed.
Thanks in advance for helpful replies.
The by far easiest way to manage a library is to use project references. Ensure that your library and the project that uses the library is in the same solution. Then right click the "references" and select "add Reference", go to the project tab and add a checkbox for the library. Read more about managing references.
You might also need to add namespaces for the classes you wish to use in the source files.
I would not recommend managing using file-references to lose dll-files, since it can easily become a hassle to manage. I.e. if you create a new version of the library you would need to build, and explicitly replace this file in all other projects and update all the references.
If you want to share libraries between multiple solutions the more popular solution would be to setup a nuget server. This solves some of the updating problems by maintaining multiple versions of the same library, and provides a nice interface to update references in all projects. But this is a somewhat more complicated solution, so I would not recommend this for new developers.

Is there a way to reference a c++ project from a c# project in the same solution?

In my solution we have projects both in c#, that controls some GUI and networking work, and c++, that manages some hardware interactions. In my c# project I have the proper PInvokes and am able to use the c++ output dll with no issue but in order to do it, I have to manually copy the output dll to the build directory or create a build script that manages the copy.
My issue with this method is that the solution, in reality, has many many projects, something like 150 at the moment, covering c++, c, c#, and vb.net. We create and delete projects all the time and managing the copy scripts is becoming a major pain. Especially since not all of the projects rely on each other and we have like 20 different build configurations.
Is it possible to simply have the c# project reference the c++ project and automatically copy the project output the same way it does with other managed projects without using post build scripts?
Well, the way I do it and have always done it is by obviously using Visual Studio, and assuming this C++ projects are VS projects you can easily create a VS Solution containing multiple projects that you can organize with "Solution Folders". The organization of your projects inside the solution is really up to how you want to organize it. It resembles a file system with nested folders. Needless to say that you can host projects in different languages such as C++, C#, VB...I'm not too sure if you can include a C project or not, that's out of my expertise.
See a screenshot below of a solution I created to demonstrate this...
Notice that "Business" has a nested solution folder (Utils) which contains a C++ project (ERM.CPPLibraries) and a VB project (ERM.VBLibraries). Then if you reference projects within the solution (Right click -> Add Reference), you will not need to copy the output assemblies everytime you compile your solution (or project(s)) VS is smart enough to resolve all dependencies, resolve them and update them.
Hope it gives you an idea
Edit based on comment
In simple words...No, it's not possible to reference a unmanaged project from a managed project in a VS solution. You can reference DLLs but not projects itself

Importing C# dll to C++ managed code (.NET)

I'm using Visual Studio 2010. I've written a dll in C#, managed dll then.
Now for some reason, I need to write a software in C++ (.NET then also managed).
I need to import the C# dll into my C++ .NET code.
I can't figure out to do this, I've made several search but this problem seems to not be covered. For example, in C# I don't have include file, then how my C++ (.NET) projet knows about classes and functions inside the dll?
Thanks,
In Visual Studio, bring up the properties of the C++/CLI project, go to "Common Properties/Framework and References" in the tree at the left, and click the "Add New Reference" button. This will bring up the standard "Add Reference" dialog you can get from a C# project, just select your C# DLL or reference a C# project in the same solution.
You need to add a reference into your project. In Visual Studio, right-click your project, then select "References".
You add reference to you assembly, Set ComVisible attribute to your assembly
Edit your AssemblyInfo.cs
[assembly: ComVisible(true)]
.Net Framework have MSIL langage in order to manage interoperability betwwen different langages
Link : http://support.microsoft.com/kb/828736
You just need to add only reference of that dll in your project, as George has replied.
then use that namespace or name of classes in your code...
It is possible. Google search would gave you answers. Few links from Stackoverflow
How to use c# Dll in vc++?
using c# dll in project c++
and you get many more links https://www.google.co.in/#sclient=psy-ab&hl=en&site=&source=hp&q=using+c%23+dll+in+vc%2B%2B&oq=using+C%23+dll+&gs_l=hp.3.2.0l4.1601.6409.0.9065.18.13.2.3.3.2.468.2716.0j9j1j2j1.13.0...0.0...1c.1.ixoWIPWicqo&pbx=1&bav=on.2,or.r_gc.r_pw.&fp=64f4e49ac7d1c408&biw=936&bih=595
Hope this helps

How to Create a DLL file in Visual Studio C# 2010 Express edition?

I have already come across the Stack Overflow question "Is there a way to generate a DLL file from Visual Studio Express without explicitly creating a DLL project?", but it does not directly answer my query, so I'm raising it here.
The problem I am facing while trying to make the DLL is that I can't find any option under Build named build class file.
I have changed the project property to class file (shown below)
This is how it is:
And here is how my build option is getting displayed:
Also when I am using the command-line option the dll file is getting generated but it is not getting the properties I'm setting in the application.
I am new with Visual Studio so a litte bit confused about this part.
The "Build Solution" option in your second screenshot is the thing you need to click to produce your dll, alternatively you can right click on your project in the Solution Explorer and click "Build":
(If you only have one project in your solution then these two will both do exactly the same thing)
The output dll will normally be placed in the bin\Debug or bin\Release directory depending on whether you are in Release or Debug configuration, check the "Build" tab of the project properties for the exact path.
The reason why you aren't seeing a "Build class file" option is because this is what the "Build project" menu item does - it will produce a class library if the project output type is "Class Library", a windows executable if the project output type is "Windows Application" etc...
You're not trying to build a class file - you're trying to build a class library.
And you just build the solution - that will build each of the projects in your solution, including your LicenseCheckLibrary project is just a class library project.
It looks like you're basically there - look in the bin\Debug or bin\Release folders under LicenseCheckLibrary, and you'll find the DLL.
Why would you want to avoid building a DLL file in the first place? Are you developing an EXE file in order to test the logic and then conver it to DLL once it is working fine? If yes, why not create two projects: Windows Console and Class Library. Inside Class Library implement the licensing logic and use Windows COnsole to test the logic. When you say you are new with Visual Studio, what exactly do you mean? You never used it before or you are new to .NET Framework programming? .NET Framework has certain classes for developing licenses. Also, there were quetions here on stackoverflow regarding the licensing. Find some of them instead of reinventing the wheel.
Have a look at this article http://www.developer.com/net/net/article.php/3074001
Create a new class library project
Create classes and code
compile Project
Dll Created
Create a new project
Click on Add Reference
Navigate to the class library folder
Go into the debug folder or whatever and include
Remember you will prob have to include the namespace. in the new
project.

Visual Studio equivalent of the Delphi "search path"

I've made a C# class defined in a .cs file, which I'm using in an existing project.
Let's call it the Magic class, defined in Magic.cs.
Now I'm working on a new project, and I'd like to instantiate an instance of that class in my new project.
How can I reference that class in my new project without actually copying the file into the new project's directory?
In Delphi, the answer would be "add the location of the class definition to your search path". This is an embarassingly stupid question, but I can't find a good answer anywhere.
Here is what I've tried:
1 - Project->Properties->Reference Paths->Add the location of my class (references to the class are still unresolved)
2 - Right click on project -> "Add existing item" -> choose the class (creates a new separate copy in my project folder)
3 - Right click on project -> "Add reference" -> see that it is expecting a compiled target like a DLL.
See also
How do you share code between projects/solutions in Visual Studio?
Visual Studio&Source Control: How to have shared code?
You are in a whole new world. There is no equivalent.
If you want to share the class with out copying it in you create a class library and build your class into that library. Then you reference that built library from your project.
You can add a class library to your solution and the reference the project while you are developing. Class libraries act very similarly to the way BPLs do if you ever used those.
Once you have a class library you can then share the library between your solutions. This can be done in 2 ways - by building the library and sharing the binary - or by including the class libraries project in your solution. At first you probably want to go with the latter method till you get the hang of it, but once things get quite large and your shared code settles down it is better to reference the pre-built binary.
There is learning curve here - but at the end of the day you will be better off.
Good luck.
Joseph, Visual Studio does not provide this functionality, so must reference each class manually. Anyway to avoid to create a copy of your class every time which is included in a new project you must use the a option Add existing item selecting add as link.
Sorry but you cant do this, its good to know that Delphi provide such facility but may be Visual Studio doesn't do this because at the end of the day this may lead to a mess of linked files. Visual studio likes to organize related files in the same project or solution.
If you want to have your earlier class available then as mentioned above you have only two options.
1) Simply copy the existing .cs file in your project directory (By Right click on project -> "Add existing item" -> choose the class )
2) Add the Project in your solution (Right click on Solution -> Add -> Add Existing Project... -> Select the project file from File Browser) and then Add the class refrence (Right click on project -> "Add reference" -> In Projects Tab Select your Project). It will automatically make a reference to the .dll.
3) And last option is to compile your class in a .dll and then add the refrence to it in your Project.
Good Luck
You want to reference either a compiled .Net assembly (dll in this case) or another project that is part of the your solution. If you have the source, you add the library project to your solution. Then add the library project as a reference to the project that uses it. When it compiles, it'll copy the dll over to your build directory. Using this method is nice because you'll be able to step through the code in the library when using the debugger.

Categories