0

有一个简单的代码:

[DllImport("winspool.Drv", EntryPoint = "AddMonitorA", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        private static extern bool AddMonitor([MarshalAs(UnmanagedType.LPStr)] string Name, Int32 Level, [In, MarshalAs(UnmanagedType.LPStruct)] MONITOR_INFO_2 mi2);

public static int MonitorInstaller(out string errorMessage)
    {
        int ret = 0;
        byte* pMonitor = GetBytePtr(Globals.PortMonitorName);
        errorMessage = null;

        ret = IsMonitorInstalled(pMonitor, false);
        Loger.WriteLog("IsMonitorInstaled: {0}", ret);
        if (ret != 0)
        {
            Loger.WriteLog("Test_point_01");
            errorMessage = "Monitor is already installed.";
            return 1;
        }

        bool delRes = DeleteMonitor(null, null, Globals.PortMonitorName);
        Loger.WriteLog("Delete result: {0}", delRes);


        MONITOR_INFO_2 mi2 = new MONITOR_INFO_2();

        mi2.pName = Globals.PortMonitorName;
        mi2.pEnvironment = "Windows NT x86";
        mi2.pDLLName = Globals.MonitorFile;

        Loger.WriteLog("mi2 data:");
        Loger.WriteLog("pName: {0}", mi2.pName);
        Loger.WriteLog("pDLLName: {0}", mi2.pDLLName);

        bool bRet = AddMonitor(null, 2, mi2);
        Loger.WriteLog("AddMonitor result: {0}", bRet);
        if (!bRet)
        {
            int errorCode = Marshal.GetLastWin32Error();
            Loger.WriteLog("AddMonitor erro: {0}", errorCode);
            errorMessage = new Win32Exception(errorCode).Message;
            return errorCode;
        }

        return 0;
    }

AddMonitor 函数总是设置 183 错误代码:当文件已存在时无法创建文件。但是没有这样的监视器。给我任何想法。PS 应用程序以管理员帐户运行,测试虚拟机上没有防病毒软件,Windows 防火墙已关闭。

4

0 回答 0