每次使用Win32 API 时都要声明,还要查询常量定义,很麻烦
不知有没有人用.net封装过Win32 API?
在网上找了下,没发现有第三方的类库

解决方案 »

  1.   

    API的东西实在太多了- -!需要用什么自己写什么
      

  2.   

    有些以前必须用API的,现在也可以用.NET自带的类实现相应的功能,但全部封装,怕只有MS自己有这个功夫了
      

  3.   

    以前有很多VB的API库,你去艘艘看,搜到了自己写个转换程序?也算你为了C#作了贡献了
      

  4.   

    以前有很多VB的API库,你去艘艘看,搜到了自己写个转换程序??你是指VB.NET吗?如果是VB6.0,那跟win32 api不是一样吗?
    如果是VB.NET,那还需要转换吗?本质上都是MSIL不知道你说的转换是什么意思本想写个工具来实现。没弄好。搁浅,有时间再弄====》什么意思,兄弟,你不能放弃啊,群众眼睁睁的看着你啊!
      

  5.   

    明显10楼说的那个VB的API应该是那个API帮助文档..
      

  6.   

    VB6.0很多API参考工具,有数据库的也有网页的
    道理都一样替换一下语法就好了
      

  7.   

    我个人建议
    你还是碰到问题自己去写API得好,用VB6时代的API 工具也很方便的
    这样所有你用过的东西都积累下来都会成为你自己的
    你如果去找现成的,没啥意思
      

  8.   

    我只用到一部分,就只做了一点点namespace BaseFrame.ShareFunction
    {
        using System;
        using System.ComponentModel;
        using System.Diagnostics;
        using System.Runtime.InteropServices;
        using System.Text;    public sealed class Win32Api
        {
            public const int CSIDL_ADMINTOOLS = 0x30;
            public const int CSIDL_ALTSTARTUP = 0x1d;
            public const int CSIDL_APPDATA = 0x1a;
            public const int CSIDL_BITBUCKET = 10;
            public const int CSIDL_CDBURN_AREA = 0x3b;
            public const int CSIDL_COMMON_ADMINTOOLS = 0x2f;
            public const int CSIDL_COMMON_ALTSTARTUP = 30;
            public const int CSIDL_COMMON_APPDATA = 0x23;
            public const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x19;
            public const int CSIDL_COMMON_DOCUMENTS = 0x2e;
            public const int CSIDL_COMMON_FAVORITES = 0x1f;
            public const int CSIDL_COMMON_MUSIC = 0x35;
            public const int CSIDL_COMMON_OEM_LINKS = 0x3a;
            public const int CSIDL_COMMON_PICTURES = 0x36;
            public const int CSIDL_COMMON_PROGRAMS = 0x17;
            public const int CSIDL_COMMON_STARTMENU = 0x16;
            public const int CSIDL_COMMON_STARTUP = 0x18;
            public const int CSIDL_COMMON_TEMPLATES = 0x2d;
            public const int CSIDL_COMMON_VIDEO = 0x37;
            public const int CSIDL_COMPUTERSNEARME = 0x3d;
            public const int CSIDL_CONNECTIONS = 0x31;
            public const int CSIDL_CONTROLS = 3;
            public const int CSIDL_COOKIES = 0x21;
            public const int CSIDL_DESKTOP = 0;
            public const int CSIDL_DESKTOPDIRECTORY = 0x10;
            public const int CSIDL_DRIVES = 0x11;
            public const int CSIDL_FAVORITES = 6;
            public const int CSIDL_FLAG_CREATE = 0x8000;
            public const int CSIDL_FLAG_DONT_VERIFY = 0x4000;
            public const int CSIDL_FLAG_MASK = 0xff00;
            public const int CSIDL_FLAG_NO_ALIAS = 0x1000;
            public const int CSIDL_FLAG_PER_USER_INIT = 0x800;
            public const int CSIDL_FONTS = 20;
            public const int CSIDL_HISTORY = 0x22;
            public const int CSIDL_INTERNET = 1;
            public const int CSIDL_INTERNET_CACHE = 0x20;
            public const int CSIDL_LOCAL_APPDATA = 0x1c;
            public const int CSIDL_MYDOCUMENTS = 12;
            public const int CSIDL_MYMUSIC = 13;
            public const int CSIDL_MYPICTURES = 0x27;
            public const int CSIDL_MYVIDEO = 14;
            public const int CSIDL_NETHOOD = 0x13;
            public const int CSIDL_NETWORK = 0x12;
            public const int CSIDL_PERSONAL = 5;
            public const int CSIDL_PRINTERS = 4;
            public const int CSIDL_PRINTHOOD = 0x1b;
            public const int CSIDL_PROFILE = 40;
            public const int CSIDL_PROGRAM_FILES = 0x26;
            public const int CSIDL_PROGRAM_FILES_COMMON = 0x2b;
            public const int CSIDL_PROGRAM_FILES_COMMONX86 = 0x2c;
            public const int CSIDL_PROGRAM_FILESX86 = 0x2a;
            public const int CSIDL_PROGRAMS = 2;
            public const int CSIDL_RECENT = 8;
            public const int CSIDL_RESOURCES = 0x38;
            public const int CSIDL_RESOURCES_LOCALIZED = 0x39;
            public const int CSIDL_SENDTO = 9;
            public const int CSIDL_STARTMENU = 11;
            public const int CSIDL_STARTUP = 7;
            public const int CSIDL_SYSTEM = 0x25;
            public const int CSIDL_SYSTEMX86 = 0x29;
            public const int CSIDL_TEMPLATES = 0x15;
            public const int CSIDL_WINDOWS = 0x24;        [DllImport("Kernel32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
            public static extern uint GetLastError();
            [DllImport("kernel32")]
            public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
            [DllImport("kernel32")]
            public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
            [DllImport("kernel32")]
            public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo);
            [DllImport("kernel32")]
            public static extern void GetWindowsDirectory(StringBuilder WinDir, int count);
            [DllImport("kernel32")]
            public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
            public static void memcpy(byte[] pDest, uint nDestStart, byte[] pSource, uint nSourceStart, uint nCount)
            {
                while (nCount > 0)
                {
                    pDest[nDestStart++] = pSource[nSourceStart++];
                    nCount--;
                }
            }        public static void memset(byte[] buf, byte newValue)
            {
                if (buf != null)
                {
                    for (int i = 0; i < buf.Length; i++)
                    {
                        buf[i] = newValue;
                    }
                }
            }        [DllImport("shell32", CharSet=CharSet.Auto)]
            public static extern uint SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, uint dwFlags, StringBuilder pszPath);
            public static void showWin32Error(int errorcode)
            {
                Win32Exception exception = new Win32Exception(errorcode);
                Trace.WriteLine("Error code:\t 0x" + exception.ErrorCode.ToString());
                Trace.WriteLine("Error message:\t " + exception.Message);
            }        [StructLayout(LayoutKind.Sequential)]
            public struct CPU_INFO
            {
                public uint dwOemId;
                public uint dwPageSize;
                public uint lpMinimumApplicationAddress;
                public uint lpMaximumApplicationAddress;
                public uint dwActiveProcessorMask;
                public uint dwNumberOfProcessors;
                public uint dwProcessorType;
                public uint dwAllocationGranularity;
                public uint dwProcessorLevel;
                public uint dwProcessorRevision;
            }        [StructLayout(LayoutKind.Sequential)]
            public struct MEMORY_INFO
            {
                public uint dwLength;
                public uint dwMemoryLoad;
                public uint dwTotalPhys;
                public uint dwAvailPhys;
                public uint dwTotalPageFile;
                public uint dwAvailPageFile;
                public uint dwTotalVirtual;
                public uint dwAvailVirtual;
            }        public enum SHGFP_TYPE
            {
                SHGFP_TYPE_CURRENT,
                SHGFP_TYPE_DEFAULT
            }        [StructLayout(LayoutKind.Sequential)]
            public struct SYSTEMTIME_INFO
            {
                public ushort wYear;
                public ushort wMonth;
                public ushort wDayOfWeek;
                public ushort wDay;
                public ushort wHour;
                public ushort wMinute;
                public ushort wSecond;
                public ushort wMilliseconds;
            }
        }
    }
      

  9.   

    你贴上来的,好多都是.Net提供的,没必要用API
      

  10.   

    期待8楼,
    有没有c#版的API函数参考手册也行啊,全是vb的,
      

  11.   

    http://www.pinvoke.net/
    谁下载了?
    麻烦给我发到邮箱[email protected](只要dll就行了)
      

  12.   

    [SuppressUnmanagedCodeSecurity, HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort=true)]
    internal static class UnsafeNativeMethods
    {
        // Fields
        public const int FILE_ACTION_ADDED = 1;
        public const int FILE_ACTION_MODIFIED = 3;
        public const int FILE_ACTION_REMOVED = 2;
        public const int FILE_ACTION_RENAMED_NEW_NAME = 5;
        public const int FILE_ACTION_RENAMED_OLD_NAME = 4;
        public const int FILE_ADD_FILE = 2;
        public const int FILE_ADD_SUBDIRECTORY = 4;
        public const int FILE_APPEND_DATA = 4;
        public const int FILE_ATTRIBUTE_ARCHIVE = 0x20;
        public const int FILE_ATTRIBUTE_COMPRESSED = 0x800;
        public const int FILE_ATTRIBUTE_DIRECTORY = 0x10;
        public const int FILE_ATTRIBUTE_HIDDEN = 2;
        public const int FILE_ATTRIBUTE_NORMAL = 0x80;
        public const int FILE_ATTRIBUTE_OFFLINE = 0x1000;
        public const int FILE_ATTRIBUTE_READONLY = 1;
        public const int FILE_ATTRIBUTE_SYSTEM = 4;
        public const int FILE_ATTRIBUTE_TEMPORARY = 0x100;
        public const int FILE_CASE_PRESERVED_NAMES = 2;
        public const int FILE_CASE_SENSITIVE_SEARCH = 1;
        public const int FILE_CREATE_PIPE_INSTANCE = 4;
        public const int FILE_DELETE_CHILD = 0x40;
        public const int FILE_EXECUTE = 0x20;
        public const int FILE_FILE_COMPRESSION = 0x10;
        public const int FILE_FLAG_BACKUP_SEMANTICS = 0x2000000;
        public const int FILE_FLAG_DELETE_ON_CLOSE = 0x4000000;
        public const int FILE_FLAG_NO_BUFFERING = 0x20000000;
        public const int FILE_FLAG_OVERLAPPED = 0x40000000;
        public const int FILE_FLAG_POSIX_SEMANTICS = 0x1000000;
        public const int FILE_FLAG_RANDOM_ACCESS = 0x10000000;
        public const int FILE_FLAG_SEQUENTIAL_SCAN = 0x8000000;
        public const int FILE_FLAG_WRITE_THROUGH = -2147483648;
        public const int FILE_LIST_DIRECTORY = 1;
        public const int FILE_NOTIFY_CHANGE_ATTRIBUTES = 4;
        public const int FILE_NOTIFY_CHANGE_CREATION = 0x40;
        public const int FILE_NOTIFY_CHANGE_DIR_NAME = 2;
        public const int FILE_NOTIFY_CHANGE_FILE_NAME = 1;
        public const int FILE_NOTIFY_CHANGE_LAST_ACCESS = 0x20;
        public const int FILE_NOTIFY_CHANGE_LAST_WRITE = 0x10;
        public const int FILE_NOTIFY_CHANGE_SECURITY = 0x100;
        public const int FILE_NOTIFY_CHANGE_SIZE = 8;
        public const int FILE_PERSISTENT_ACLS = 8;
        public const int FILE_READ_ATTRIBUTES = 0x80;
        public const int FILE_READ_DATA = 1;
        public const int FILE_READ_EA = 8;
        public const int FILE_SHARE_DELETE = 4;
        public const int FILE_SHARE_READ = 1;
        public const int FILE_SHARE_WRITE = 2;
        public const int FILE_TRAVERSE = 0x20;
        public const int FILE_TYPE_CHAR = 2;
        public const int FILE_TYPE_DISK = 1;
        public const int FILE_TYPE_PIPE = 3;
        public const int FILE_TYPE_REMOTE = 0x8000;
        public const int FILE_TYPE_UNKNOWN = 0;
        public const int FILE_UNICODE_ON_DISK = 4;
        public const int FILE_VOLUME_IS_COMPRESSED = 0x8000;
        public const int FILE_WRITE_ATTRIBUTES = 0x100;
        public const int FILE_WRITE_DATA = 2;
        public const int FILE_WRITE_EA = 0x10;
        public const int GetFileExInfoStandard = 0;
        public const int OPEN_ALWAYS = 4;
        public const int OPEN_EXISTING = 3;    // Methods
        [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
        internal static extern bool ClearCommBreak(SafeFileHandle hFile);
        [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
        internal static extern bool ClearCommError(SafeFileHandle hFile, ref int lpErrors, IntPtr lpStat);
        [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
        internal static extern bool ClearCommError(SafeFileHandle hFile, ref int lpErrors, ref COMSTAT lpStat);
        [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
        public static extern bool ClearEventLog(SafeHandle hEventLog, HandleRef lpctstrBackupFileName);
        [DllImport("ole32.dll")]
        internal static extern int CoGetMarshalSizeMax(out int pulSize, ref Guid riid, IntPtr pv, int dwDestContext, IntPtr pvDestContext, int mshlflags);
        [DllImport("ole32.dll")]
        internal static extern int CoGetStandardMarshal(ref Guid riid, IntPtr pv, int dwDestContext, IntPtr pvDestContext, int mshlflags, out IntPtr ppMarshal);
        [DllImport("ole32.dll")]
        internal static extern int CoMarshalInterface([MarshalAs(UnmanagedType.Interface)] object pStm, ref Guid riid, IntPtr pv, int dwDestContext, IntPtr pvDestContext, int mshlflags);
        [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
        internal static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, IntPtr securityAttrs, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
        [DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
        public static extern IntPtr CreateWindowEx(int exStyle, string lpszClassName, string lpszWindowName, int style, int x, int y, int width, int height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, [MarshalAs(UnmanagedType.AsAny)] object pvParam);
        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern IntPtr DefWindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern bool DestroyWindow(HandleRef hWnd);
        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern int DispatchMessage([In] ref NativeMethods.MSG msg);
        [DllImport("kernel32.dll", SetLastError=true)]
        internal static extern bool EscapeCommFunction(SafeFileHandle hFile, int dwFunc);
        [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
        internal static extern bool FlushFileBuffers(SafeFileHandle hFile);
        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern bool GetClassInfo(HandleRef hInst, string lpszClass, [In, Out] NativeMethods.WNDCLASS_I wc);
        [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
        internal static extern bool GetCommModemStatus(SafeFileHandle hFile, ref int lpModemStat);
        [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
        internal static extern bool GetCommProperties(SafeFileHandle hFile, ref COMMPROP lpCommProp);
        [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
        internal static extern bool GetCommState(SafeFileHandle hFile, ref DCB lpDCB);
        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern IntPtr GetDC(HandleRef hWnd);
        [DllImport("kernel32.dll", SetLastError=true)]
        internal static extern int GetFileType(SafeFileHandle hFile);
        [DllImport("version.dll", CharSet=CharSet.Auto)]
        public static extern bool GetFileVersionInfo(string lptstrFilename, int dwHandle, int dwLen, HandleRef lpData);
        [DllImport("version.dll", CharSet=CharSet.Auto, SetLastError=true)]
        public static extern int GetFileVersionInfoSize(string lptstrFilename, out int handle);
        [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
        public static extern int GetModuleFileName(HandleRef hModule, StringBuilder buffer, int length);
        [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
        public static extern IntPtr GetModuleHandle(string modName);
        [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
        public static extern bool GetNumberOfEventLogRecords(SafeHandle hEventLog, out int count);
        [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
        public static extern bool GetOldestEventLogRecord(SafeHandle hEventLog, int[] number);
        [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
        internal static extern unsafe bool GetOverlappedResult(SafeFileHandle hFile, NativeOverlapped* lpOverlapped, ref int lpNumberOfBytesTransferred, bool bWait);
      

  13.   

    贴不下了。你自己反射看.net的就可以了。