473,503 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Peramanent event consumer(WMI) in c#

Dheeraj Joshi
1,123 Recognized Expert Top Contributor
I have been trying to register a permanent event consumer in a remote machine, i am able to register for an event, but when the event occurs in remote machine it wont forward back the response...

Code for registering

Expand|Select|Wrap|Line Numbers
  1. connOptions = new ConnectionOptions();
  2. connOptions.Username = "10.50.27.103\\testadmin";
  3. connOptions.Password = "Tel12345";
  4. connOptions.Authentication = AuthenticationLevel.PacketPrivacy;
  5. connOptions.EnablePrivileges = true;
  6. connOptions.Impersonation = ImpersonationLevel.Impersonate;
  7. mScope = new ManagementScope("\\10.50.27.103\\root\\subsription", connOptions);
  8.                 mScope.Connect();
  9. if (mScope.IsConnected)
  10. {
  11. Console.WriteLine(SystemName + "Connection Successfull");
  12.  
  13. ObjectGetOptions goption = new ObjectGetOptions();
  14.  
  15. ManagementClass objEventFilterClass = new                                                                       ManagementClass("\\\\10.50.27.103\\root\\subscription",
  16.                                                         "__EventFilter", null);
  17.                     objEventFilterClass.Scope = mScope;
  18.  
  19. ManagementObject mgmtobj = objEventFilterClass.CreateInstance();
  20. mgmtobj["CreatorSID"] = null;
  21. mgmtobj["EventNamespace"] = "\\\\10.50.27.103\\root\\cimv2";
  22. mgmtobj["Name"] = "cmdListner";
  23. mgmtobj["Query"] = "SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " +
  24.                         "TargetInstance ISA \"Win32_Process\"" +
  25.                         " AND TargetInstance.Name = \"cmd.exe\"";
  26.  
  27. mgmtobj["QueryLanguage"] = "WQL";
  28. mgmtobj.Put(options);
  29.  
  30. string[] strArr = { "10.50.26.217" };
  31.  
  32.  
  33. ManagementClass objForwardingConsumerClass = new ManagementClass("\\\\10.50.27.103\\root\\subscription",
  34.                                                                 "MSFT_ForwardingConsumer", goption);
  35.  
  36. objForwardingConsumerClass.Scope = mScope;
  37. Console.WriteLine("GET OPTION IS" + goption.Context.ToString());
  38. ManagementObject mgmtobj1 = objForwardingConsumerClass.CreateInstance();
  39.  
  40. mgmtobj1["CreatorSID"] = null;
  41. mgmtobj1["Name"] = "forwadingCMD";
  42. mgmtobj1["Authenticate"] = false;
  43. mgmtobj1["Targets"] = strArr;
  44. mgmtobj1["Encryption"] = true;
  45. mgmtobj1.Put(options);
  46.  
  47.  
  48. ManagementClass objBindingclass = new ManagementClass("\\\\10.50.27.103\\root\\subscription",
  49.                                                         "__FilterToConsumerBinding", null);
  50.  
  51. objBindingclass.Scope = mScope;
  52. ManagementObject mgmtobjbind = objBindingclass.CreateInstance();
  53. mgmtobjbind["CreatorSID"] = null;
  54. mgmtobjbind["Consumer"] = mgmtobj1;
  55. mgmtobjbind["Filter"] = mgmtobj;
  56. mgmtobjbind.Put(options);
  57. Console.WriteLine(" \\\\10.50.27.103 Registered SucccessFully ");
  58. }
  59.  
  60. else
  61.  
  62. Console.WriteLine("CED4 Connection not succeeded");
  63.  
  64.  
And code for forwarding...

