473,800 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Asserting the permission to execute unmanaged code

Hi,
What is the "declaratio n" (class) I need to assert the permission that
my code can run unmanaged code.

I have:
=============== =
[System.Runtime. InteropServices .DllImport("KER NEL32")]

private static extern bool QueryPerformanc eCounter( ref long

lpPerformanceCo unt);

[System.Runtime. InteropServices .DllImport("KER NEL32")]

private static extern bool QueryPerformanc eFrequency( ref long

lpFrequency);

=============== =====

and before calling QueryPerformanc eCounter, I want to Assert() the
permission, but I completely miss the syntax, having not even a real clue
about the class to use (for illustration, SecurityAction, but probably
something else):

=============== =

static Timings()

{
// Initialize the frequency

CodeAccessPermi ssion perm = new SecurityAction? ?? ; // <<< HERE

perm.Assert();

dummy=QueryPerf ormanceFrequenc y( ref frequency);

dummy=QueryPerf ormanceCounter( ref fpsStartingCoun ter);

perm.RevertAsse rt();

....

==============

Thanks in advance,

Vanderghast, Access MVP


Nov 16 '05 #1
10 2545
Michel,

You want to create an instance of the SecurityPermiss ion class, passing
in the UnmanagedCode value from the SecurityPermiss ionFlag enumeration.
From there, you can call Assert.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Michel Walsh" <vanderghast@Vi rusAreFunnierTh anSpam> wrote in message
news:u9******** ******@TK2MSFTN GP14.phx.gbl...
Hi,
What is the "declaratio n" (class) I need to assert the permission that
my code can run unmanaged code.

I have:
=============== =
[System.Runtime. InteropServices .DllImport("KER NEL32")]

private static extern bool QueryPerformanc eCounter( ref long

lpPerformanceCo unt);

[System.Runtime. InteropServices .DllImport("KER NEL32")]

private static extern bool QueryPerformanc eFrequency( ref long

lpFrequency);

=============== =====

and before calling QueryPerformanc eCounter, I want to Assert() the
permission, but I completely miss the syntax, having not even a real clue
about the class to use (for illustration, SecurityAction, but probably
something else):

=============== =

static Timings()

{
// Initialize the frequency

CodeAccessPermi ssion perm = new SecurityAction? ?? ; // <<< HERE

perm.Assert();

dummy=QueryPerf ormanceFrequenc y( ref frequency);

dummy=QueryPerf ormanceCounter( ref fpsStartingCoun ter);

perm.RevertAsse rt();

...

==============

Thanks in advance,

Vanderghast, Access MVP

Nov 16 '05 #2
This should do the trick (in a slightly safer form <g>):

IStackWalk perm = new SecurityPermiss ion
SecurityPermiss ionFlag.Unmanag edCode);
perm.Assert();

try
{
dummy = QueryPerformanc eFrequency(ref frequency);
dummy = QueryPerformanc eCounter(ref fpsStartingCoun ter);
}
finally
{
CodeAccessPermi ssion.RevertAll ();
}
"Michel Walsh" <vanderghast@Vi rusAreFunnierTh anSpam> wrote in message
news:u9******** ******@TK2MSFTN GP14.phx.gbl...
Hi,
What is the "declaratio n" (class) I need to assert the permission that
my code can run unmanaged code.

I have:
=============== =
[System.Runtime. InteropServices .DllImport("KER NEL32")]

private static extern bool QueryPerformanc eCounter( ref long

lpPerformanceCo unt);

[System.Runtime. InteropServices .DllImport("KER NEL32")]

private static extern bool QueryPerformanc eFrequency( ref long

lpFrequency);

=============== =====

and before calling QueryPerformanc eCounter, I want to Assert() the
permission, but I completely miss the syntax, having not even a real clue
about the class to use (for illustration, SecurityAction, but probably
something else):

=============== =

static Timings()

{
// Initialize the frequency

CodeAccessPermi ssion perm = new SecurityAction? ?? ; // <<< HERE

perm.Assert();

dummy=QueryPerf ormanceFrequenc y( ref frequency);

dummy=QueryPerf ormanceCounter( ref fpsStartingCoun ter);

perm.RevertAsse rt();

...

==============

Thanks in advance,

Vanderghast, Access MVP

Nov 16 '05 #3
Sorry, weird copy-paste artifact. That should have been:

IStackWalk perm = new
SecurityPermiss ion(SecurityPer missionFlag.Unm anagedCode);
perm.Assert();
....
"Michel Walsh" <vanderghast@Vi rusAreFunnierTh anSpam> wrote in message
news:u9******** ******@TK2MSFTN GP14.phx.gbl...
Hi,
What is the "declaratio n" (class) I need to assert the permission that
my code can run unmanaged code.

I have:
=============== =
[System.Runtime. InteropServices .DllImport("KER NEL32")]

private static extern bool QueryPerformanc eCounter( ref long

lpPerformanceCo unt);

[System.Runtime. InteropServices .DllImport("KER NEL32")]

private static extern bool QueryPerformanc eFrequency( ref long

lpFrequency);

=============== =====

and before calling QueryPerformanc eCounter, I want to Assert() the
permission, but I completely miss the syntax, having not even a real clue
about the class to use (for illustration, SecurityAction, but probably
something else):

=============== =

static Timings()

{
// Initialize the frequency

CodeAccessPermi ssion perm = new SecurityAction? ?? ; // <<< HERE

perm.Assert();

dummy=QueryPerf ormanceFrequenc y( ref frequency);

dummy=QueryPerf ormanceCounter( ref fpsStartingCoun ter);

perm.RevertAsse rt();

...

==============

Thanks in advance,

Vanderghast, Access MVP

Nov 16 '05 #4
Hi Nicole,
That is here that you hide yourself in these days... :-)

When I compared with FileIOPermissio n, which takes two arguments,
the first argument allowing to Append permissions as example, the fact that
SecurityPermiss ion accepts just one argument, not two, throw me back, but
now that I think about it, that makes sense too.
Is there a relatively good reference on similar security aspects,
with C#; I already have "Profession al C#" (Wrox, first edition), and, sure,
the help file.

Thanks again, and to Nicholas too.

Vanderghast, Access MVP

"Nicole Calinoiu" <calinoiu REMOVETHIS AT gmail DOT com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
This should do the trick (in a slightly safer form <g>):

IStackWalk perm = new SecurityPermiss ion
SecurityPermiss ionFlag.Unmanag edCode);
perm.Assert();

try
{
dummy = QueryPerformanc eFrequency(ref frequency);
dummy = QueryPerformanc eCounter(ref fpsStartingCoun ter);
}
finally
{
CodeAccessPermi ssion.RevertAll ();
}
"Michel Walsh" <vanderghast@Vi rusAreFunnierTh anSpam> wrote in message
news:u9******** ******@TK2MSFTN GP14.phx.gbl...
Hi,
What is the "declaratio n" (class) I need to assert the permission that
my code can run unmanaged code.

I have:
=============== =
[System.Runtime. InteropServices .DllImport("KER NEL32")]

private static extern bool QueryPerformanc eCounter( ref long

lpPerformanceCo unt);

[System.Runtime. InteropServices .DllImport("KER NEL32")]

private static extern bool QueryPerformanc eFrequency( ref long

lpFrequency);

=============== =====

and before calling QueryPerformanc eCounter, I want to Assert() the
permission, but I completely miss the syntax, having not even a real clue
about the class to use (for illustration, SecurityAction, but probably
something else):

=============== =

static Timings()

{
// Initialize the frequency

CodeAccessPermi ssion perm = new SecurityAction? ?? ; // <<< HERE

perm.Assert();

dummy=QueryPerf ormanceFrequenc y( ref frequency);

dummy=QueryPerf ormanceCounter( ref fpsStartingCoun ter);

perm.RevertAsse rt();

...

==============

Thanks in advance,

Vanderghast, Access MVP


Nov 16 '05 #5
You could also put the two calls into their own internal class and apply the

[SuppressUnmanag edCodeSecurity]

attribute to the class. This will stop the stack walk from starting in the first place for these two interop calls.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

This should do the trick (in a slightly safer form <g>):

IStackWalk perm = new SecurityPermiss ion
SecurityPermiss ionFlag.Unmanag edCode);
perm.Assert();

try
{
dummy = QueryPerformanc eFrequency(ref frequency);
dummy = QueryPerformanc eCounter(ref fpsStartingCoun ter);
}
finally
{
CodeAccessPermi ssion.RevertAll ();
}

Nov 16 '05 #6
Hi,
That is exactly what does one of my reference, and I was wondering what
that code was really implying. Definitively, that is preferable in this
case, where high precision timing is expected.

Thanks,
Vanderghast, Access MVP

"Richard Blewett [DevelopMentor]" <ri******@devel op.com> wrote in message
news:eB******** *****@TK2MSFTNG P10.phx.gbl...
You could also put the two calls into their own internal class and apply
the

[SuppressUnmanag edCodeSecurity]

attribute to the class. This will stop the stack walk from starting in the
first place for these two interop calls.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

This should do the trick (in a slightly safer form <g>):

IStackWalk perm = new SecurityPermiss ion
SecurityPermiss ionFlag.Unmanag edCode);
perm.Assert();

