Mor*_*app 5 c# windows com registry windows-10
我正在尝试创建一组我可以安排在将来运行的 C# 类。目的是通过我的代码的其他部分以编程方式安排这些。
这是我试图通过 COM 调用的当前类。
using System;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Win32.TaskScheduler;
namespace SchedulableTasks
{
[Guid("F5CAE94C-BCC7-4304-BEFB-FE1E5D56309A")]
public class TaskRegistry : ITaskHandler
{
private ITaskHandler _taskHandler;
public void Start(object pHandlerServices, string data)
{
var arguments = data.Split('|');
var taskTypeName = arguments.FirstOrDefault();
var taskArguments = arguments.Skip(1).FirstOrDefault();
var taskType = Type.GetType(taskTypeName);
_taskHandler = (ITaskHandler) Activator.CreateInstance(taskType);
_taskHandler.Start(pHandlerServices, taskArguments);
}
public void Stop(out int pRetCode)
{
var retCode = 1;
_taskHandler?.Stop(out retCode);
pRetCode = retCode;
}
public void Pause()
{
_taskHandler.Pause();
}
public void Resume()
{
_taskHandler.Resume();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试安排任务的方式
using System;
using Microsoft.Win32.TaskScheduler;
using Newtonsoft.Json;
using Action = Microsoft.Win32.TaskScheduler.Action;
namespace MyProject.Service
{
public static class SchedulingService
{
public enum ScheduledTask
{
BillingQuery
}
private static readonly Guid RegistryGUI = new Guid("F5CAE94C-BCC7-4304-BEFB-FE1E5D56309A");
public static void ScheduleAction(string name, string description, Trigger trigger, Action action)
{
using (var taskService = new TaskService())
{
var task = taskService.NewTask();
task.RegistrationInfo.Description = description;
task.Triggers.Add(trigger);
task.Actions.Add(action);
taskService.RootFolder.RegisterTaskDefinition(name, task);
}
}
public static Action CreateCSharpAction(ScheduledTask task, object data)
{
var taskData = $"{task.ToString()}|{JsonConvert.SerializeObject(data)}";
return new ComHandlerAction(RegistryGUI, taskData);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Microsoft.Win32.TaskScheduler是这个库的2.7.2 版
我可以毫无问题地创建计划任务,但是当我尝试运行它时,我得到一个Class not registered (0x80040154).
在我的 .csproj 中,我将程序集注册为 COM 对象。对于PlatformTarget属性,我已经尝试了所有的AnyCPU,x86以及x64与相应regasm.exe。
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<RegisterForComInterop>true</RegisterForComInterop>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
我正在使用以下构建后事件来注册 .dll。
powershell C:\Windows\Microsoft.Net\Framework64\v4*\RegAsm.exe /codebase '$(ProjectDir)$(OutDir)SchedulableTasks.dll'
Run Code Online (Sandbox Code Playgroud)
Procmon.exe 似乎报告相同。我担心的是“NAME NOT FOUND” \TreatAs,\InprocHandler并且\InprocServer32\InprocServer32

我想我看到了你的问题:
<PropertyGroup Condition=" '$(配置)|$(平台)' == '调试| AnyCPU ' ">
尝试以 x86 为目标。请参阅此答案以获取深入描述:How to Solution COM Exception Class not Registration (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))?
编辑:
我无法重现它,我添加了一些内容:
[ComVisible(true)]| 归档时间: |
|
| 查看次数: |
717 次 |
| 最近记录: |