This is yet another example of the use of CLR Interop from within Dynamics Ax. This time, we're going to retrieve the current state of a Windows service (Spooler, Telnet, ...) on any given Windows machine.
I'm using the ServiceController class from the .NET framework for this. For this example to work, make sure you have added a reference in the AOT to the System.ServiceProcess assembly.
static boolean GetServiceState(str _servicename,str _machinename)
{ System.ServiceProcess.ServiceController ServiceController;
System.ServiceProcess.ServiceControllerStatus ServiceStatus;
str myStatus;
InteropPermission permission = new InteropPermission(InteropKind::ClrInterop);
;
if(!_servicename !_machinename)
return false;
try
{
permission.assert();
ServiceController= new System.ServiceProcess.ServiceController(_servicename,_machinename);
ServiceStatus=ServiceController.get_Status();
myStatus=ServiceStatus.ToString();
if(myStatus=='Running')
return true;
} catch
{
}
return false;
}
Make sure you have appropriate rights for executing this code.
This code can be modified to start and stop services as well.
Showing posts with label CLR Interop. Show all posts
Showing posts with label CLR Interop. Show all posts
Tuesday, August 18, 2009
Friday, June 26, 2009
Email from Ax by CLR interop
In my previous post, I talked about the CLR Interop possibilities from Dynamics Ax. As you might know, CLR interop is used to get access to assemblies installed with the .NET Framework, or even the assemblies you create on your own with Visual Studio.
Here's another example. This time, we are going to send an email from Ax with it. (Yes I know, this kind of functionalities are provided with Ax out-of-the-box as well.)
void Email()
{ System.Net.Mail.MailMessage myMailMessage;
System.Net.Mail.SmtpClient myMail;
System.Net.Mail.MailAddress myMailFrom;
System.Net.Mail.MailAddress myMailTo;
System.Net.NetworkCredential myNC;
str mySubject="Mail from Ax 2009";
str myMailBody="The email body goes here";
str myMailFromStr="youremail@yourdomain.com";
str myMailToStr="youremail@yourdomain.com";
str mysmtpServer="yourmailservername";
str mylogin="loginname";
str mypassword="loginpassword";
;
myMailFrom = new System.Net.Mail.MailAddress(myMailFromStr,myMailFromStr);
myMailTo = new System.Net.Mail.MailAddress(myMailToStr,myMailToStr);
myMailMessage = new System.Net.Mail.MailMessage(myMailFrom,myMailTo);
myMailmessage.set_Subject(mySubject);
myMailmessage.set_Body(myMailBody);
myMail = new System.Net.Mail.SmtpClient(mysmtpServer);
myNC= new System.Net.NetworkCredential(mylogin,mypassword);
myMail.set_Credentials(myNC);
myMail.Send(myMailmessage);
}
Supplying of the network credentials is optional. If not specified, the default network credentials will be used. Depending on your email server settings (relay on/off), only emails to the local domain are allowed.
This example can be extended, to include file attachments for example.
Greetingz,
Willy
Here's another example. This time, we are going to send an email from Ax with it. (Yes I know, this kind of functionalities are provided with Ax out-of-the-box as well.)
void Email()
{ System.Net.Mail.MailMessage myMailMessage;
System.Net.Mail.SmtpClient myMail;
System.Net.Mail.MailAddress myMailFrom;
System.Net.Mail.MailAddress myMailTo;
System.Net.NetworkCredential myNC;
str mySubject="Mail from Ax 2009";
str myMailBody="The email body goes here";
str myMailFromStr="youremail@yourdomain.com";
str myMailToStr="youremail@yourdomain.com";
str mysmtpServer="yourmailservername";
str mylogin="loginname";
str mypassword="loginpassword";
;
myMailFrom = new System.Net.Mail.MailAddress(myMailFromStr,myMailFromStr);
myMailTo = new System.Net.Mail.MailAddress(myMailToStr,myMailToStr);
myMailMessage = new System.Net.Mail.MailMessage(myMailFrom,myMailTo);
myMailmessage.set_Subject(mySubject);
myMailmessage.set_Body(myMailBody);
myMail = new System.Net.Mail.SmtpClient(mysmtpServer);
myNC= new System.Net.NetworkCredential(mylogin,mypassword);
myMail.set_Credentials(myNC);
myMail.Send(myMailmessage);
}
Supplying of the network credentials is optional. If not specified, the default network credentials will be used. Depending on your email server settings (relay on/off), only emails to the local domain are allowed.
This example can be extended, to include file attachments for example.
Greetingz,
Willy
Subscribe to:
Posts (Atom)