473,837 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.AccessVi olationExceptio n

Hi,
I am getting this exceptions when I am trying to access the Frotran dll. Here
is sample code for that.
using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Runtime. InteropServices ;

namespace TestApplication FracEngine
{
class InterfaceClass
{
[DllImport("fort ran.dll", EntryPoint = "FR_MAIN_DL L",
CallingConventi on = CallingConventi on.StdCall)]
public static extern void FR_MAIN_DLL(int errorNumber, string
errorMessage, int length);
public void CallFortran()
{
FR_MAIN_DLL(1, "", 120);
}
}
}
}
I have copied the all the dependent dlls in the debug directory. When I run
this program I am getting the following exception which I couldn't get rid of.
An unhandled exception of type 'System.AccessV iolationExcepti on' occurred in
TestApplication FracEngine.exe

Additional information: Attempted to read or write protected memory. This is
often an indication that other memory is corrupt.

Please help me out in solving this issue.

Sep 8 '08 #1
5 15024
Hi,

it is a "clasic" memory boundary violation.
Try to make the:

"string errorMessage"

a

"StringBuil der errorMessage"

and supply in the Constructor of the StringBuilder
its capacity e.g. 120 for 120 chars. Try to hold it
big enough. You Pinvoke should look
like this:

[DllImport("fort ran.dll", EntryPoint = "FR_MAIN_DL L",
CallingConventi on = CallingConventi on.StdCall)]
public static extern void FR_MAIN_DLL(int errorNumber, StringBuilder
errorMessage, int length);
Your call should look like this:

StringBuilder sb = new StringBuilder(5 12);
int error_code = 1;

FR_MAIN_DLL(err or_code,sb,sb.C apacity);

then sb will hold the translated error message. You
can get the string by doing a sb.ToString(); But this
onyl works if your function declaration is valid and
calling convention etc is also valid. My pinvoke call
is basing on your signature. The most obvious fault
was the missing StringBuilder, the other data, i dint
know since i dont know this functions signature. Try
it and let me know, or show me the orignal signature,...

Hope this helps,...

Regards

Kerem
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.codeplex.com/restarts
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
"SSekar" <u46044@uweschr ieb im Newsbeitrag news:89de08c96f cea@uwe...
Hi,
I am getting this exceptions when I am trying to access the Frotran dll.
Here
is sample code for that.
using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Runtime. InteropServices ;

namespace TestApplication FracEngine
{
class InterfaceClass
{
[DllImport("fort ran.dll", EntryPoint = "FR_MAIN_DL L",
CallingConventi on = CallingConventi on.StdCall)]
public static extern void FR_MAIN_DLL(int errorNumber, string
errorMessage, int length);
public void CallFortran()
{
FR_MAIN_DLL(1, "", 120);
}
}
}
}
I have copied the all the dependent dlls in the debug directory. When I
run
this program I am getting the following exception which I couldn't get rid
of.
An unhandled exception of type 'System.AccessV iolationExcepti on' occurred
in
TestApplication FracEngine.exe

Additional information: Attempted to read or write protected memory. This
is
often an indication that other memory is corrupt.

Please help me out in solving this issue.
Sep 8 '08 #2
Hi Kerem,
Thanks for your reply. I tried as per your suggestion but it doesn't work for
me. The same function is being called in C++ code. I am trying the same here
in C#.
Please find the following declaration in C++.

DllExport void __stdcall PL_MAIN_DLL(int *driverErrorNum ber,
char *errorMessage,
unsigned int *lengthArg);
Kerem Gümrükcü wrote:
>Hi,

it is a "clasic" memory boundary violation.
Try to make the:

"string errorMessage"

a

"StringBuild er errorMessage"

and supply in the Constructor of the StringBuilder
its capacity e.g. 120 for 120 chars. Try to hold it
big enough. You Pinvoke should look
like this:

[DllImport("fort ran.dll", EntryPoint = "FR_MAIN_DL L",
CallingConvent ion = CallingConventi on.StdCall)]
public static extern void FR_MAIN_DLL(int errorNumber, StringBuilder
errorMessage , int length);

Your call should look like this:

StringBuilde r sb = new StringBuilder(5 12);
int error_code = 1;

FR_MAIN_DLL(er ror_code,sb,sb. Capacity);

then sb will hold the translated error message. You
can get the string by doing a sb.ToString(); But this
onyl works if your function declaration is valid and
calling convention etc is also valid. My pinvoke call
is basing on your signature. The most obvious fault
was the missing StringBuilder, the other data, i dint
know since i dont know this functions signature. Try
it and let me know, or show me the orignal signature,...

Hope this helps,...

Regards

Kerem
>Hi,
I am getting this exceptions when I am trying to access the Frotran dll.
[quoted text clipped - 35 lines]
>>
Please help me out in solving this issue.
--
Message posted via http://www.dotnetmonster.com

Sep 8 '08 #3
Hi,,

the point is. What is a IN parameter and what
is a OUT parameter? IN means that we have to
insert the data nto the call and OUT means
that the application will fill the data into our
varibale. This is another suggestion i can make:

