locked
DLLImport and entrypoint in c# RRS feed

  • General discussion

  • i made a dll file with this code

    using System;
    
    using System.Collections.Generic;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Runtime.InteropServices;
    
    using System.Reflection;
    
    namespace _1stDLL
    
    {
    
     
    
     public class mydll
     {
    
    
    
       public static int sum(int i,int j) 
    
      {
    
      return (i + j);
    
      }  
    
     }
    
    }
    
    <br/>
    
    

    and a aplication to call the method sum in the dll file i created

     [DllImport("c:\\1stDLL.dll")]
    
     
    
      public static extern int sum(int i,int j); 
    
    
    
    
    
    private void button1_Click(object sender, EventArgs e)
    
      {
    
       richTextBox1.AppendText(sum(9,9).ToString());
    
       
    
      }
    
    

    the follong error happens

    System.EntryPointNotFoundException was unhandled
      Message=Unable to find an entry point named 'sum' in DLL 'c:\1stDLL.dll'.

    so the question is what is the entrypoint and how i use it to call the sum method

    • Changed type Prutal Monday, March 7, 2011 4:54 PM
    Monday, March 7, 2011 6:33 AM

All replies

  • Why are you using DLLImport on C# DLL? I am not sure whether DLLImport can be applied on C# assemblies. In your case, the 'sum' function in other ways. For example,

    1. Add a reference to that dll and call the function directly as 1stDLL.mydll.sum(9,9)

    2. If you don't want to add reference the dll, then you can make use of reflection.

     


    Please mark this post as answer if it solved your problem. Happy Programming !!!
    Monday, March 7, 2011 7:26 AM
  • P/Invoke or using DllImport attribute is used for calling unmanaged code from managed code. In your case you need not use the DllImport attribute.

    .Net Assemblies are self describing and you can just use the Add Reference to access the other assembly, moreover you will get intellisense support in viusal studio.

    However as Adavesh mentioned, you can use Reflection with .Net assemblies.

    I am also curious to know whether we can call .net assemblies using DllImport?  if no then why ?

     


    Abhinav Galodha
    Monday, March 7, 2011 9:08 AM
  • I am also curious to know whether we can call .net assemblies using DllImport?  if no then why ?
    You can call .NET assemblies using DllImport if you used a language able to generate mixed-mode assemblies, like C++. Other assemblies cannot be used with DllImport because the compiler won't expose any function.
    Monday, March 7, 2011 10:58 AM
  • Absolutely. If you take C#, it is a pure object oriented langauge. Which means, everything should be wrapped inside a class. For example, consider a MyLib.dll which has a class as below.

    public class MyClass
    {
     public static MyMethod()
     {
     }
    }
    

    If you want to DLLImport the MyMethod, how will you do it? You can't do something like below,

    [DLLImport("MyLib.dll")]
    public static extern void MyMethod();
    

    Because, from where to fetch the MyMethod as it is inside the class .Also, DLLImport doesn't access fully qualified MethodName seperated by dot (.). Which means you can't do,

    [DLLImport("MyLib.dll")]
    public static extern void MyClass.MyMethod();
    
    

    This concludes that you can't DLLImport methods from C# or VB.NET like classes.

     


    Please mark this post as answer if it solved your problem. Happy Programming !!!
    Monday, March 7, 2011 12:07 PM
  • so is there any way to tak eadvantage of the dll i write if yes tell me how and if not and i cant access the method inside what is the need of the the dll project in visual studio as i know from surfing the msdn that the way i used should work but isnt. any suggesion regards
    prutal
    Monday, March 7, 2011 5:01 PM
  • aHi Prutal,

    As I have already mentioned, there are 2 ways. Easiest way is,

    Add a reference to your dll :- Open Solution Explorer, Right Click on your project and select 'Add reference'. Now browse your c:\\1stDLL.dll and click Ok. Now you can access your function as,

    using _1stDLL;
    ..
    
    
    public partial class Form1: Form
    {
      ...
    
      private void button1_Click(object sender, EventArgs e)
     {
        int sum = mydll.sum(9,9);
        richTextBox1.AppendText(sum.ToString());
     }
    }
    

    Look at the first statement i.e "using _1stDLL;". If you don't specify this, you have to give full qualifier for sum() as _1stDLL.mydll.sum(9,9).

    Hope you get this.


    Please mark this post as answer if it solved your problem. Happy Programming !!!
    Monday, March 7, 2011 5:42 PM
  • To use a .NET dll:

    1) add a reference to the dll

    2) use the classes from that dll exactly as if they were in the same project

    Monday, March 7, 2011 5:47 PM