[英]RPC_E_SERVERFAULT when calling Visual FoxPro COM object (out-of-process) from .Net?
我需要從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()
為了消除它與COM可執行文件相關的可能性,我使用以下代碼創建了一個MT dll:
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”的“ Throwd”框。 工具+附加到流程。
當然,默認處理無濟於事。 您可以在Vista及更高版本中使用IGlobalOptions界面進行修復。 使用COMGLB_EXCEPTION_HANDLING強制服務器崩潰。 這需要在服務器代碼中完成,我想這在Foxpro中應該是一個問題。
這是由於DEP(數據執行保護)(感謝pst)所致,這似乎是Foxpro代碼的問題。
您可以通過GUI將您的應用程序手動添加到DEP例外列表中,但是我需要一種編程方式來做到這一點:
使用注冊表項將exe添加到排除列表中:
HKLM \\ SOFTWARE \\ Microsoft \\ Windows NT \\ CurrentVersion \\ AppCompatFlags \\ Layers
“fullPathToExe”= “DisableNXShowUI”
如果DEP處於活動狀態,則必須重新引導以使其起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.