[DllImport("fort ran.dll",
EntryPoint = "FR_MAIN_DL L",
CallingConventi on = CallingConventi on.Winapi)]
public static extern void FR_MAIN_DLL(ref System.Int32 errorNumber,
StringBuilder errorMessage,
ref System.UInt32 length);

Please tell me what exactly hese parameters mean and which
one must be given to the function as in and wich one does the
application write into our variable. Also would be important
to know whether this is a UNCIODE or ANSI call, i mean the
string part, but it seems to be a ANSI due to char pointer...

Please try th Signature and tell me what exactly the parameters
are on which we are working on...
Regards

Kerem
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.codeplex.com/restarts
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
"SSekar via DotNetMonster.c om" <u46044@uweschr ieb im Newsbeitrag
news:89e3394d79 0a7@uwe...
Hi Kerem,
Thanks for your reply. I tried as per your suggestion but it doesn't work
for
me. The same function is being called in C++ code. I am trying the same
here
in C#.
Please find the following declaration in C++.

DllExport void __stdcall PL_MAIN_DLL(int *driverErrorNum ber,
char *errorMessage,
unsigned int *lengthArg);
Kerem Gümrükcü wrote:
>>Hi,

it is a "clasic" memory boundary violation.
Try to make the:

"string errorMessage"

a

"StringBuilde r errorMessage"

and supply in the Constructor of the StringBuilder
its capacity e.g. 120 for 120 chars. Try to hold it
big enough. You Pinvoke should look
like this:

[DllImport("fort ran.dll", EntryPoint = "FR_MAIN_DL L",
CallingConven tion = CallingConventi on.StdCall)]
public static extern void FR_MAIN_DLL(int errorNumber,
StringBuilde r
errorMessag e, int length);

Your call should look like this:

StringBuild er sb = new StringBuilder(5 12);
int error_code = 1;

FR_MAIN_DLL(e rror_code,sb,sb .Capacity);

then sb will hold the translated error message. You
can get the string by doing a sb.ToString(); But this
onyl works if your function declaration is valid and
calling convention etc is also valid. My pinvoke call
is basing on your signature. The most obvious fault
was the missing StringBuilder, the other data, i dint
know since i dont know this functions signature. Try
it and let me know, or show me the orignal signature,...

Hope this helps,...

Regards

Kerem
>>Hi,
I am getting this exceptions when I am trying to access the Frotran dll.
[quoted text clipped - 35 lines]
>>>
Please help me out in solving this issue.

--
Message posted via http://www.dotnetmonster.com
Sep 8 '08 #4
Hi,
Thanks for the reply.
All the parameters are input ones. Also please let me know how do I pass the
correct variables from C#. Like how do i pass the ref int error number.

Kerem Gümrükcü wrote:
>Hi,,

the point is. What is a IN parameter and what
is a OUT parameter? IN means that we have to
insert the data nto the call and OUT means
that the application will fill the data into our
varibale. This is another suggestion i can make:

[DllImport("fort ran.dll",
EntryPoint = "FR_MAIN_DL L",
CallingConvent ion = CallingConventi on.Winapi)]
public static extern void FR_MAIN_DLL(ref System.Int32 errorNumber,
StringBuilde r errorMessage,
ref System.UInt32 length);

Please tell me what exactly hese parameters mean and which
one must be given to the function as in and wich one does the
application write into our variable. Also would be important
to know whether this is a UNCIODE or ANSI call, i mean the
string part, but it seems to be a ANSI due to char pointer...

Please try th Signature and tell me what exactly the parameters
are on which we are working on...

Regards

Kerem
>Hi Kerem,
Thanks for your reply. I tried as per your suggestion but it doesn't work
[quoted text clipped - 57 lines]
>>>>
Please help me out in solving this issue.
--
Message posted via DotNetMonster.c om
http://www.dotnetmonster.com/Uwe/For...sharp/200809/1

Sep 8 '08 #5
Hi,

then i hope this should work finally:

[DllImport("fort ran.dll",
EntryPoint = "FR_MAIN_DL L",
CallingConventi on = CallingConventi on.Winapi,
CharSet=CharSet .Ansi)]
public static extern void FR_MAIN_DLL(ref System.Int32 errorNumber,
[MarshalAs(Unman agedType.LPStr)]
string errorMessage,
ref System.UInt32 length);
Winapi is calling convention, means windows standard
Charset is set to ANSI, i asume this by the char*
errorMessage will be marshalleed as a single byte,
null terminated ANIS Character string and the
rest should be fine,...

Does the In Parameters have to be initialized
and does the errorMessage have a maximum
of fixed length? If yes, this is also important!

Try it, i hope this should work....

Regards

Kerem
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.codeplex.com/restarts
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
"SSekar via DotNetMonster.c om" <u46044@uweschr ieb im Newsbeitrag
news:89e3d6ba84 699@uwe...
Hi,
Thanks for the reply.
All the parameters are input ones. Also please let me know how do I pass
the
correct variables from C#. Like how do i pass the ref int error number.

Kerem Gümrükcü wrote:
>>Hi,,

the point is. What is a IN parameter and what
is a OUT parameter? IN means that we have to
insert the data nto the call and OUT means
that the application will fill the data into our
varibale. This is another suggestion i can make:

