0

我有以下两组代码,它们都产生相同的结果:

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

问题只是我做错了什么?我需要清除 Internet 缓存,并且更喜欢使用此方法,因为我知道它在工作时会执行我想要的操作(rundll32 inetcpl.cpl,ClearMyTracksByProcess 8 工作正常)。我尝试以普通用户和管理员身份运行均无济于事。本项目在 VS2012 中使用 C# 编写,针对 .NET3.5 编译(由于客户端限制,必须保持在 3.5)

4

1 回答 1

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);
    }
}
于 2013-06-28T16:32:52.003 回答