|
You should ask yourself if you should be doing this. The reason I used this code was I had a windows service that logged into a DB2 database via an ODBC driver. They routinely changed their passwords and sometimes would forget to tell us. When we tried to login in after a password had been changed the ODBC driver would pop up (attempt to pop up) a dialog allowing you to enter the new password which of course never showed up as it didn't have permissions. We didn't realize any problem until the client called telling us that they hadn't received any new data.
Anyways the following code worked for me.
Reference System.Management
protected override void OnStart(string[] args)
{
ServiceDesktopPermission();
}
static public void ServiceDesktopPermission()
{
try
{
ConnectionOptions coOptions = new ConnectionOptions();
coOptions.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope mgmtScope = new ManagementScope(@"root\CIMV2", coOptions);
mgmtScope.Connect();
ManagementObject wmiService;
wmiService = new ManagementObject("Win32_Service.Name='" + "My Service Name" + "'");
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"] = true;
wmiService.InvokeMethod("Change", InParam, null);
}
catch(Exception ex)
{
}
}
|
|
|
|
|
it worked for me; thanks a lot
|
|
|
|
|
Excellent, thank you. Just what I was looking for. I only have one suggestion: do it in the service installer after service installation is completed, before the service starts up. This way the first time the service starts, it's already able to interract with the desktop:
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
ServiceDesktopPermission();
var controller = new ServiceController("SERVICE_NAME");
controller.Start();
}
|
|
|
|
|
Try this C# code
href="http://it.expertmonster.com/question/Allow-service-to-interact-with-desktop-112.html
|
|
|
|
|
Hi
I am trying to modify class instance members using reflection. I am having problem when trying to add/remove/display elements related to List<int> member.
Following is the code.
[CODE]
class TestClass
{
public int i = 0;
public int IValue
{
get
{
return i;
}
set
{
i = value;
}
}
public List<int> m_intList = new List<int>();
}
class Program
{
static void Main(string[] args)
{
TestClass tcObject = new TestClass();
tcObject.i = 1;
tcObject.m_intList.Add(1);
tcObject.m_intList.Add(2);
{
FieldInfo fieldInfo = tcObject.GetType().
GetField(
"i",
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public);
fieldInfo.SetValue(tcObject, 2);
System.Console.WriteLine("I value '{0}'", fieldInfo.GetValue(tcObject));
}
{
PropertyInfo propertyInfo = tcObject.GetType().
GetProperty(
"IValue",
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public);
MethodInfo propertySetMethodInfo =
propertyInfo.GetSetMethod(true);
propertySetMethodInfo.Invoke(tcObject, new Object[] { 3 });
System.Console.WriteLine("Property IValue '{0}'", tcObject.i);
}
{
FieldInfo fieldInfo = tcObject.GetType().
GetField(
"m_intList",
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public);
foreach (int intItem in tcObject.m_intList)
{
System.Console.WriteLine("List Item value '{0}'", intItem);
}
}
}
}
</string></int></int></int> [/CODE]
Thanks
Chandra
modified on Thursday, October 16, 2008 1:55 AM
|
|
|
|
|
I don't want to lower your morale but I've been trying some time ago a similar thing to do and I had to abandon the idea because it get's very problematic when you are trying to generalize the issue. The main idea is, if you don't know the 'assembly type', you don't have a chance...
|
|
|
|
|
What do you mean by 'assembly type'. The assembly is .NET assembly written in C#.
|
|
|
|
|
At compile time I do not know whether the type is List<int> or List<string>. At runtime I should interpret it from the fieldInfo.FieldType.
|
|
|
|
|
I even tried the following code
MethodInfo addMethodInfo = fieldInfo.FieldType.GetMethod("Add");
object[] intValue = { 5 };
addMethodInfo.Invoke(tcObject, new Object[] { 5 });
But the above code gives me error "Unhandled Exception: System.Reflection.TargetException: Object does not match target type." when executing the "invoke"
|
|
|
|
|
Ok, forget 'assembly', if you don't know an Object type, you can't construct it... Hope somebody will have a solution for you problem, I'm just saying what I was trying to do and always came back to the same problem...
|
|
|
|
|
Hello everyone, thanks for your help. I was able to find the solution for my requirement. Following is the code
class ListElement
{
public ListElement()
{
m_element = 0;
}
public ListElement(int element)
{
m_element = 1;
}
public int m_element;
}
class TestClass
{
public int i = 0;
public int IValue
{
get
{
return i;
}
set
{
i = value;
}
}
public List<int> m_intList = new List<int>();
public List<listelement> m_lstElement = new List<listelement>();
}
class Program
{
static void Main(string[] args)
{
TestClass tcObject = new TestClass();
tcObject.i = 1;
tcObject.m_intList.Add(1);
tcObject.m_intList.Add(2);
tcObject.m_lstElement.Add(new ListElement(1));
{
FieldInfo fieldInfo = tcObject.GetType().
GetField(
"i",
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public);
fieldInfo.SetValue(tcObject, 2);
System.Console.WriteLine("I value '{0}'", fieldInfo.GetValue(tcObject));
}
{
PropertyInfo propertyInfo = tcObject.GetType().
GetProperty(
"IValue",
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public);
MethodInfo propertySetMethodInfo =
propertyInfo.GetSetMethod(true);
propertySetMethodInfo.Invoke(tcObject, new Object[] { 3 });
System.Console.WriteLine("Property IValue '{0}'", tcObject.i);
}
{
FieldInfo fieldInfo = tcObject.GetType().
GetField(
"m_intList",
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public);
MemberInfo[] listMemberInfoArray = tcObject.GetType().GetMember("m_intList");
MemberInfo listMemberInfo = listMemberInfoArray[0];
MethodInfo addMethodInfo = fieldInfo.FieldType.GetMethod("Add");
object[] elementValue = { 5 };
addMethodInfo.Invoke(fieldInfo.GetValue(tcObject), elementValue);
foreach(int currElement in tcObject.m_intList)
{
System.Console.WriteLine("INt List element {0}", currElement);
}
Console.WriteLine();
}
{
FieldInfo fieldInfo = tcObject.GetType().
GetField(
"m_lstElement",
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public);
MethodInfo addMethodInfo = fieldInfo.FieldType.GetMethod("Add");
Object listElementObject = Activator.CreateInstance(fieldInfo.
FieldType.
GetGenericArguments()[0]);
FieldInfo listElementFieldInfo = listElementObject.GetType().
GetField(
"m_element",
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public);
listElementFieldInfo.SetValue(listElementObject, 2);
object[] elementValue = { listElementObject };
addMethodInfo.Invoke(fieldInfo.GetValue(tcObject), elementValue);
foreach (ListElement currListElement in tcObject.m_lstElement)
{
System.Console.WriteLine("List Item value '{0}'", currListElement.m_element);
}
}
}
}
</listelement></listelement></int></int>
|
|
|
|
|
mates,
I have a project solution file but when i open it in microsoft visual studio 2005, format version 9.00, it doesn't open. i search already in google but does not solve my problem. some results are due to version problem, such as microsoft visual studio 2005 open in microsoft visual studio 2008. but my problem is even i open it in visual 2005 it still not open. could someone help my problem, i'm stuck on this. thanks in advance.
C# Coudou
Microsoft End User
2000-2008
******************************
The best things in life are free
******************************
|
|
|
|
|
Seems rather obvious to me. You are trying to open a VS2008 (ver 9) file with vs2005 (ver 8). Why not get VS2008 express.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
no.i'm trying to open vs2005(ver9) in vs2005(ver9).
there is no vs2008(ver9), i think so.
C# Coudou
Microsoft End User
2000-2008
******************************
The best things in life are free
******************************
|
|
|
|
|
ver 9 is VS 2008
Open up VS, go to help | about and you will see the version, in 2005 it is 8.#######.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
This is what i mean, open a solution file in a notepad and you can see the version.
=========================
solution file for vs2005
-----------------------------------------------------------------------------
Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "Squawk", "Squawk\Squawk.vbproj", "{AF4879E7-ED61-4247-A0A3-46A4D14B536D}"
EndProject
=========================
solution file for vs2008
-----------------------------------------------------------------------------
Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "ProjectConverter", "ProjectConverter.vbproj", "{B637ACFD-0AFC-4FBB-A8C0-602B5ABA62F0}"
EndProject
* My problem is every time I double click my solution file it will not open eventhough it is in vs2005.
C# Coudou
Microsoft End User
2000-2008
******************************
The best things in life are free
******************************
|
|
|
|
|
Interesting, I looked at the VS IDE, not the solution file. The IDE version is 8 and 9, not 10.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Take a look at my response here[^].
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|
|
Subtle but confusing difference. The Microsoft Visual Studio Solution File, Format Version 10.00 line does not indentify the version of Visual Studio but rather the version of the solution file itself. The version of Visual Studio is identified by the next line (# Visual Studio 2008 .
Visual Studio 2008 is actually Visual Studio version 9 but uses a solution file format of 10.00. Likewise, Visual Studio 2005 is actually Visual Studio version 8 but uses a solution file format of 9.00.
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|
|
Mycroft Holmes wrote: ver 9 is VS 2008
WRONG! VS2k8 is 10 (well the solution format version number thingies says so).
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - coming soon ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
Yeah, I was looking at the IDE, nasty bit of versioning there.....
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
so, is there any solutions out there? i want to open this file but seems it still can't open..
C# Coudou
Microsoft End User
2000-2008
******************************
The best things in life are free
******************************
|
|
|
|
|
Hi,
Don't bother trying to fix it. Just delete the solution file and then open the main project in VS. It will automatically generate a new solution. You can then add any other projects back into the new solution using "add existing project".
Alan.
|
|
|
|
|
You are trying to open a Visual Studio 2008 solution file in Visual Studio 2005? You can't do that. You can open projects but not solutions.
Your only options are to have two different solution files, one for VS2008 and one for VS2005 or to edit the solution file each time. To edit the solution file, simply change the "10.00" to a "9.00" (line 1) and the "2008" to a "2005" (line 2).
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|
|
Well, only partly. See my response here[^].
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|