rodro
rodro

Reputation: 158

Problem with dll in C#

Hello I want to create a dll with some functions. For starters I'm trying to do some simple example to just test. I'm creating a new class library with for example such code as below. When I build it (no error) and create a dll file, I try to use it in my other project by

[DllImport("nllibrary.dll")]  
 public static extern long Add(long i, long j);

I can compile it but when I try to run the app , it gives me error "cannot find entry point". And when I look at this dll with depends.exe it shows no functions in the dll. What is wrong with my dll?

The code of the dll:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  


namespace nlLibrary  
{  
    public class nlClass
    {  

        public static long Add(long i, long j)
        {
            return (i + j*2);
        }           
    }      
} 

Upvotes: 0

Views: 150

Answers (2)

user1228
user1228

Reputation:

Also, you cannot run a DLL. When you try to run a dll (debug) it will give you that error message. If you want to test your DLL, look into creating a test project.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039398

You don't need to use the [DllImport] attribute. That's for calling unmanaged C/C++ libraries. For .NET you simply add the generated DLL to the project references and use it directly:

enter image description here

So for example if you have two projects in your Visual Studio solution called Proj1 (class library) and Proj2 (console application) you right click on the References of Proj2 and select Proj1 from the Project References Tab. Then you simply use the class directly:

long result = nlClass.Add(1, 3);

after having added the proper using to the namespace:

using nlLibrary;

Upvotes: 6

Related Questions