Jason Huang
Jason Huang

Reputation: 216

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?

Upvotes: 0

Views: 387

Answers (2)

Robert Harvey
Robert Harvey

Reputation: 180908

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

Upvotes: 1

Mayur Tendulkar
Mayur Tendulkar

Reputation: 718

You can load that link library inside an Application Domain and then unload that application domain.

Upvotes: 0

Related Questions