LeaderBoard

WinAPI Static Methods in Dynamics Ax

 

I’ll explore the WinAPI class in AX. It is a great class, because it has a lot of Static methods that access, like the name suggestions the Windows API.

FileExists()
One of the methods I have used a lot is the WinAPI::FileExists(). This method is returns a boolean value stating if the past in file name and path exists. Some sample code would look like:
if(WinAPI::fileExists("c:\\temp\\test.txt"))
{
// preform some code
}

ShellExecute()

This method take variables, and based on what you send will launch as application, through the OS shell execute.
If anyone has every done any windows development, prior to or outside of X++, then most likely you have used the ShellExecute before.
Below is sample code that shows you how to launch IE, and feed IE a web site to launch, which happens to be a static HTML page in my c:\temp folder.
server static void WinAPITest(Args _args)
{
;
// WinAPI::shellExecute() returns an int value. 1 being that everything launched ok, 0 being a problem. (usually path, etc.)
// WinAPI::shellExecute(FileName _lpFile,[str _lpParameters, str _lpDirectory, str _lpOperation, int _show]);
// example
WinAPI::shellExecute("explorer.exe","c:\\temp\\TempInv.html","c:\\","",1);
}
If you copy and paste the above into a job, you will see the comments, and see it run like a champ! Of course instead of sending the static HTML page to launch you could also send an UNC path, or http:// path.

CopyFile() Method

This is a great little method, that came in handy for me today, when I had to copy files to a location from within X++. The Method looks something like this:
WinAPI::copyFile(str _strFile, str _newfile, [boolean _overwrite])
With Example as:
WinAPI::copyFile("c:\\test.txt","c:\\temp\\test.txt",true);

SndPlaySound()

Ever wanted to give a audio based interaction, to when a user does something? In the past windows based applications incorporated sound to signal that a process what complete.
Now you can add sounds to your methods, and processes by using the WinAPI call: WinAPI::sndPlaySound()
//example
WinAPI::sndPlaySound("c:\\horay.wav");
This is a little simple, trivial type of method to some. Put if AX is being used in the production process, and you have screen's at different processes then having sound to signal that a barcode scan has completed correctly helps in a loud Manufacturing environment. I also assume you could accomplish the same thing through ActiveX and MIDI control. I am not sure what would be the best way, for performance, to play a sound, but for development cost, the above is pretty easy and straight forward.

No comments:

Post a Comment