Solved

System.AccessViolationException exception when accessing COM DLL written in VFP

Posted on 2013-06-13
10
1,131 Views
Last Modified: 2013-06-15
Hi,

I have a C# WinForms .NET 3.5 application that I need to call a COM DLL written in Visual FoxPro 7. I have added the COM object to the C# project fine, and can

view the objects and its members in the object browser fine.

However when I attempt to call any methods or access any properties from the COM object I get the following exception thrown :

System.AccessViolationException was unhandled
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=Interop.emscosting
  StackTrace:
       at emscosting.ComBaseClass.ShellExecute(String tcItem, String tcAction)
...

Below is some example C# code I am using :

emscosting.ComBaseClass com = new emscosting.ComBaseClass();
com.ShellExecute(file, "TRUE"); <--- The exception is thrown here

Also when I try and access any of the public properties of the COM object in the debugger watch window I get the following appear in the Value column :
'com.DatabaseUserName' threw an exception of type 'System.AccessViolationException'

I am already using this COM object without any issues from other VFP applications, Office applications, and in Javascript/Classic ASP.

Can someone help me resolve this issue please ?

I do have the original VFP source code for the COM still and below is a snippet of the code where I am declaring the public properties, however I am trying to

avoid rewriting the COM DLL if possible!

#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

......

NOTE: Both the tow test functions defined above fail with the exception System.AccessViolationException
0
Comment
Question by:chriscboy
10 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39244127
is it possible u need to initialize the COM object before starting using it?
0
 

Author Comment

by:chriscboy
ID: 39244141
I am already doing that by the following line of code :
emscosting.ComBaseClass com = new emscosting.ComBaseClass();
0
 
LVL 9

Expert Comment

by:sarabhai
ID: 39244145
Add a line to your application's config file under the configuration/runtime element:

<legacyCorruptedStateExceptionsPolicy enabled="true|false"/>

and try out.
0
 

Author Comment

by:chriscboy
ID: 39244160
According to this article http://msdn.microsoft.com/en-us/library/dd638517.aspx that configuration setting is only for .NET 4 applications, mine is compiled to 3.5.
0
 
LVL 41

Expert Comment

by:pcelba
ID: 39244550
You may read following articles describing .NET - VFP interoperability:
http://www.west-wind.com/presentations/vfpdotnetinterop/vfpdotnetinterop.htm#_COM_Interoperability
http://www.markusegger.com/articles/Article.aspx?quickid=0404072
http://www.craigberntson.com/docs/formsinterop.pdf

All above articles are written for older VS versions but I hope they'll give you sufficient starting point. OTOH, your error means a security issue which could be new .NET 4 enhancement and you have to find a way how to override it...

Of course, the ShellExecute is a candidate #1 to apply some security restrictions because it is very dangerous command (if it executes external code).
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:chriscboy
ID: 39245245
Thanks for the links, I have read some of this and have now written a small test COM DLL in VisualFoxPro which just has a single property and a function.

I have called this from my C# app and  still get the same error, so I am wondering if it is some sort of .NET configuration. I am not an expert on such things though!
0
 
LVL 41

Expert Comment

by:pcelba
ID: 39245325
I also don't know more details about COM DLL security... VS 2012 is just on its way to my home PC...

One more question: Do you use 32 bit or 64 bit compilation? 32 bit is better in this case maybe.
0
 
LVL 41

Accepted Solution

by:
pcelba earned 500 total points
ID: 39245394
0
 

Author Comment

by:chriscboy
ID: 39247314
That post did the trick!!! Thank you I am so happy now! I did the following things to get mine working :

1) Right click on project in Visual Studio
2) Properties
3) Build Events
4) Edit Post Build ...
 
REM Mark project as DEP Noncompliant
call "$(DevEnvDir)..\..\VC\bin\vcvars32.bat"
call "$(DevEnvDir)..\..\VC\bin\editbin.exe" /NXCOMPAT:NO "$(TargetPath)"

THEN

#1: Turn off "Enable the Visual Studio hosting process" under the "Debug" tab. This is because VS makes it's own executable, filename appended with .vshost.exe, which doesn't seem to have the NXCOMPAT bit set, or perhaps it runs as a subprocess of Visual Studio which uses DEP.
0
 
LVL 41

Expert Comment

by:pcelba
ID: 39249704
Thanks for the final notes! They will help to many other developers.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Visual FoxPro (short VFP) is a programming language with it’s own IDE and database, ranking somewhat between Access and VB.NET + SQL Server (Express). Product Description: http://msdn.microsoft.com/en-us/vfoxpro/default.aspx (http://msd…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now