Expand|Select|Wrap|Line Numbers
  1. PutOptions options = new PutOptions();
  2. options.Type = PutType.UpdateOrCreate;
  3.  
  4.  
  5. ConnectionOptions connOptions1 = new ConnectionOptions();
  6. connOptions1.Username = "santaji";
  7. connOptions1.Password = "san@124421";
  8. connOptions1.Authentication = AuthenticationLevel.PacketPrivacy;
  9. connOptions1.Impersonation = System.Management.ImpersonationLevel.Impersonate;
  10. connOptions1.EnablePrivileges = true;
  11. connOptions1 = null;
  12. localScope = new ManagementScope("\\\\10.50.26.115\\root\\subscription", connOptions1);
  13.  
  14. localScope.Connect();
  15. if (localScope.IsConnected)
  16. {
  17.  
  18. Console.WriteLine("10.50.26.115  Connection Successfull");
  19.  
  20. PutOptions option = new PutOptions();
  21. option.Type = PutType.UpdateOrCreate;
  22. ObjectGetOptions goption = new ObjectGetOptions();
  23.  
  24. ManagementClass objEventConsumer = new ManagementClass("\\\\10.50.26.115\\root\\subscription",
  25.                                                                 "LogFileEventConsumer", goption);
  26.  
  27. objEventConsumer.Scope = localScope;
  28. ManagementObject mgmtoj1 = objEventConsumer.CreateInstance();
  29. mgmtoj1["CreatorSID"] = arrint;
  30. mgmtoj1["Name"] = "Logfile";
  31. mgmtoj1["Filename"] = "D:\\FORWARDTraceLog.csv";
  32.  
  33. mgmtoj1["Text"] = "\"%TargetInstance.Name%\"," +
  34.                                                     "\"%TIME_CREATED%\"" +
  35.                                                     "\"%ProcessId%\"";
  36. mgmtoj1.Put(options);
  37.  
  38.  
  39. ManagementClass objEventFilter = new ManagementClass("\\\\10.50.26.115\\root\\subscription",
  40.                                                         "__EventFilter", null);
  41.  
  42. objEventFilter.Scope = localScope;
  43.  
  44. ManagementObject localmgmt = objEventFilter.CreateInstance();
  45. localmgmt["CreatorSID"] = arrint;
  46. localmgmt["EventNamespace"] = "\\\\10.50.26.115\\Root\\Cimv2";
  47. localmgmt["Name"] = "cmdListnerLocal";
  48. localmgmt["Query"] = "select * from MSFT_ForwardedEvent";
  49.  
  50.  
  51. localmgmt["QueryLanguage"] = "WQL";
  52. localmgmt.Put(options);
  53.  
  54. ManagementClass objBinding = new ManagementClass("\\\\10.50.26.115\\root\\subscription",
  55.                                                         "__FilterToConsumerBinding", null);
  56.  
  57. objBinding.Scope = localScope;
  58. ManagementObject mgmtbind = objBinding.CreateInstance();
  59. mgmtbind["CreatorSID"] = arrint;
  60. mgmtbind["Consumer"] = mgmtoj1;
  61. mgmtbind["Filter"] = localmgmt;
  62. mgmtbind.Put(options);
  63.  
  64. }
  65. else
  66.  
  67. Console.WriteLine("10.50.26.115 Connection not succeeded");
  68.  
  69.  
I have posted entire code snippet.... Its bit long i know. I really appreciate any helps.

Regards
Dheeraj Joshi
Sep 19 '09 #1
11 4979
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
Adding to previous question...

There is no support for permanent event consumer after Windows 2003 server and Vista..

So can we still register permanent events?? If yes, is there any alternative methods? I could not find one.. Any help is appreciated...

Regards
Dheeraj Joshi
Sep 19 '09 #2
Plater
7,872 Recognized Expert Expert
EDIT: whoops, that will teach me not to read
So your piece of software, listens for events on a remote machine, then attempts to fire the same event on a different remote machine?
Sep 21 '09 #3
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
Basically i am trying to register for an event in PC2(Remote comp).. When event occurs there PC2 is supposed to forward the event notification to my software running on PC1(My machine).

Regards
Dheeraj Joshi
Sep 23 '09 #4
Plater
7,872 Recognized Expert Expert
Hmm well I cannot seem to attach to any of the winXP Pro systems in my network (spent the last hour trying to deal with "RPC server not available" exception)
I could connect to my 2003 server computer, but I cannot find any events in the namespace that ever fire.

is your forwarded notification getting blocked by a firewall or anything?
Sep 23 '09 #5
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
Nop.... In both machines firewall is disabled....

I do register fwdprov.dll by command

Expand|Select|Wrap|Line Numbers
  1. regsvr32 fwdprov.dll
  2.  
In wbemess.log file in C:\WINDOWS\system32\wbem\Logs\ (In remote machine PC2)
I get following log entry

(Thu Sep 24 09:53:53 2009.658000) : Consumer provider Microsoft WMI Forwarding Consumer Provider does not support IWbemUnboundObjectSink: error code 80004002

So.. Any idea on this....

Regards
Dheeraj Joshi
Sep 24 '09 #6
Plater
7,872 Recognized Expert Expert
I think you know more on the subject then I.

