6

私は次のようなアンマネージコードを使用しています-

 [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);
    }

Disposeを呼び出すときに、このextern静的オブジェクトを破棄/クリーンアップする方法に関する提案はありますか?

4

3 に答える 3

6

'extern static object'と思われるのは、オブジェクトではなく、DLLで関数を見つける方法についてのコンパイラ/ランタイムへの一連の指示です。

サンダーが言うように、クリーンアップするものは何もありません。

于 2011-01-18T08:52:29.817 に答える
3

ここには、管理されていないリソースへのハンドルはありません。クリーンアップするものはありません。

于 2011-01-18T08:47:48.713 に答える
2

例として、ポインタを取得できるかどうかによって異なります

[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);
        }
    }
}
于 2011-01-18T08:56:57.163 に答える