472,993 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 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 14806
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.