By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,230 Members | 2,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,230 IT Pros & Developers. It's quick & easy.

System.AccessViolationException

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.