I need to call a Foxpro (VFP 8) COM object from C#
The vfp code looks like this:
Define Class x2 As session OlePublic
Function testInteropServer()
ENDFUNC
Enddefine
The C# code looks like this:
[TestFixture]
public class TestFixture
{
[Test]
public void TestXtmBase()
{
xtmbase.x2 xtmBaseX2 = new xtmbase.x2();
xtmBaseX2.testInteropServer();
}
}
The COM server is compiled as an executable (not a dll). I can load it from Fox. It loads the object into .Net but I can’t seem to call any of it’s methods. I am importing the COM reference through the GUI in VS 2005. It recognizes all the methods and properties of the object. I just can’t access them.
I am getting the following error:
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
I then created a console app:
class Program
{
static void Main(string[] args)
{
mtx2.mtx2 mtx2 = new mtx2.mtx2();
mtx2.testInteropServer();
Console.WriteLine("Done");
}
}
and it fails:
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()
Any ideas why?
It was due to DEP (Data Execution Prevention) (thanks pst) which seems to be a problem with Foxpro code.
You can manually add your app to the DEP exception list via the GUI, but I needed a programmatic way of doing it so:
Add the exe to the exclusion list using a registry key:
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
“fullPathToExe”=”DisableNXShowUI”
If DEP is active you have to reboot to make it work.