简体   繁体   English

如何在管理代码中使用Windows本机dll?

[英]How to use Windows native dll with Administrative code?

I have made a windows native dll (written in C) which tries to acquire exclusive access of a volume when opening it with CreateFile function. 我已经制作了一个Windows本机dll(用C语言编写),当使用CreateFile函数打开它时,它试图获取卷的独占访问权限。 This requires administrative privileges most the time as my dll also tries to lock volume and unmount volume at some point of time. 由于我的dll也会在某个时间点尝试锁定卷和卸载卷,因此大多数时候这需要管理特权。 I start my executable project as an administrator from Visual studio 2010 after referencing my dll project in to the solution. 在将我的dll项目引用到解决方案后,我以管理员身份从Visual Studio 2010启动了可执行项​​目。 To this point, it works fine and my process is able to get exclusive access of the volume. 至此,它工作正常,并且我的进程能够获得该卷的独占访问权。

Now when I release my dll and use it with my GUI program built with QT (also started as an administrator), it is not able to open volume and gives me sharing violation error. 现在,当我释放dll并将其与QT构建的GUI程序(也以管理员身份启动)一起使用时,它无法打开卷,并给我带来共享冲突错误。 I assume that my dll is not able to acquire administrative rights. 我认为我的dll无法获得管理权限。 How should I use my dll so that it can get exclusive access to the volume? 我应该如何使用我的dll,以便它可以独占访问该卷?

Any help would be appreciated. 任何帮助,将不胜感激。

Your problem is elsewhere. 您的问题在其他地方。 Elevation is performed by the process when is starts. 高度是在开始时由过程执行的。 If the process that hosts your DLL is elevated, then the code in your DLL will execute with elevated privileges. 如果提升了承载DLL的进程,则DLL中的代码将以提升的特权执行。

The error message that you report is a sharing violation, presumably ERROR_SHARING_VIOLATION . 您报告的错误消息是共享冲突,可能是ERROR_SHARING_VIOLATION That indicates that some other party has opened the volume in a way that blocks sharing. 这表明某个其他方以阻止共享的方式打开了该卷。 This is not the error that you will receive if you run without elevation. 如果没有高程运行,这不是您将收到的错误。 If you attempt to open a volume without sufficient rights, you would see ERROR_ACCESS_DENIED or ERROR_ELEVATION_NEEDED . 如果尝试在没有足够权限的情况下打开卷,则会看到ERROR_ACCESS_DENIEDERROR_ELEVATION_NEEDED

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM