简体   繁体   中英

How to release memory when using Win32 dll?

dllManager.LoadFun(@"user32", "GetAsyncKeyState");
object tempFlag = dllManager.Invoke(ObjArray_Parameter, TypeArray_ParameterType, ModePassArray_Parameter, Type_Return, "GetAsyncKeyState");
r = (int)tempFlag;
llManager.UnLoadDll(); 

public void UnLoadDll()
{
    FreeLibrary(hModule);
    hModule = IntPtr.Zero;
    farProc = IntPtr.Zero;
}

dllManager.UnloadDll() does not seem to work, the memory is not released as when I call the DLL once again, it will continue to increase the memory. How do I release the memory?

In general, you declare an extern function, and don't worry too much about memory usage. Windows uses this DLL internally anyway.

[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(System.Windows.Forms.Keys key);

private void button1_Click(object sender, EventArgs e)
{
    short state = GetAsyncKeyState(Keys.D);
    switch (state)
    {
        // ?
    }
}

See also
http://www.pinvoke.net/default.aspx/user32.getasynckeystate

您可以在应用程序域中加载该链接库,然后卸载该应用程序域。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM