How to get imported DLL handle
-
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 ?
-
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 ?
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 -
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