【问题标题】:System.AccessViolationException error when accessing COM DLL written in Visual FoxPro访问用 Visual FoxPro 编写的 COM DLL 时出现 System.AccessViolationException 错误
【发布时间】:2026-01-12 09:20:15
【问题描述】:

我有一个 C# WinForms .NET 3.5 应用程序,我需要调用一个用 Visual FoxPro 7 编写的 COM DLL。我已将 COM 对象添加到 C# 项目中,并且可以

在对象浏览器中查看对象及其成员。

但是,当我尝试从 COM 对象调用任何方法或访问任何属性时,会抛出以下异常:

System.AccessViolationException 未处理 Message=试图读取或写入受保护的内存。这通常表明其他内存已损坏。 来源=Interop.emscosting 堆栈跟踪: 在 emscosting.ComBaseClass.ShellExecute(字符串 tcItem,字符串 tcAction) ...

以下是我正在使用的一些示例 C# 代码:

emscosting.ComBaseClass com = new emscosting.ComBaseClass(); com.ShellExecute(文件,“真”);

此外,当我尝试在调试器监视窗口中访问 COM 对象的任何公共属性时,我会在“值”列中看到以下内容: “com.DatabaseUserName”引发了“System.AccessViolationException”类型的异常

我已经在使用这个 COM 对象,在其他 VFP 应用程序、Office 应用程序和 Javascript/Classic ASP 中没有任何问题。

有人可以帮我解决这个问题吗?

我仍然有 COM 的原始 VFP 源代码,下面是我声明公共属性的代码的 sn-p,但是我正在尝试

尽可能避免重写 COM DLL!

#If BUILD_AS_COM_OBJECT
Define Class emsCosting As Combase OlePublic
#Else
Define Class emsCosting As Combase
#Endif

CostingsPath = COSTINGS_PATH
Dimension CostingsPath_COMATTRIB[5]
CostingsPath_COMATTRIB[1] = COMATTRIB_NONE
CostingsPath_COMATTRIB[2] = "Contains the path of where the costings get saved" && HelpString
CostingsPath_COMATTRIB[3] = "CostingsPath" && Capitalisation
CostingsPath_COMATTRIB[4] = "String" && Property Type
CostingsPath_COMATTRIB[5] = 0 && Number of parameters

CostingsMaster = COSTINGS_MASTER
Dimension CostingsMaster_COMATTRIB[5]
CostingsMaster_COMATTRIB[1] = COMATTRIB_NONE
CostingsMaster_COMATTRIB[2] = "Contains the filename of the costings master (usually costings.xls)" && HelpString
CostingsMaster_COMATTRIB[3] = "CostingsMaster" && Capitalisation
CostingsMaster_COMATTRIB[4] = "String" && Property Type
CostingsMaster_COMATTRIB[5] = 0 && Number of parameters

Function SetCostingsPath(tcPath As String) As VOID HelpString "This is a test function"
    CostingsPath = tcPath
EndFunc

Function TestFunctionNoParam() AS String HelpString "This is a helpstring"
    Return "\\TEST\ContractReviewCostings\"
EndFunc

......

注意:上面定义的两个测试函数都失败并出现异常 System.AccessViolationException

【问题讨论】:

    标签: c# com foxpro visual-foxpro


    【解决方案1】:

    如果您有 x64 系统,您必须为 x86 编译您的 .Net 应用程序。 使用“Any CPU”或“x64”编译时,无法访问x32 FoxPro-DLL。

    【讨论】: