尝试使用服务器上运行的Winapi :: findFirstFile [英] Try to use Winapi::findFirstFile running on server
问题描述
我在尝试在服务器上运行Winapi::findFirstFile
时遇到问题.
我已经尝试过在类WinapiServer上复制该方法,并更改一些行,如下所示:
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)];
}
但是现在我有另一种错误 库DLL"KERNEL32"上的函数"FindFirstFileW"引发异常.
But now I have another kind of error The function 'FindFirstFileW' on the library DLL 'KERNEL32' throws an exception.
这是因为我正在x64服务器上执行该方法. 有解决这个问题的主意吗?
This is because I'm executing the method on a x64 server. Anyone with an idea for solving this problem?
推荐答案
以下是在客户端和服务器端都对我有用的示例代码.它使用.NET命名空间来获取给定模式下给定文件夹路径中的文件列表.
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.
您可以对其进行修改,以创建自己的 FindFirstFile 方法的服务器端版本.
You can modify this to create your own server side version of FindFirstFile method.
X ++代码
X++ Code
static container findMatchingFiles(
str _folderPath
, str _filePattern = '*.*')
{
System.IO.DirectoryInfo directory;
System.IO.FileInfo[] files;
System.IO.FileInfo file;
InteropPermission permission;
str fileName;
counter filesCount;
counter loop;
container mathchingFiles;
;
permission = new InteropPermission(InteropKind::ClrInterop);
permission.assert();
directory = new System.IO.DirectoryInfo(_folderPath);
files = directory.GetFiles(_filePattern);
filesCount = files.get_Length();
for (loop = 0; loop < filesCount; loop++)
{
file = files.GetValue(loop);
fileName = file.get_FullName();
mathchingFiles = conins(mathchingFiles, conlen(mathchingFiles) + 1, fileName);
}
CodeAccessPermission::revertAssert();
return mathchingFiles;
}
测试工作
Test job
要测试以上代码,我在路径C:\temp\Files\
To test the above code, I created the following sample files in the path C:\temp\Files\
我将上述方法放在一个名为 Tutorial_WinApiServer 的示例类中.然后,使用以下代码创建名为fetchFiles
的作业.
I placed the above mentioned method in a sample class named Tutorial_WinApiServer. Then, created a job named fetchFiles
with the following code.
static void fetchFiles(Args _args)
{
container files;
counter loop;
str fileName;
;
files = Tutorial_WinApiServer::findMatchingFiles(@'C:\temp\Files', '*.txt');
for (loop = 1; loop <= conlen(files); loop++)
{
fileName = conpeek(files, loop);
info(fileName);
}
}
执行作业将得到以下输出.
Executing the job gave the following output.
将文件格式更改为 F *.* 后,作业产生以下输出.
After changing the file pattern to F*.*, the job produced the following output.
希望有帮助.
这篇关于尝试使用服务器上运行的Winapi :: findFirstFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!