2016-06-16 4 views
1

VS2015를 사용하여 C#에서 긴 파일 경로에 대해이 p/invoke 메서드를 실행하려고합니다. 그러나 EFileAccess EFileShare ECreationDisposition EFileAttributes는 네임 스페이스에 없습니다. FileAccess, FileShare, FileMode 및 FileAttributes로 변경하면 오류가 사라집니다. 이러한 유형은 문맥에서 상호 교환 가능합니까? 내 프로젝트에서 내가 누락 된 부분은 무엇입니까?C# P/Invoke 누락 된 형식

using System; 
using System.IO; 
using System.Runtime.InteropServices; 
using Microsoft.Win32.SafeHandles; 

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] 
internal static extern SafeFileHandle CreateFile(
    string lpFileName, EFileAccess dwDesiredAccess, EFileShare dwShareMode, 
    IntPtr lpSecurityAttributes, ECreationDisposition dwCreationDisposition, 
    EFileAttributes dwFlagsAndAttributes, IntPtr hTemplateFile); 

public static void TestCreateAndWrite(string fileName) { 
    string formattedName = @"\\?\" + fileName; 
    // Create a file with generic write access 
    SafeFileHandle fileHandle = CreateFile(formattedName, 
     EFileAccess.GenericWrite, EFileShare.None, IntPtr.Zero, 
     ECreationDisposition.CreateAlways, 0, IntPtr.Zero); 

    // Check for errors 
    int lastWin32Error = Marshal.GetLastWin32Error(); 
    if (fileHandle.IsInvalid) { 
     throw new System.ComponentModel.Win32Exception(lastWin32Error); 
    } 

    // Pass the file handle to FileStream. FileStream will close the 
    // handle 
    using (FileStream fs = new FileStream(fileHandle, 
     FileAccess.Write)) { 
     fs.WriteByte(80); 
     fs.WriteByte(81); 
     fs.WriteByte(83); 
     fs.WriteByte(84); 
    } 
} 
+0

'네임 스페이스에 잘 있지 않습니다. 당신은 그것을 정의해야합니다. 나는 한번 동등성을 확인했다는 것을 기억합니다. 일부는 동급이었고 일부는 그렇지 않았습니다. – usr

+0

하지만 그들은 kernell32.dll – Yangrui

답변

2

열거는 가져올 수 없습니다. 네이티브 DLL은 정의 할 방법이 없습니다. ABI 경계에서 열거 형은 정수입니다. 열거 형은 전적으로 .NET 개념입니다.

직접 열거 형을 정의하십시오. (실제로 이것은 어딘가에서 그것을 훔치는 것을 의미합니다.)

+0

에서 가져 오기로되어있었습니다. 고마워요! 그냥 어딘가에서 enums을 발견 ... – Yangrui