try
{
dummy = QueryPerformanc eFrequency(ref frequency);
dummy = QueryPerformanc eCounter(ref fpsStartingCoun ter);
}
finally
{
CodeAccessPermi ssion.RevertAll ();
}

Nov 16 '05 #7
"Michel Walsh" <vanderghast@Vi rusAreFunnierTh anSpam> wrote in message
news:Os******** ******@TK2MSFTN GP11.phx.gbl...
Hi Nicole,
That is here that you hide yourself in these days... :-)
Yeah, it's been quite some time since I used that "other" product for
anything other than support of old apps that just won't die... <g>

When I compared with FileIOPermissio n, which takes two arguments,
the first argument allowing to Append permissions as example, the fact
that SecurityPermiss ion accepts just one argument, not two, throw me back,
but now that I think about it, that makes sense too.
Is there a relatively good reference on similar security aspects,
with C#; I already have "Profession al C#" (Wrox, first edition), and,
sure, the help file.
Last time I went book shopping, "Programmin g .NET Security" (Freeman and
Jones, from O'Reilly) was the best I found. That was about a year ago, so
there might be something better out by now. Also, I never actually got
around to the reading the thing, so I can't give a real recommendation. If
you would like to borrow it for a bit (I _do_ intend to read it eventually
<g>), just give a private shout...


Thanks again, and to Nicholas too.

Vanderghast, Access MVP

"Nicole Calinoiu" <calinoiu REMOVETHIS AT gmail DOT com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
This should do the trick (in a slightly safer form <g>):

IStackWalk perm = new SecurityPermiss ion
SecurityPermiss ionFlag.Unmanag edCode);
perm.Assert();

