Solved

C# - How to add unmanaged dll to a c# project

Posted on 2013-06-17
5
893 Views
Last Modified: 2013-06-17
I have a unmanaged 'C' dll  I need to used in a C# project.  I have created dllimport for my 'C' functions.  I was unable to add reference.  How do I do?  When I right click  on the project and select add reference.  I got an error whether I choose extension of assemblies or COM. See attached screen shots.  I am currently using Visual Studio 2010.
Capture1.JPG
Capture2.JPG
0
Comment
Question by:tommym121
  • 2
  • 2
5 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Is the DLL a COM DLL? If not, then you will need to import the functions by name using the DLLImport attribute. Otherwise, if the DLL is a COM DLL, then you may need to register the DLL on your system using regsvr32.exe.
0
 

Author Comment

by:tommym121
Comment Utility
It is not a COM dll.  So I don't need use to Add reference. Am I right?
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 200 total points
Comment Utility
You cannot use "Add Reference," no. As long as you know the names of the functions you are trying to call, you should be able to explicitly import those functions via a DllImport attribute. Make sure you know the calling mechanism used by the DLL as well as the types on the parameters as you may need to perform some marshalling in order to pass and receive data correctly.
0
 
LVL 15

Accepted Solution

by:
Minh Võ Công earned 300 total points
Comment Utility
You don't want add reference c++ dll into C# project.
You copy c++ dll to bin folder of project and you DLLImport
cpp project
int __declspec(dllexport) a(int b)
{
      return b;
}
c# project
class Program
{
   [DllImport(@"c:\a.dll")]
   private static extern int a(int b);
   static void Main(string[] args)
   {
       Console.WriteLine(a(3));
   }
}
http://www.codeproject.com/Articles/27298/Dynamic-Invoke-C-DLL-function-in-C
http://www.codeproject.com/Articles/1285/Calling-API-functions-using-C
0
 

Author Closing Comment

by:tommym121
Comment Utility
Thanks
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now