次の 2 セットのコードがあり、どちらも同じ結果を生成します。
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ResetIE
{
class Program
{
[DllImport("InetCpl.cpl", SetLastError=true, CharSet=CharSet.Unicode)]
public static extern long ClearMyTracksByProcessW(IntPtr hwnd, IntPtr hinst, ref TargetHistory lpszCmdLine, FormWindowState nCmdShow);
static void Main(string[] args)
{
TargetHistory th = TargetHistory.CLEAR_TEMPORARY_INTERNET_FILES;
ClearMyTracksByProcessW(Process.GetCurrentProcess().Handle, Marshal.GetHINSTANCE(typeof(Program).Module), ref th, FormWindowState.Maximized);
Console.WriteLine("Done.");
}
}
と ...
static class NativeMethods
{
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);
}
public class CallExternalDLL
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate long ClearMyTracksByProcessW(IntPtr hwnd, IntPtr hinst, ref TargetHistory lpszCmdLine, FormWindowState nCmdShow);
public static void Clear_IE_Cache()
{
IntPtr pDll = NativeMethods.LoadLibrary(@"C:\Windows\System32\inetcpl.cpl");
if (pDll == IntPtr.Zero)
{
Console.WriteLine("An Error has Occurred.");
}
IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "ClearMyTracksByProcessW");
if (pAddressOfFunctionToCall == IntPtr.Zero)
{
Console.WriteLine("Function Not Found.");
}
ClearMyTracksByProcessW cmtbp = (ClearMyTracksByProcessW)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(ClearMyTracksByProcessW));
TargetHistory q = TargetHistory.CLEAR_TEMPORARY_INTERNET_FILES;
long result = cmtbp(Process.GetCurrentProcess().Handle, Marshal.GetHINSTANCE(typeof(ClearMyTracksByProcessW).Module), ref q, FormWindowState.Normal);
}
}
どちらも次の Enum を使用します。
public enum TargetHistory
{
CLEAR_ALL = 0xFF,
CLEAR_ALL_WITH_ADDONS = 0x10FF,
CLEAR_HISTORY = 0x1,
CLEAR_COOKIES = 0x2,
CLEAR_TEMPORARY_INTERNET_FILES = 0x8,
CLEAR_FORM_DATA = 0x10,
CLEAR_PASSWORDS = 0x20
}
これを行う両方の方法は、コンパイルして正常に実行され、エラーは発生しませんが、両方とも、作業から戻ることはありません。PInvoke コードは次の VB から移植されたもので、追跡がかなり困難でした。
Option Explicit
Private Enum TargetHistory
CLEAR_ALL = &HFF&
CLEAR_ALL_WITH_ADDONS = &H10FF&
CLEAR_HISTORY = &H1&
CLEAR_COOKIES = &H2&
CLEAR_TEMPORARY_INTERNET_FILES = &H8&
CLEAR_FORM_DATA = &H10&
CLEAR_PASSWORDS = &H20&
End Enum
Private Declare Function ClearMyTracksByProcessW Lib "InetCpl.cpl" _
(ByVal hwnd As OLE_HANDLE, _
ByVal hinst As OLE_HANDLE, _
ByRef lpszCmdLine As Byte, _
ByVal nCmdShow As VbAppWinStyle) As Long
Private Sub Command1_Click()
Dim b() As Byte
Dim o As OptionButton
For Each o In Option1
If o.Value Then
b = o.Tag
ClearMyTracksByProcessW Me.hwnd, App.hInstance, b(0), vbNormalFocus
Exit For
End If
Next
End Sub
Private Sub Form_Load()
Command1.Caption = "削除"
Option1(0).Caption = "インターネット一時ファイル"
Option1(0).Tag = CStr(CLEAR_TEMPORARY_INTERNET_FILES)
Option1(1).Caption = "Cookie"
Option1(1).Tag = CStr(CLEAR_COOKIES)
Option1(2).Caption = "履歴"
Option1(2).Tag = CStr(CLEAR_HISTORY)
Option1(3).Caption = "フォーム データ"
Option1(3).Tag = CStr(CLEAR_HISTORY)
Option1(4).Caption = "パスワード"
Option1(4).Tag = CStr(CLEAR_PASSWORDS)
Option1(5).Caption = "すべて削除"
Option1(5).Tag = CStr(CLEAR_ALL)
Option1(2).Value = True
End Sub
問題は、単に私が間違っていることは何ですか? 私はインターネットキャッシュをクリアする必要があり、この方法を使用することを好みます。これは、動作するときに必要なことを実行することがわかっているためです (rundll32 inetcpl.cpl,ClearMyTracksByProcess 8 は正常に動作します)。通常のユーザーと管理者の両方で実行しようとしましたが、役に立ちませんでした。このプロジェクトは、VS2012 で C# を使用して記述され、.NET3.5 に対してコンパイルされます (クライアントの制限により、3.5 のままにしておく必要があります)。