try
{
dummy = QueryPerformanc eFrequency(ref frequency);
dummy = QueryPerformanc eCounter(ref fpsStartingCoun ter);
}
finally
{
CodeAccessPermi ssion.RevertAll ();
}
"Michel Walsh" <vanderghast@Vi rusAreFunnierTh anSpam> wrote in message
news:u9******** ******@TK2MSFTN GP14.phx.gbl...
Hi,
What is the "declaratio n" (class) I need to assert the permission
that my code can run unmanaged code.

I have:
=============== =
[System.Runtime. InteropServices .DllImport("KER NEL32")]

private static extern bool QueryPerformanc eCounter( ref long

lpPerformanceCo unt);

[System.Runtime. InteropServices .DllImport("KER NEL32")]

private static extern bool QueryPerformanc eFrequency( ref long

lpFrequency);

=============== =====

and before calling QueryPerformanc eCounter, I want to Assert() the
permission, but I completely miss the syntax, having not even a real
clue about the class to use (for illustration, SecurityAction, but
probably something else):

=============== =

static Timings()

{
// Initialize the frequency

CodeAccessPermi ssion perm = new SecurityAction? ?? ; // <<< HERE

perm.Assert();

dummy=QueryPerf ormanceFrequenc y( ref frequency);

dummy=QueryPerf ormanceCounter( ref fpsStartingCoun ter);

perm.RevertAsse rt();

...

==============

Thanks in advance,

Vanderghast, Access MVP



Nov 16 '05 #8
It would probably be a good idea to add a level of abstraction to that
arrangement since it has a rather high likelihood of becoming less secure
with subsequent modification. A somewhat safer appropach would be to make
the interop calls private to the new internal class, with internal method
wrappers exposed for calling the private methods. This would at least
ensure that a checkpoint exists for screening all callers to the unmanaged
code, should this ever become necessary or desirable (i.e.: if security
concerns are ever found to outweigh performance concerns).
"Richard Blewett [DevelopMentor]" <ri******@devel op.com> wrote in message
news:eB******** *****@TK2MSFTNG P10.phx.gbl...
You could also put the two calls into their own internal class and apply
the

[SuppressUnmanag edCodeSecurity]

attribute to the class. This will stop the stack walk from starting in the
first place for these two interop calls.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

This should do the trick (in a slightly safer form <g>):

IStackWalk perm = new SecurityPermiss ion
SecurityPermiss ionFlag.Unmanag edCode);
perm.Assert();

