M2TeamArchived / NSudo

[Deprecated, work in progress alternative: https://github.com/M2Team/NanaRun] Series of System Administration Tools
https://nsudo.m2team.org/
Other
1.96k stars 224 forks source link

DevilMode: can it be used from a dotnet Windows Service? #102

Closed OllieJones closed 2 years ago

OllieJones commented 2 years ago

I hope to create a Windows Service, written in C#, to use your "devil mode". It's excellent when used from an elevated process for Any platform or for x86.

When trying for the x64, the LoadLibrary (for the 64-bit DLL) fails with error 126 ( (generic DLL failure)

I have tried to make my service work similarly to my ordinary elevated process. But the LoadLibrary operation fails.

Here are the scenarios I've tried:

C# targeting Any platform ... the attempt gets Win32 error 203 (missing environment) ... targeting x86 ... the attempt gets 126 (generic DLL failure)

Is there an example of using devil mode in a service I may follow? Thanks.

MouriNaruto commented 2 years ago

It looks like error 126 is "The specified module could not be found.". Where is the folder you put the Devil Mode dll?

//
// MessageId: ERROR_MOD_NOT_FOUND
//
// MessageText:
//
// The specified module could not be found.
//
#define ERROR_MOD_NOT_FOUND              126L

Kenji Mouri

OllieJones commented 2 years ago

You are right. My service could not find the DevilMode DLL. Sorry for the dumb question.

The thing I learned is this: assemblies (dotnet programs) running as services have c:\Windows\System32 as their working directories, and LoadLibrary loads from the working directory unless you give it a path.

So, if the DLL is in the same directory as the service .exe one can do this to find it. This will work whether in an elevated app or a service.

var asmLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
var path = Path.GetDirectoryName(asmLocation ) + @"\NSudoDMX86.dll";
_libHandle =  LoadLibrary (path);

Thank you for very useful software!