Solved

System.AccessViolationException exception when accessing COM DLL written in VFP

Posted on 2013-06-13
10
1,212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

626 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