Solved

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

Posted on 2013-06-17
5
1,013 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

840 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