MethodImplOptions.InternalCall gives Security Exception

  • Thread starter Thread starter Robert A. van Ginkel
  • Start date Start date
R

Robert A. van Ginkel

I have a strange C# problem. Is the following a bug? Because it should be
possible to run unsafe code. How can I accomplish this? And where can I read
more about this, because documentation on this is rare.

I have a project where i need to use some internal calls. In this project I
have 'Allow unsafe code blocks' set to true.
And in my class i have:

[MethodImpl(MethodImplOptions.InternalCall)]
extern private static IntPtr GetInvalidHandle();

private static readonly IntPtr InvalidHandle = GetInvalidHandle();

At runtime I get a Runtime Error called:
An unhandled exception of type 'System.Security.SecurityException' occurred
in mydll.dll
Additional information: Security error.

Can you please clarrify the problem?

Regards, Robert
 
Robert A. van Ginkel wrote:
|| I have a strange C# problem. Is the following a bug? Because it
|| should be possible to run unsafe code. How can I accomplish this?
|| And where can I read more about this, because documentation on this
|| is rare.
||
|| I have a project where i need to use some internal calls. In this
|| project I have 'Allow unsafe code blocks' set to true.
|| And in my class i have:
||
|| [MethodImpl(MethodImplOptions.InternalCall)]
|| extern private static IntPtr GetInvalidHandle();
||
|| private static readonly IntPtr InvalidHandle = GetInvalidHandle();
||
|| At runtime I get a Runtime Error called:
|| An unhandled exception of type 'System.Security.SecurityException'
|| occurred in mydll.dll
|| Additional information: Security error.
||
|| Can you please clarrify the problem?
||
|| Regards, Robert

You can't do this. The explicit use of internalcall will cause
the execution engine to throw a System.Security.SecurityException.
"MethodImplOptions.InternalCall" is used to declare a method in managed code that has no
managed implementation. The implementation (unmanaged) is supplied by the runtime itself (CLR).

What exactly are you trying to achieve?

Willy.
 
Thanx for replying Willy,

I know that there is managed code and unmanaged, what the difference is I
don't know, I will look this up shortly but what I can understand is that
because I dont have the source of Framework with *.pdb files and headers and
stuff, I can't call it.
First of all why can't I just call these function's, like calling api's in
vb6? And can I never achieve this or should I first do something?

On the question what am I making:
A new socket lib., because with the framework his System.Net.Sockets
assembly it happends that if you send data to a server and when that server
closes the socket (i.e. smtp, quit command) it sometimes happens that the
socket hangs. First of all Framework is Cool, Development Enviroment Sucks,
Why, because if you pause he stops all threads and breaks at the main, there
is no thread monitor where u can choose which thread to pause. And if I am
listening to a port in a thread, I can't Abort that thread.
So I looked around and found some smart guys from Southern Storm Software
that made .NET for unix, ok so I had the source of .NET, great, I ripped out
Socket, TcpClient, Networkstream, TcpListener and compiled this, and found
myself with a security problem.

How can I solve this?

----- Original Message -----
From: "Willy Denoyette [MVP]" <[email protected]>
Newsgroups:
microsoft.public.dotnet.academic,microsoft.public.dotnet.framework,microsoft
..public.dotnet.framework.clr,microsoft.public.dotnet.languages.csharp,micros
oft.public.msdn.drgui.drguidotnet.discussion
Sent: Monday, August 25, 2003 12:17 PM
Subject: Re: MethodImplOptions.InternalCall gives Security Exception

Robert A. van Ginkel wrote:
|| I have a strange C# problem. Is the following a bug? Because it
|| should be possible to run unsafe code. How can I accomplish this?
|| And where can I read more about this, because documentation on this
|| is rare.
||
|| I have a project where i need to use some internal calls. In this
|| project I have 'Allow unsafe code blocks' set to true.
|| And in my class i have:
||
|| [MethodImpl(MethodImplOptions.InternalCall)]
|| extern private static IntPtr GetInvalidHandle();
||
|| private static readonly IntPtr InvalidHandle = GetInvalidHandle();
||
|| At runtime I get a Runtime Error called:
|| An unhandled exception of type 'System.Security.SecurityException'
|| occurred in mydll.dll
|| Additional information: Security error.
||
|| Can you please clarrify the problem?
||
|| Regards, Robert

You can't do this. The explicit use of internalcall will cause
the execution engine to throw a System.Security.SecurityException.
"MethodImplOptions.InternalCall" is used to declare a method in managed code that has no
managed implementation. The implementation (unmanaged) is supplied by the runtime itself (CLR).

What exactly are you trying to achieve?

Willy.
 
Back
Top