The only way I know how to get a remote event is with code something like this:
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.   ConnectionOptions connection = new ConnectionOptions();
  4.   connection.Username = "username";
  5.   connection.Password = "password";
  6.   connection.Authority = "ntlmdomain:mydomain";
  7.  
  8.   ManagementScope scope = new ManagementScope("\\\\remotePC\\root\\CIMV2", connection);
  9.   scope.Connect();
  10.  
  11.   WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent");
  12.  
  13.   ManagementEventWatcher watcher = new ManagementEventWatcher(scope, query);
  14.   ManagementBaseObject eventObj = watcher.WaitForNextEvent();
  15.   Console.WriteLine("{0} event occurred.", eventObj["__CLASS"]);
  16.   // Cancel the event subscription
  17.   watcher.Stop();
  18.  
  19.   Close();
  20.   return;
  21. }
  22. catch(ManagementException err)
  23. {
  24.   MessageBox.Show("An error occurred while trying to receive an event: " + err.Message);
  25. }
  26. catch(System.UnauthorizedAccessException unauthorizedErr)
  27. {
  28.   MessageBox.Show("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);
  29. }
  30.  
Sep 24 '09 #7
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
Hey PLATER...

Your code works fine if it was temporary event consumer..and i have a working code with me... It is similar to your code...

But i want to solve this permanent event consumer :(

And one more info we need to register a MOF file before registering for permanent event.. I did that too..... But no luck yet....

Regards
Dheeraj Joshi
Sep 24 '09 #8
Plater
7,872 Recognized Expert Expert
I guess I don't know what a permanent event consumer is.
With slight modifications, that code will receieve remote events as long as the program is running.
Sep 24 '09 #9
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
I don't want to do re registration for events in remote machine every time when the machine stops and starts again... So i am trying to register for an event permanently(which notifies the event till it is externally unregistered.... )

Regards
Dheeraj Joshi
Sep 24 '09 #10
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
Hi... Here are few updates on my problem.... I have tried to get the event notification via SMTP too.

I am able to see the instance of SMTPEventConsumer is getting created by wbemtest tool... But even this logs the same error as i mentioned in the previous post(When it tries to give notification) . So, i seriously doubt i am missing some configuration steps.
(I do register smtpconf.dll and mof file prior to registration...

Any inputs are appreciated.............. :(

Regards
Dheeraj Joshi
Sep 25 '09 #11
sumitrawani
1 New Member
I'm trying to connect to a remote machine and receive events. I'm able to run WMI queries on the remote machine but unable to get events (using temporary consumer method).
I tried the code by platter, still can't receive any events, any updates?

Thanks,
Sumit
Jun 22 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1937
by: Dips | last post by:
Python Gurus, Is it possible to write python WMI event consumer using event handler rather than polling WMI? I got the equivalent C# code snippet from...
0
1347
by: Fazan | last post by:
I'm using Tim Golden's WMI implementation (http://tgolden.sc.sabren.com/python/wmi.html) version 0.6 along with pywin32 build 202. I'm able to successfully query WMI in the main thread but not in...
9
6300
by: | last post by:
Hi All, I have allready tried to ask a similar question , but got no answer until now. In the meantime, I found, that I cannot understand some thread-settings for the Main() function . If I use...
0
1429
by: Joe Campbell | last post by:
I am encountering a problem raising WMI events from an asp.net application. The error received (as captured in the event log) is as follows: System.Runtime.InteropServices.COMException...
3
2018
by: Sujoan | last post by:
Hi, What is WMI? Is it possible to use WMI event to capture a Toolbar button click(using RSOP_IEToolbarButton object) from a DLL(A Browser Helper Object in my project)?If so,please provide me some...
7
3317
by: gel | last post by:
Below is how it is down with vbscript. What is the best way to convert this to python? strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" &...
3
3778
by: ciffycyclops | last post by:
Hi i have written a vbscript to manage permanent event consumer classes of wmi .. here i used the NTEventLogEventConsumer class to store the specific event notification in the windows event...
1
1587
by: =?Utf-8?B?Sm9obkJhdGVz?= | last post by:
Problem: I need to backup and clear the security event log. I have this working via a vbsscript which I will post below. However while I can use this script manually it is not user friendly and...
3
2486
by: =?Utf-8?B?Sm9obkJhdGVz?= | last post by:
I'm trying to (programatically) backup and clear the security event log on the local machine. I can do this manually through the event viewer and I am logged on as an administrator. I can...
0
7202
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,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6991
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...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
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,...
1
5014
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...
0
3167
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...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
382
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...

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.