Question

J'utilise un code non géré comme -

 [DllImport("wininet.dll")]
    private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
    //Creating a function that uses the API function...
    public static bool IsConnectedToInternet() {
        int Desc;
        return InternetGetConnectedState(out Desc, 0);
    }

Toutes les suggestions sur la façon dont je peux disposer / nettoyer cet objet statique extern quand je l'appelle Éliminez?

Était-ce utile?

La solution

La chose que vous pensez est un « objet statique extern » est pas un objet du tout, il est juste un ensemble d'instructions au compilateur / runtime sur la façon de trouver une fonction dans une DLL.

Comme Sander dit, il n'y a rien à nettoyer.

Autres conseils

Vous n'avez pas poignées de ressources non gérées ici. Il n'y a rien à nettoyer.

Cela dépend si vous pouvez obtenir un pointeur, comme un exemple

[DllImport("advapi32.dll", SetLastError = true)]
static extern bool LogonUser(string principal, string authority, string password, LogonSessionType logonType, LogonProvider logonProvider, out IntPtr token);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr handle);

public void DoSomething() {
    IntPtr token = IntPtr.Zero;
    WindowsImpersonationContext user = null;
    try {
        bool loggedin = LogonUser((string)parameters[1], (string)parameters[2], (string)parameters[3], LogonSessionType.Interactive, LogonProvider.Default, out token);
        if (loggedin) {
            WindowsIdentity id = new WindowsIdentity(token);
            user = id.Impersonate();
        }
    } catch (Exception ex) {

    } finally {
        if (user != null) {
            user.Undo();
        }
        if (token != IntPtr.Zero) {
            CloseHandle(token);
        }
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top