我需要从 C# 调用 Foxpro (VFP 8) COM 对象
vfp 代码如下所示:
Define Class x2 As session OlePublic
Function testInteropServer()
ENDFUNC
Enddefine
C# 代码如下所示:
[TestFixture]
public class TestFixture
{
[Test]
public void TestXtmBase()
{
xtmbase.x2 xtmBaseX2 = new xtmbase.x2();
xtmBaseX2.testInteropServer();
}
}
COM 服务器被编译为可执行文件(不是 dll)。我可以从 Fox 加载它。它将对象加载到 .Net 中,但我似乎无法调用它的任何方法。我正在通过 VS 2005 中的 GUI 导入 COM 引用。它识别对象的所有方法和属性。我只是无法访问它们。 我收到以下错误:
Test 'TestProject/TestFixture/TestXtmBase' failed: Execute System.Runtime.InteropServices.COMException: The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)) ErrorCode: -2147417851 at xtmbase.x2Class.testInteropServer()
To remove the possibility of it being related to a COM executable I created a MT dll with the following code:
Define Class MTx2 As session OlePublic
Function testInteropServer()
ENDFUNC
Enddefine
然后我创建了一个控制台应用程序:
class Program
{
static void Main(string[] args)
{
mtx2.mtx2 mtx2 = new mtx2.mtx2();
mtx2.testInteropServer();
Console.WriteLine("Done");
}
}
失败了:
System.Runtime.InteropServices.COMException was unhandled
Message="The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))"
Source="Interop.mtx2"
ErrorCode=-2147417851
StackTrace:
at mtx2.mtx2Class.testInteropServer()
at ConsoleTest.Program.Main(String[] args) in E:\development\iisx2\ConsoleTest\Program.cs:line 12
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
有什么想法吗?
最佳答案
你在错误的过程中寻找问题。是 COM 服务器因异常而失败。 RPC_E_SERVERFAULT 错误只是一个通知,一个非常无用的错误,因为有关异常的所有信息都丢失了。
在运行语句之前将调试器附加到服务器进程,并让它在出现所有异常时停止,以便您可以对此进行诊断。在使用 Debug + Exceptions 完成的 VS 中,勾选“Win32 Exceptions”的 Thrown 框。工具 + 附加到流程。
默认处理当然毫无帮助。您可以在 Vista 及更高版本中使用 IGlobalOptions interface 修复此问题.使用 COMGLB_EXCEPTION_HANDLING 强制服务器崩溃。这需要在服务器代码中完成,我想这在 Foxpro 中应该是个问题。
关于c# - 从 .Net 调用 Visual FoxPro COM 对象(进程外)时出现 RPC_E_SERVERFAULT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8697282/