I have a problem trying to run Winapi::findFirstFile
running on server.
I havve already tried copying the method on the class WinapiServer, and changing some lines, like this:
server static container findFirstFile(str filename)
{
InteropPermission interopPerm;
Binary data;
DLL _winApiDLL;
DLLFunction _findFirstFile;
;
interopPerm = new InteropPermission(InteropKind::DllInterop);
interopPerm.assert();
data = new Binary(592); // size of WIN32_FIND_DATA when sizeof(TCHAR)==2
_winApiDLL = new DLL(#KernelDLL);
_findFirstFile = new DLLFunction(_winApiDLL, 'FindFirstFileW');
_findFirstFile.returns(ExtTypes::DWord);
_findFirstFile.arg(ExtTypes::WString,ExtTypes::Pointer);
return [_findFirstFile.call(filename, data),data.wString(#offset44)];
}
But now I have another kind of error The function 'FindFirstFileW' on the library DLL 'KERNEL32' throws an exception.
This is because I'm executing the method on a x64 server. Anyone with an idea for solving this problem?
I've found that .NET classes expect a System.String instead of a str. Refering to:
When compiling the CIL I get:
My solution is to assign _folderPath to a System.String.
Maybe it's better to use the .NET framework?
http://greg.agiletortoise.com/2007/04/02/dynamics-ax-making-net-calls-from-inside-ax/
Check if someone else has done this for you.
Here is a sample code that has worked for me in both client side as well as server side. It uses .NET namespaces to fetch the list of files in a given folder path for a given pattern.
You can modify this to create your own server side version of FindFirstFile method.
X++ Code
Test job
To test the above code, I created the following sample files in the path
C:\temp\Files\
I placed the above mentioned method in a sample class named Tutorial_WinApiServer. Then, created a job named
fetchFiles
with the following code.Executing the job gave the following output.
After changing the file pattern to F*.*, the job produced the following output.
Hope that helps.