Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to get imported DLL handle

How to get imported DLL handle

Scheduled Pinned Locked Moved C#
questioncsharpc++tutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fu0
    wrote on last edited by
    #1

    in my C# app, i want to import somefuntions from win32 dll. look like this: [Dllimport("own.dll")] public static extern void function(); then use the function(). i want to get the own.dll 's handle like LoadLibrary("own.dll") in VC++. but HOW ? and if i import 2 or more functions from defferent win32 dll, HOw to get their handles respectivly ? and last question: HOW to free the DLL like FreeLibrary do ?

    N 1 Reply Last reply
    0
    • F fu0

      in my C# app, i want to import somefuntions from win32 dll. look like this: [Dllimport("own.dll")] public static extern void function(); then use the function(). i want to get the own.dll 's handle like LoadLibrary("own.dll") in VC++. but HOW ? and if i import 2 or more functions from defferent win32 dll, HOw to get their handles respectivly ? and last question: HOW to free the DLL like FreeLibrary do ?

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      fu0 wrote: i want to get the own.dll 's handle like LoadLibrary("own.dll") in VC++. but HOW ?

      [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
      static extern IntPtr LoadLibrary(string lpFileName);
      [DllImport("unmanaged.dll")]
      static extern int YourFunction(int i);
      string path = @"..\somedir\own.dll";
      IntPtr ptr = LoadLibrary(path);
      int i = YourFunction(7);

      In .NET 2.0 you will be able to do this:

      [DllImport("kernel32.dll")]
      internal static extern IntPtr LoadLibrary(String dllname);
      [DllImport("kernel32.dll")]
      internal static extern IntPtr GetProcAddress(IntPtr hModule, String procname);
      internal delegate int MyMsgBox(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)]String text, [MarshalAs(UnmanagedType.LPWStr)]String Caption, int type);
      IntPtr user32 = LoadLibrary("user32.dll");
      IntPtr procaddr = GetProcAddress(user32, "MessageBoxW");
      MyMsgBox msgbox = (MyMsgBox)Marshal.GetDelegateForFunctionPointer(procaddr, typeof(MyMsgBox));
      msgbox(IntPtr.Zero, "Hello, World", "A Test Run", 0);

      - Nick Parker
      My Blog | My Articles

      F 1 Reply Last reply
      0
      • N Nick Parker

        fu0 wrote: i want to get the own.dll 's handle like LoadLibrary("own.dll") in VC++. but HOW ?

        [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
        static extern IntPtr LoadLibrary(string lpFileName);
        [DllImport("unmanaged.dll")]
        static extern int YourFunction(int i);
        string path = @"..\somedir\own.dll";
        IntPtr ptr = LoadLibrary(path);
        int i = YourFunction(7);

        In .NET 2.0 you will be able to do this:

        [DllImport("kernel32.dll")]
        internal static extern IntPtr LoadLibrary(String dllname);
        [DllImport("kernel32.dll")]
        internal static extern IntPtr GetProcAddress(IntPtr hModule, String procname);
        internal delegate int MyMsgBox(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)]String text, [MarshalAs(UnmanagedType.LPWStr)]String Caption, int type);
        IntPtr user32 = LoadLibrary("user32.dll");
        IntPtr procaddr = GetProcAddress(user32, "MessageBoxW");
        MyMsgBox msgbox = (MyMsgBox)Marshal.GetDelegateForFunctionPointer(procaddr, typeof(MyMsgBox));
        msgbox(IntPtr.Zero, "Hello, World", "A Test Run", 0);

        - Nick Parker
        My Blog | My Articles

        F Offline
        F Offline
        fu0
        wrote on last edited by
        #3

        THanks for your suggestion!!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups