CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: C# dll

  1. #1
    Join Date
    Dec 2005
    Posts
    180

    C# dll

    After someone creates a DLL in C# using the Microsoft Visual development environment, how would another programmer take that code, make a new project that includes the DLL's source and make a GUI that uses the DLL'S API?

  2. #2
    Join Date
    May 2006
    Posts
    306

    Re: C# dll

    In your References "folder", in the treeview of the solution explorer, find your DLL and add it.

    To find it when you click add reference or w/e, there's tabs, and one of them is "browse".

    Make sure the DLL's classes and/or anything are set to public, and that you have the using statement for the namespace of the DLL.

    "using MyDllProject;"

    That should do it.

  3. #3
    Join Date
    Oct 2008
    Posts
    51

    Re: C# dll

    I've had the same issue. Since the DLL is being compiled you will need to use the entry points defined in the DLL and make sure that the exe has access to it.
    Check to make sure that in the dll is being copied into the folder you executable is in. You assume that VS would put a dll from your project into your debug folder but odder things have happened.

  4. #4
    Join Date
    Dec 2005
    Posts
    180

    Re: C# dll

    I have done a few things. I have created a console app to use as the front-end to the project. Then I have added the DLL as a reference like this:

    1) In the Solution Explorer right-click "References" and select "Add Reference ...".

    2) Select the "Browse" tab.

    3) Navigate to the DLL and select it.

    4) Add the appropriate using directive to the code file(s) where you want to use the DLL.


    I have also found that I can add the Project that the DLL was made in into my new Solution file.

    I have included the namespace of the DLL in the "using" section of my code.

    And the intelligent type I am using recognizes this class when I declare it in my code.

    But it does not seem to recognize all the methods (api's) of the class.

    What am I doing wrong?



    Strangely, "Equals", "GetHashCode", "GetType" and "ToString" are not seen in the DLL's source code for available methods for this class.

    Also, I know that if you double click on the added reference, an Object Explorer should come up showing the available methods. This is not what happens:



    So now to look closer at the DLL source code. Could there be a problem in the fact that the constructor ( private NookieBookieDoopieDoo() ) is
    defined as a privvate? Could there be a problem that the methods are static?

    It looks something like this:

    Code:
    namespace BladaFlabaDab
    {
    
        public class NookieBookieDoopieDoo
        {
    
            private static readonly string SomethingYouDoNotNeedToKnow = @"SLAMADAMNADDORK";
    
            private NookieBookieDoopieDoo()
            {
            }
    
    
    
            public static void Send(string to, string subject, string body)
            {
                .
                .
                .
            }
    
            public static void Send(string to, string replyTo, string subject, string body)
            {
                .
                .
                .
            }
    
            private static void InitializeClient()
            {
                .
                .
                .
            }
        }
    }
    Could there be a problem in the fact that the constructor ( private NookieBookieDoopieDoo() ) is
    defined as a privvate? Could there be a problem that the methods are static?

  5. #5
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: C# dll

    The methods GetHashCode, ToString etc are implemented for all class instances - they are members of System.Object from which every class derives from (implicitly if not specified).

    The methods on your class are static (i.e. don't have an instance), so to gain access to them you should do :

    Code:
    NookieBookieDoopieDoo.Send("aaa", "bbb", "ccc");
    You should read up on instances (what they are) and static methods - the concept of having an instance of a class is fundamental to the way that .NET (and object orientation in general) works.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  6. #6
    Join Date
    Oct 2008
    Posts
    51

    Exclamation Re: C# dll

    First, in the future please use code tags to show your code, the image you posted was distorted on my system and others probably had issues with it as well.

    If you are just looking to access the functions in the DLL then you are really talking about creating a Class.
    1) Create a new class project and add it to your solution.
    2) From any project that will reference that class needs a reference to that project.
    3) Your "using" statement should reference the name space that the class is in.

    When you compile the code VS, will turn the class into a dll, and it will show up in the same folder as the executable.

    If you are looking to call functions from an existing DLL then there are some other things you should be aware of.
    1) Your code will have to reference interop services.
    2) You will have to use the DLL import attribute for any functions that will use the DLL's functions
    3) You will have to make sure that you either have a hard coded path to the DLL or that a copy of the DLL is in the folder, e.g. the debug folder, that your executable is running in.

    Microsoft has references to this on the MSDN site:
    http://msdn.microsoft.com/en-us/libr...attribute.aspx
    Code:
    using System;
    using System.Runtime.InteropServices;
    
    class Example
    {
        // Use DllImport to import the Win32 MessageBox function.
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
    
        static void Main()
        {
            // Call the MessageBox function using platform invoke.
            MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
        }
    }
    Last edited by ifdef; December 3rd, 2008 at 09:34 AM.

  7. #7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured