[英]System.InvalidCastException trying to build a COM client and a COM server in C#
[英]InvalidCastException with COM client and server in C#
我知道有關此主題的問題很多,但我相信我在過去10天內都經歷了所有這些問題,但我找不到我所遭受的嚴重錯誤的解決方案。
我在C#中有一個COM服務器dll,在C#中有一個COM客戶端。 Windows 7中的全部。我收到InvalidCastException,但無法解決問題。 我開始懷疑是否可以在C#中創建COM服務器。
我在實例化COM對象時遇到了該異常:
Test.MyImplementation mi = new Test.MyImplementation();
未處理System.InvalidCastException HResult = -2147467262消息=無法將類型為“ MyTest.MyImplementation”的對象強制轉換為類型“ Test.MyImplementation”。 Source = ConsoleApplication3 StackTrace:位於c:\\ Users \\ rkohn \\ Documents \\ Visual Studio 2013 \\ Projects \\ ConsoleApplication3 \\ ConsoleApplication3 \\ Program.cs:ConsoleAppCOM.Program.Main(String [] args),位於System.AppDomain._nExecuteAssembly( RuntimeAssembly程序集,位於Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly(),位於System.Threading.ExecutionContext.RunInternal(ExecutionContext執行上下文,ContextCallback回調,對象狀態,布爾狀態,位於System.Threading.ExecutionContext.Run()處。在System.Threading.ExecutionContext.Run處的ExecutionContext executeContext,ContextCallback回調,對象狀態,布爾值保持SyncCtx)在System.Threading.ThreadHelper.ThreadStart()處的ExecutionContext executeContext,ContextCallback回調,對象狀態)InnerException:
這是COM服務器代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace MyTest
{
[ComVisible(true)]
[Guid("DBE0E8C4-DABA-41F3-B6A4-CAFE353D3D16")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IPimcManager
{
void GetTabletCount(out UInt32 count);
}
[ComVisible(true)]
[Guid("C6659361-DABA-4746-931C-CAFE4B146690")]
[ProgId("FakeServer.MyImplementation")]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(IPimcManager))] //This to explicitly establish which is the default interface
public class MyImplementation : IPimcManager
{
public MyImplementation() { }
~MyImplementation() { }
public void GetTabletCount(out UInt32 count)
{
Console.WriteLine("GetTabletCount called!");
count = 1;
}
}
}
這是客戶端代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Test
{
[ComImport]
[Guid(PimcConstants.IPimcManagerIID)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IPimcManager
{
void GetTabletCount(out UInt32 count);
}
[ComImport]
[Guid(PimcConstants.PimcManagerCLSID)]
class MyImplementation
{
}
// void GetTabletCount(out UInt32 count);
//void GetTablet(UInt32 tablet, out IPimcTablet IPimcTablet);
internal static class PimcConstants
{
//internal const string PimcManagerCLSID = "e23b1ced-5e47-4fdb-af66-b20370261b5e";
internal const string PimcManagerCLSID = "C6659361-DABA-4746-931C-CAFE4B146690";
internal const string IPimcManagerIID = "DBE0E8C4-DABA-41F3-B6A4-CAFE353D3D16";
//internal const string PimcManagerCLSID = "c6659361-daba-4746-931c-cafe4b146690";
//internal const string IPimcManagerIID = "dbe0e8c4-daba-41f3-b6a4-cafe353d3d16";
//internal const string IPimcManagerIID = "af44bf80-36dd-4118-b4cf-8b1e3f4fb9ce";
}
}
namespace ConsoleAppCOM
{
class Program
{
[STAThread]
static void Main(string[] args)
{
Test.MyImplementation mi = new Test.MyImplementation();
Test.IPimcManager pimcManager = ((Test.IPimcManager)mi);
uint cTablets = 0;
pimcManager.GetTabletCount(out cTablets);
System.Console.WriteLine(DateTime.Now + "-VALUE OBTAINED from PimcManager.GetTabletCount: " + cTablets);
//Thread.Sleep(5);
System.Console.ReadLine();
}
}
}
我已經嘗試過STAThread,在服務器項目屬性中選中了“為COM Interop注冊”,並檢查了客戶端和服務器都針對x64 ...
我已經用C ++創建了一個COM Server,並且同一客戶端可以與C ++ COM Server完美配合。 沒有InvalidCastException。
這是從C#FakeServer的類型庫獲得的IDL:
// Generated .IDL file (by the OLE/COM Object Viewer)
//
// typelib filename: FakeServer.tlb
[
uuid(A3CFF4E2-8724-461F-AFD4-D74583E89513),
version(1.0),
custom(90883F05-3D28-11D2-8F17-00A0C9A6186D, "FakeServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
]
library FakeServer
{
// TLib : // TLib : mscorlib.dll : {BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}
importlib("mscorlib.tlb");
// TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
importlib("stdole2.tlb");
// Forward declare all types defined in this typelib
interface IPimcManager;
[
odl,
uuid(DBE0E8C4-DABA-41F3-B6A4-CAFE353D3D16),
version(1.0),
oleautomation,
custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "MyTest.IPimcManager")
]
interface IPimcManager : IUnknown {
HRESULT _stdcall GetTabletCount([out] unsigned long* count);
};
[
uuid(C6659361-DABA-4746-931C-CAFE4B146690),
version(1.0),
custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "MyTest.MyImplementation")
]
coclass MyImplementation {
interface _Object;
[default] interface IPimcManager;
};
};
這是從C ++ COM Server的類型庫中獲得的IDL(請不要考慮添加到接口中的Name方法,我在測試中就這樣做了):
// Generated .IDL file (by the OLE/COM Object Viewer)
//
// typelib filename: simplecomserver.tlb
[
uuid(6F818C55-E6AD-488B-9EB6-511C0CCC0612),
version(1.0),
custom(DE77BA64-517C-11D1-A2DA-0000F8773CE9, 134218331),
custom(DE77BA63-517C-11D1-A2DA-0000F8773CE9, 1413900762),
custom(DE77BA65-517C-11D1-A2DA-0000F8773CE9, "Created by MIDL version 8.00.0603 at Tue Oct 21 11:12:41 2014
")
]
library LibCOMServer
{
// TLib : // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
importlib("stdole2.tlb");
// Forward declare all types defined in this typelib
interface ICOMServer;
[
odl,
uuid(7F24AABF-C822-4C18-9432-21433208F4DC),
oleautomation
]
interface ICOMServer : IUnknown {
HRESULT _stdcall Name([out, retval] BSTR* objectname);
HRESULT _stdcall GetTabletCount([out] unsigned long* pcTablets);
};
[
uuid(6AE24C34-1466-482E-9407-90B98798A712),
helpstring("COMServer object")
]
coclass CoCOMServer {
[default] interface ICOMServer;
};
};
與從接口_Object繼承的C#協類是否有任何關系?
coclass MyImplementation {
interface _Object;
[default] interface IPimcManager;
};
任何幫助表示贊賞。
也許您的MyImplementation
需要實現IPimcManager
(就像在服務器代碼中一樣)。 嘗試:
class MyImplementation : IPimcManager
{
public void GetTabletCount(out UInt32 count)
{
// implement your code here
}
}
希望這可以幫助。
請嘗試以下操作:
在您的客戶端類聲明上,而不是
[ComImport]
[Guid(PimcConstants.PimcManagerCLSID)]
class MyImplementation
{
}
將其更改為接口並使用父接口GUID:
[ComImport]
[Guid(PimcConstants.IPimcManagerIID)]
interface MyImplementation:IPimcManager
{
}
希望能幫助到你
在.NET客戶端中聲明共同創建(可實例化)的COM類的通常模式不僅要求您聲明類(例如下面的FooClass
),而且還需要類接口( Foo
),這是您將與new
一起使用的接口( new Foo()
)。 例如:
[ComImport]
[Guid(IIDs.IFoo)] // IIDs.IFoo is a string constant containing the interface's IID
[CoClass(typeof(FooClass))]
public interface Foo : IFoo { }
[ComImport]
[Guid(CLSIDs.Foo)] // CLSIDs.Foo is a string constant containing the co-class' CLSID
[ComDefaultInterface(typeof(IFoo))]
public class FooClass { }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.