我有以下两组代码,两者都产生相同的结果:
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);
}
}
都使用以下枚举:
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)
答案 0 :(得分:0)
rundll32
功能的签名是:
void CALLBACK EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow);
所以第一个问题是你的第三个参数是一个枚举,它变成一个整数类型,但你需要传递一个字符串。另请注意,这是LPSTR
,而不是LPTSTR
,因此字符集是ANSI,而不是Unicode。
其次,调用约定是stdcall
而不是cdecl
。
所以也许更多的内容如下:
[DllImport("InetCpl.cpl", SetLastError = true, CharSet = CharSet.Ansi)]
private static extern void ClearMyTracksByProcess(IntPtr hwnd, IntPtr hinst, string lpszCmdLine, int nCmdShow);
测试哪个在这里工作,至少在获得函数返回时:
class Program
{
[DllImport("InetCpl.cpl", SetLastError = true, CharSet = CharSet.Ansi)]
private static extern void ClearMyTracksByProcessW(IntPtr hwnd, IntPtr hinst, string lpszCmdLine, int nCmdShow);
static void Main(string[] args)
{
ClearMyTracksByProcessW(IntPtr.Zero, IntPtr.Zero, "2", 5);
}
}