try
{
dummy = QueryPerformanc eFrequency(ref frequency);
dummy = QueryPerformanc eCounter(ref fpsStartingCoun ter);
}
finally
{
CodeAccessPermi ssion.RevertAll ();
}

Nov 16 '05 #9
It depends on the size of the library that is performing this of course. On the basis that this class is internal if the library is not large then, as you in control of the code, then changing te level of abstraction is not necessarily a huge deal. Adding a level of abstraction adds complexity to the library creator (not the consumer who is insulated as this class is internal) which may or may not be justified in the the component in question.

I wouldn't at all suggest adding that attribute to a public class.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

It would probably be a good idea to add a level of abstraction to that
arrangement since it has a rather high likelihood of becoming less secure
with subsequent modification. A somewhat safer appropach would be to make
the interop calls private to the new internal class, with internal method
wrappers exposed for calling the private methods. This would at least
ensure that a checkpoint exists for screening all callers to the unmanaged
code, should this ever become necessary or desirable (i.e.: if security
concerns are ever found to outweigh performance concerns).


Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2554
by: Russ | last post by:
Hello. My new dev machine is running XP Pro. In the past all equipment has only used Windows 2000. I have had a lot of problems getting my projects up and running on the new machine. The current one is a permission error. The project is a VC++ Web Service. It works fine when the service is hosted on the old W2K dev machine, but on the new XP machine I get a permission error when the service tries to open a text file on the Windows...
1
3458
by: Mohan | last post by:
Hi, I have developed a .NET Windows application. I able to run(Execute) it from the local drive of my PC. If I try to execute it from the SourceControl(Clear case) or from Shared drives, then it is not starting the application, but given the below error. Request for the permission of type System.Security.Permission.RegistryPermission, mscorlib, Version=1.0.5000.0, Culture=neutral,PublicKeyToken=b77a5c561934e089 failed
8
9567
by: Jiggaz | last post by:
Hi, In my ASPX Page, i have a form for signup. And whene user click on the button, the event Button1_Click must use a stored procedure. But instead of use stored proc, i get this exception : _____ System.Data.SqlClient.SqlException: EXECUTE permission denied on object 'CreateAccount', database 'wizou', schema 'dbo'. at
7
1753
by: Mike L. | last post by:
Hi, I got this 'EXECUTE permission denied on object <mySproc>' error message everytime I try executing my SQL server Sproc. What's this and how to fix this err? many thnaks in advance, mike
3
1287
by: Marius Groenendijk | last post by:
Hi group, I want my app to show a msg if my it doesn't have the required permission(s), however this simply doesn't work. What am I overlooking/doing wrong?? Try Dim x As New SecurityPermission(SecurityPermissionFlag.UnmanagedCode)
0
1010
by: Mamatha | last post by:
Hi i hava wrote the code to restore a file of SQL server database through ASP.It can access the backup file on server and restored but some times or in some systems it gives error like permission denide.What is the reason for that. Here is the code: <!--#Include File="Connect.inc"-->
8
6052
by: mike2036 | last post by:
I have an application (that has unmanaged code) and when I launch it without 'FullTrust' permissions (LocalIntranet_Zone), it crashes. When I set 'FullTrust' permissions, it launches fine. Is there a way I can compile the application such that it won't even attempt to launch without correct permissions instead of just crashing? Thanks for any help. -Mike
4
4502
by: Richard MSL | last post by:
I have a simple application in C++ that uses a dll I wrote in C#. It works fine when I run it on C: on my local PC, but when I attempt to run it on the i: drive on my LAN, it gets a loading permission error, presumably when loading my c# dll. I gather that I must set an attribute in my C++ program to grant security to load the dll, but I can not figure out what to use. Here is my c++ program: #include <stdio.h> #using <csfu.dll>
1
3441
by: Michael Tissington | last post by:
How can I enable execute permissions for a folder on my website using the web.config file ?
0
9690
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
10275
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...
1
10253
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7576
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
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
3
2945
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.