473,587 Members | 2,447 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 14984
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
7820
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
1225
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
13655
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
1483
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
2291
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
4029
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
3520
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
1776
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
1819
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
10862
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
7924
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7854
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8219
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8349
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...
0
8221
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
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
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.