我需要从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中,在“ Throwd”框中选中“ Win32 Exceptions”。工具+附加到流程。

当然,默认处理无济于事。您可以在Vista及更高版本中使用IGlobalOptions interface修复此问题。使用COMGLB_EXCEPTION_HANDLING强制服务器崩溃。这需要在服务器代码中完成,我想这在Foxpro中应该是一个问题。

10-07 19:01
查看更多