Solved

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

Posted on 2013-06-17
5
920 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 75

Expert Comment

by:käµfm³d 👽
ID: 39253122
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
ID: 39253144
It is not a COM dll.  So I don't need use to Add reference. Am I right?
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 200 total points
ID: 39253159
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
ID: 39253195
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
ID: 39253228
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

947 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

22 Experts available now in Live!

Get 1:1 Help Now