473,434 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

System.AccessViolationException

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.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace TestApplicationFracEngine
{
class InterfaceClass
{
[DllImport("fortran.dll", EntryPoint = "FR_MAIN_DLL",
CallingConvention = CallingConvention.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.AccessViolationException' occurred in
TestApplicationFracEngine.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 14942
Hi,

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

"string errorMessage"

a

"StringBuilder 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("fortran.dll", EntryPoint = "FR_MAIN_DLL",
CallingConvention = CallingConvention.StdCall)]
public static extern void FR_MAIN_DLL(int errorNumber, StringBuilder
errorMessage, int length);
Your call should look like this:

StringBuilder sb = new StringBuilder(512);
int error_code = 1;

FR_MAIN_DLL(error_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
--
-----------------------
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@uweschrieb im Newsbeitrag news:89de08c96fcea@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.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace TestApplicationFracEngine
{
class InterfaceClass
{
[DllImport("fortran.dll", EntryPoint = "FR_MAIN_DLL",
CallingConvention = CallingConvention.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.AccessViolationException' occurred
in
TestApplicationFracEngine.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 *driverErrorNumber,
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

"StringBuilder 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("fortran.dll", EntryPoint = "FR_MAIN_DLL",
CallingConvention = CallingConvention.StdCall)]
public static extern void FR_MAIN_DLL(int errorNumber, StringBuilder
errorMessage, int length);

Your call should look like this:

StringBuilder sb = new StringBuilder(512);
int error_code = 1;

FR_MAIN_DLL(error_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("fortran.dll",
EntryPoint = "FR_MAIN_DLL",
CallingConvention = CallingConvention.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.com" <u46044@uweschrieb im Newsbeitrag
news:89e3394d790a7@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 *driverErrorNumber,
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

"StringBuilder 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("fortran.dll", EntryPoint = "FR_MAIN_DLL",
CallingConvention = CallingConvention.StdCall)]
public static extern void FR_MAIN_DLL(int errorNumber,
StringBuilder
errorMessage, int length);

Your call should look like this:

StringBuilder sb = new StringBuilder(512);
int error_code = 1;

FR_MAIN_DLL(error_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("fortran.dll",
EntryPoint = "FR_MAIN_DLL",
CallingConvention = CallingConvention.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
>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.com
http://www.dotnetmonster.com/Uwe/For...sharp/200809/1

Sep 8 '08 #5
Hi,

then i hope this should work finally:

[DllImport("fortran.dll",
EntryPoint = "FR_MAIN_DLL",
CallingConvention = CallingConvention.Winapi,
CharSet=CharSet.Ansi)]
public static extern void FR_MAIN_DLL(ref System.Int32 errorNumber,
[MarshalAs(UnmanagedType.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.com" <u46044@uweschrieb im Newsbeitrag
news:89e3d6ba84699@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("fortran.dll",
EntryPoint = "FR_MAIN_DLL",
CallingConvention = CallingConvention.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
>>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.com
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
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...
0
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...
5
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...
1
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...
1
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...
8
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...
12
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...
0
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...
2
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...
5
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
1
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...
0
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.