C# COM无法启动进程外.Net服务器编译为AnyCPU
我正在尝试让COM启动进程外.NET COM服务器。如果服务器进程是用x64编译的,那么它可以工作,但是如果我使用AnyCPU(这是我想要的),那么它会挂起一段时间,最终以0x80005失败(CO_E_server_EXEC_FAILURE)。我怎样才能让它工作C# COM无法启动进程外.Net服务器编译为AnyCPU,c#,.net,windows,com,C#,.net,Windows,Com,我正在尝试让COM启动进程外.NET COM服务器。如果服务器进程是用x64编译的,那么它可以工作,但是如果我使用AnyCPU(这是我想要的),那么它会挂起一段时间,最终以0x80005失败(CO_E_server_EXEC_FAILURE)。我怎样才能让它工作 我在64位计算机上运行:Windows7和VisualStudio2008SP1 我可以在任务管理器中看到它确实启动了我的服务器。所以我想问题在于COM和服务器之间的通信(类注册) 我的测试客户机应用程序是用C#编写的,但不管它是为x
- 我在64位计算机上运行:Windows7和VisualStudio2008SP1
- 我可以在任务管理器中看到它确实启动了我的服务器。所以我想问题在于COM和服务器之间的通信(类注册)
- 我的测试客户机应用程序是用C#编写的,但不管它是为x86还是x64编译的。这个问题也发生在32位C++中的一些东西。
- 如果我使用x64重新构建服务器并运行它,然后作为AnyCPU重新构建,那么COM可以启动它。重新启动会使我回到原来的状态。也许COM事先不知道要使用什么比特率,而先前的执行会有所帮助
- 我找到并尝试将CLSCTX_ACTIVATE_64位_服务器传递给CoCreateInstance(),但这会在前面触发一个故障:0x80040154(REGDB_E_CLASSNOTREG)。我的COM注册有问题吗?你可以在下面看到它非常简单。以64位运行时会发生注册,当客户端为64位时会发生问题,因此不应涉及Wow6432Node
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("ole32.dll", ExactSpelling = true, PreserveSig = false)]
[return: MarshalAs(UnmanagedType.Interface)]
static extern object CoCreateInstance(
[In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
[MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter,
CLSCTX dwClsContext,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid riid);
[Flags]
enum CLSCTX : uint
{
CLSCTX_LOCAL_SERVER = 0x4,
CLSCTX_ACTIVATE_64_BIT_SERVER = 0x80000,
}
private void Form1_Load(object sender, EventArgs e)
{
IPersistFile pf = (IPersistFile)CoCreateInstance(
new Guid("1984D314-FC8D-44bc-9146-8A13500666A6"),
null,
CLSCTX.CLSCTX_LOCAL_SERVER,
new Guid("0000010b-0000-0000-C000-000000000046")); // IPersistFile
pf.Load("c:\\bozo", 0);
}
}
这是服务器:
static class Program
{
[STAThread]
static void Main()
{
if (Environment.CommandLine.Contains("/reg")) {
RegistryKey cls = Registry.LocalMachine.CreateSubKey(String.Format(
"SOFTWARE\\Classes\\CLSID\\{0}", PersistFile.ClassID.ToString("B")));
cls.SetValue("InprocHandler32", "Ole32.dll");
RegistryKey ls32 = cls.CreateSubKey("LocalServer32");
ls32.SetValue(null, '"' + Application.ExecutablePath + '"');
ls32.SetValue("ServerExecutable", Application.ExecutablePath);
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
RegistrationServices reg = new RegistrationServices();
reg.RegisterTypeForComClients(
typeof(PersistFile),
RegistrationClassContext.LocalServer,
RegistrationConnectionType.MultipleUse);
Application.Run(new Form1());
}
}
[ComVisible(true),
Guid("1984D314-FC8D-44bc-9146-8A13500666A6"),
ClassInterface(ClassInterfaceType.None)]
public class PersistFile : IPersistFile
{
public static Guid ClassID
{
get
{
GuidAttribute a = (GuidAttribute)typeof(PersistFile).GetCustomAttributes(typeof(GuidAttribute), false)[0];
return new Guid(a.Value);
}
}
#region IPersistFile
public void GetClassID(out Guid pClassID)
{
MessageBox.Show("GetClassID");
pClassID = ClassID;
}
public int IsDirty()
{
MessageBox.Show("IsDirty");
return 1;
}
public void Load(string pszFileName, int dwMode)
{
MessageBox.Show(String.Format("Load {0}", pszFileName));
}
public void Save(string pszFileName, bool fRemember)
{
MessageBox.Show("Save");
throw new NotImplementedException();
}
public void SaveCompleted(string pszFileName)
{
MessageBox.Show("SaveCompleted");
throw new NotImplementedException();
}
public void GetCurFile(out string ppszFileName)
{
MessageBox.Show("GetCurFile");
throw new NotImplementedException();
}
#endregion
}
尝试使用该类注册com程序集。它还将选择正确的注册表路径并执行其他一些操作
例如:
Assembly currentAssembly = Assembly.GetExecutingAssembly();
System.Runtime.InteropServices.RegistrationServices regAsm = new System.Runtime.InteropServices.RegistrationServices();
bool isRegistered = regAsm.RegisterAssembly(currentAssembly, System.Runtime.InteropServices.AssemblyRegistrationFlags.SetCodeBase);
另外,我认为,根据.NET com服务器,.NET客户端程序集有一些问题,但我找不到任何资源
希望,它会有帮助…我想问题出在运行时。我创建了一个COM服务器,它使用C++库进行注册(注册完成得很好)。我在从.NET(CS)切换到任何CPU时遇到问题 架构:
- C++库接口COM(在x64和x86平台上构建)
- .NET应用程序(CS)-COM客户端或COM服务器
- x86在classes\wow6432节点中注册类
- x64和AnyCPU在类中注册类(在x64 windows机器上,键完全相同;我打赌x86和AnyCPU在x86机器上注册相同的键)
还有谁在这件事上取得了一些进展?我们应该联系微软吗?听起来像是AnyCPU世界中的某些东西默认使用x86,而且只需要一个流氓就可以搞定整个事情。我认为在激活调用中传递CLSCTX\u ACTIVATE\u 64位\u服务器将不起作用,除非您在注册调用中将其传递给RegisterTypeForComClient。我怀疑构建为x64并运行,然后为x86重建,之所以有效,是因为您注册了类对象,但没有注销它,因此注册会保留在全局类对象表中。我发现AnyCPU是一个难题,我只在100%.NET项目中使用它(即几乎从未使用过!)。感谢@Ciaran,也许这归结为COM不够聪明,无法智能地解释CLR.exe头中的AnyCPU标志。我尝试将CLSCTX_ACTIVATE_64位_服务器传递给RegisterTypeForComClient,但它抛出了E_INVALIDARG。我并不感到惊讶,因为当我调用时,我已经以64位运行了,函数可以很容易地确定这一点,那又怎样呢