2011-11-28 22 views
5

Tôi gặp sự cố khi cố gắng chạy Winapi::findFirstFile chạy trên máy chủ. tôi havve đã cố gắng sao chép các phương pháp trên WinapiServer lớp, và thay đổi một số dòng, như thế này:Hãy thử sử dụng Winapi :: findFirstFile chạy trên máy chủ

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)]; 
} 

Nhưng bây giờ tôi có một loại lỗi Chức năng 'FindFirstFileW' trên thư viện DLL 'kernel32' ném một ngoại lệ.

Điều này là do tôi đang thực hiện phương pháp trên máy chủ x64. Bất kỳ ai có ý tưởng giải quyết vấn đề này?

+0

Bạn đã chỉ định thông báo lỗi chính xác từ nhật ký thông tin chưa? –

+0

Ngoại lệ của nó là gì? Có lẽ AX ném, vì WinAPI không ném ngoại lệ, nó trả về mã lỗi. Hãy thử 'WinAPIServer :: getLastError()' để lấy mã lỗi –

Trả lời

6

Đây là mã mẫu đã làm việc cho tôi ở cả phía máy khách cũng như phía máy chủ. Nó sử dụng các không gian tên .NET để lấy danh sách các tệp trong một đường dẫn thư mục đã cho cho một mẫu đã cho.

Bạn có thể sửa đổi điều này để tạo phiên bản phía máy chủ của riêng mình là phương thức FindFirstFile.

X ++ Mã

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; 
} 

việc thử nghiệm

Để kiểm tra mã trên, tôi tạo ra các tập tin mẫu sau trong đường dẫn C:\temp\Files\

List of files

Tôi đã đặt phương thức được đề cập ở trên trong một lớp mẫu có tên Tutorial_WinApiServer. Sau đó, tạo công việc có tên là fetchFiles bằng mã sau.

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); 
    } 
} 

Thực hiện công việc đã cho kết quả sau.

Job Output 1

Sau khi thay đổi mô hình tập tin để F *. *, công việc sản xuất đầu ra sau đây.

Job Output 2

Hy vọng điều đó sẽ hữu ích.

0

Tôi đã tìm thấy rằng các lớp .NET mong đợi một System.String thay vì một str.Đề cập đến:

directory = new System.IO.DirectoryInfo(_folderPath); 

Khi biên dịch các CIL tôi nhận được:

Không thể tạo một bản ghi trong thông tin Compiler (TmpCompilerOutput). Đường dẫn: \ Classes \\, Cảnh báo: Không tìm thấy proxy. Nhập FileIOPermission được tìm thấy trên ngăn xếp. Mã này trong Class:, Phương pháp:.

Giải pháp của tôi là gán _folderPath vào System.String.

Các vấn đề liên quan