Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm using a dll both in a service and in an application.
How can I findout in a function of the dll, whether it is used actually in a service or in an application (e. g. for using MessageBox.Show)?

Thanks, Gerald

What I have tried:

Can't find any help for this problem ...
Posted
Updated 28-Oct-20 2:24am
Comments
PIEBALDconsult 28-Oct-20 14:56pm    
I looked through some old service code and I didn't find anything like that, I thought there was. I know I have code which can run either way, but I'm unsure it detects it.

I have found a method I wrote to determine whether or not a process is interactive, which uses System.Environment.UserInteractive (as mentioned below), but it also detects whether or not the console is redirected (to a pipe), which might not be what you need.

The simplest answer is not to use a MessageBox in a dll that is shared with non-interactive applications. If the dll needs to report something, then it should do so by returning information to the calling process (status, text etc.). The caller can then decide what action to take: for a service use a logging sytem, for a Windows app use a MessageBox, or for a Console app a Console.WriteLine.
 
Share this answer
 
You can't use MessageBox.Show in a service - they have no user interface.

If the service and the app have different names, you can use Application.ExecutablePath which returns the path to the EXE file that started the app.

It's also possible to get the app GUID of the "startup application" but that's quite a bit more involved.
 
Share this answer
 
v2
Comments
Member 11168540 28-Oct-20 7:13am    
"You can't use MessageBox.Show in a service - they have no user interface."
That's the reason, while I want to know, if function is used in a service or an application, then I could handle the MessageBox.
So there is no easier way than to distinguish between service and application over Name or GUID?
If Dll is used in a lot of services and applications, this way seems to be not very comfortable.
There are two possible options you could look at to best determine whether a process is a service, though I don't believe there's a concrete "yes this is absolutely a service" check you can explicitly do.

1. Use System.Environment.UserInteractive which, when returning true, indicates that the current process couldn't be a service.

2. Use Console.In by attempting to assign it to a variable within a try block, and if an exception is thrown then there's no console input, and therefore the process is likely to be a service.

Again, these aren't definitive answers but they can help you narrow it down.
 
Share this answer
 
Comments
Member 11168540 28-Oct-20 7:17am    
Ok, thanks. I think, then I could better set directly the MessageBox.Show into a try block.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900