[DllImport("fort ran.dll",
EntryPoint = "FR_MAIN_DL L",
CallingConven tion = CallingConventi on.Winapi)]
public static extern void FR_MAIN_DLL(ref System.Int32 errorNumber,
StringBuild er errorMessage,
ref System.UInt32 length);

Please tell me what exactly hese parameters mean and which
one must be given to the function as in and wich one does the
application write into our variable. Also would be important
to know whether this is a UNCIODE or ANSI call, i mean the
string part, but it seems to be a ANSI due to char pointer...

Please try th Signature and tell me what exactly the parameters
are on which we are working on...

Regards

Kerem
>>Hi Kerem,
Thanks for your reply. I tried as per your suggestion but it doesn't
work
[quoted text clipped - 57 lines]
>>>>>
Please help me out in solving this issue.

--
Message posted via DotNetMonster.c om
http://www.dotnetmonster.com/Uwe/For...sharp/200809/1
Sep 8 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
7864
by: Richard Lionheart | last post by:
Hi All, I tried using RegEx, but the compiler barfed with "The type of namespace 'RegEx' could not be found. Prior to this, I had the same problem with MatchCollection, but discovered it's in the namespace "System.Text.RegularExpressions;" and that namespace is, in turn, defined in the namespace "System", according to MSDN at http://msdn2.microsoft.com/en-us/library/c75he57e(en-us,VS.80).aspx. So adding "using...
0
1248
by: oarrocha | last post by:
I'm trying to use a DLL that was built with Visual C++.NET 7.1 on a new program that I'm developing in VS2005 C# program. Every time I call a function of the 7.1 DLL I receive an anoying AccessViolationException I followed the code, and found that the problem is originated on a simple malloc call: LPBYTE imData = (unsigned char *)malloc(100); Does anybody has an idea of why this happening?
5
13670
by: deepakkumarb | last post by:
I am getting this exception while executing non query from .NET code to oracle 10g DB. Can you plz help System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at System.Reflection.AssemblyName.nInit(Assembly& assembly, Boolean forIntrospection, Boolean raiseResolveEvent) at System.Reflection.AssemblyName.nInit() at...
1
1493
by: 4Codehelp | last post by:
I have a code in VC++ that gives this particular error on exiting the program while executing in release mode. Error: An unhandled exception of type 'System.AccessViolationException' occurred in Unknown Module. Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. The execution breaks at one of these places: 1. file name oleinit.cpp
1
2304
by: Creativ | last post by:
A COM component has an method, called Register(Variant variant), which expect an array of string via Variant. If I want to use it, I can pass an object whose element boxes a string. The UnregisterAllItems method of the component remove all registered items. Most of the time, UnregisterAllItems works fine but sometime UnregisterAllItems returns an AccessViolationException. Any idea why it goes wrong?
8
4045
by: SSix2 | last post by:
.Net 2005 Managed C++ Background. I am writing a managed c++ wrapper for legacy unmanaged code. I created a test application in C# to create the legacywrapper and I am attempting to get some value returned from legacy. I am receiving a System.AccessViolationException at the end of this function... void LegacyWrapper::LegacyWrap::GetValue(String^ dataPath, Object^% pValue) { // This stuff is just used to build up the correct...
12
3551
by: rmiller407 | last post by:
I am getting an AccessViolationException when calling an old legacy fortran dll from c# in vs2008 express, but the same code worked fine in vs2005. If I create a vs2005 c# wrapper dll to call the old fortran dll, then I get the exception if I call that wrapper dll from a vs2008 console app, but not if I call it from a vs2005 console app. I've tried changing the vs2008 code to target the 2.0 framework, but I still get the exception. ...
0
1806
by: Dilip | last post by:
I have cross-posted this question to 3 newsgroups since I am not sure exactly where it belongs (and some NG's have very less activity). Apologies in advance for this. I have set the followup to the dotnet.framework.interop NG but feel free to chage it as appopriate. I have a question regarding converting a Managed System.String^ to unmanaged const char*. I inherited a code that does it like this: String^ s = "00000";
2
1839
by: =?Utf-8?B?VHJpc3RhbiBNU0ROIEtlZW4=?= | last post by:
I've written a test harness in C# to test one of our company's products, which is written in C. If the application crashes, the Instruction Address, Memory Address and the Access Type are stored in the private fields _ip, _target and _accessType of the AccessViolationException that is thrown. I want to be able to report these errors to the user in the C# harness to assist in writing defect reports for the development teams. I can...
5
10887
by: craig1231 | last post by:
I have a problem trying to pass a structure to an unmanaged c++ DLL from C#. When I call PCSBSpecifyPilotLogon from C#, it throws an AccessViolationException This is what I have... The exported C++ DLL Function; __declspec(dllexport) void PCSBSpecifyATCLogon(PCSBSessionID, const char * inServer, unsigned short inPort, const char * inID, const char * inPassword, const PCSBATCConnection_t * inInfo); And the C++ structure;typedef...
0
10566
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10623
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10272
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9401
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7806
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7001
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5669
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4040
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3124
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.