Solved

System.AccessViolationException exception when accessing COM DLL written in VFP

Posted on 2013-06-13
10
1,138 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
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: 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
 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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…

777 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