+ Reply to Thread
Results 1 to 2 of 2

Thread: How to call DLL function from C#

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    1

    How to call DLL function from C#

    Hi All,

    I just want quick example how to call DLL function in C#. I came from Java background and I used to use JNI for DLL calls. Now I'm learning C# and it will be great if someone can show me example. Thanks in advanced!

  2. #2
    Here is an example how to call DLL API function from C#:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;  // for dll import
    using System.Linq;
    using System.Text;
    
    namespace CallDLLExample
    {
        class Program
        {
    
            // To call a DLL function from C#, you must provide this declaration.
            [DllImport("Kernel32.dll")]
            private static extern int Beep(uint freq, uint duration);
    
            [DllImport("user32.dll")]
            private static extern int MessageBox(uint handle, string text, string caption, uint type);
    
            static void Main(string[] args)
            {
    
                Beep(100, 1000);
                MessageBox(0, "test message", "caption", 48);
    
            }
        }
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

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