Solved

System.AccessViolationException exception when accessing COM DLL written in VFP

Posted on 2013-06-13
10
1,108 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
Comment Utility
is it possible u need to initialize the COM object before starting using it?
0
 

Author Comment

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

Expert Comment

by:sarabhai
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:chriscboy
Comment Utility
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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:chriscboy
Comment Utility
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
Comment Utility
Thanks for the final notes! They will help to many other developers.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
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: …

762 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

15 Experts available now in Live!

Get 1:1 Help Now