Solved

System.AccessViolationException exception when accessing COM DLL written in VFP

Posted on 2013-06-13
10
1,155 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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 42

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
 

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 42

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 42

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 42

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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