Note. To testing Native API/Win32 API in NETCF v2

I wrote a few code to testing call native api in NETCF v2 on PocketPC 2003 Emulator/Windows Mobile 6 Emulator/Windows Mobile 6 Device. The program run on all Windows Mobile 6 is passed,on Pocket PC 2003 Emulator is failed(I guess PPC2003 Emulator is a x86 machine so NETCF can’t correctly translation)


using System;
using System.Collections.Generic;
using System.Text;

namespace KunYiTools
{
    namespace Win32API
    {

        using System.Runtime.InteropServices;

        public sealed class Files
        {
            [DllImport("coredll.dll")]
            public static extern int CreateFile(
            string lpFileName,
            int dwDesiredAccess,
            int dwShareMode,
            int lpSecurityAttributes,
            int dwCreationDisposition,
            int dwFlagsAndAttributes,
            int hTemplateFile);

            [DllImport("coredll.dll")]
            public static extern int CloseHandle(int hObject);

        }

        public sealed class Constants4Files
        {
            public const int FILE_READ_DATA = 0x0001;     // file & pipe
            public const int FILE_LIST_DIRECTORY = 0x0001;     // directory
            public const int FILE_WRITE_DATA = 0x0002;     // file & pipe
            public const int FILE_ADD_FILE = 0x0002;     // directory
            public const int FILE_APPEND_DATA = 0x0004;     // file
            public const int FILE_ADD_SUBDIRECTORY = 0x0004;     // directory
            public const int FILE_CREATE_PIPE_INSTANCE = 0x0004;     // named pipe
            public const int FILE_READ_EA = 0x0008;     // file & directory
            public const int FILE_WRITE_EA = 0x0010;     // file & directory
            public const int FILE_EXECUTE = 0x0020;     // file
            public const int FILE_TRAVERSE = 0x0020;     // directory
            public const int FILE_DELETE_CHILD = 0x0040;     // directory
            public const int FILE_READ_ATTRIBUTES = 0x0080;     // all
            public const int FILE_WRITE_ATTRIBUTES = 0x0100;     // all
            public const int STANDARD_RIGHTS_REQUIRED = 0x000F0000;
            public const int READ_CONTROL = 0x00020000;
            public const int SYNCHRONIZE = 0x00100000;
            public const int STANDARD_RIGHTS_READ = READ_CONTROL;
            public const int STANDARD_RIGHTS_WRITE = READ_CONTROL;
            public const int STANDARD_RIGHTS_EXECUTE = READ_CONTROL;
            public const int STANDARD_RIGHTS_ALL = 0x001F0000;
            public const int SPECIFIC_RIGHTS_ALL = 0x0000FFFF;
            public const int FILE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x1FF;
            public const int FILE_GENERIC_READ = STANDARD_RIGHTS_READ |
                   FILE_READ_DATA |FILE_READ_ATTRIBUTES |
                   FILE_READ_EA | SYNCHRONIZE;
            public const int FILE_GENERIC_WRITE = STANDARD_RIGHTS_WRITE |
                   FILE_WRITE_DATA |FILE_WRITE_ATTRIBUTES |FILE_WRITE_EA |
                   FILE_APPEND_DATA | SYNCHRONIZE;
            public const int FILE_GENERIC_EXECUTE = STANDARD_RIGHTS_EXECUTE |
                   FILE_READ_ATTRIBUTES | FILE_EXECUTE | SYNCHRONIZE;
            public const int FILE_SHARE_READ = 0x00000001;
            public const int FILE_SHARE_WRITE = 0x00000002;
            public const int FILE_SHARE_DELETE = 0x00000004;
            public const int FILE_ATTRIBUTE_READONLY = 0x00000001;
            public const int FILE_ATTRIBUTE_HIDDEN = 0x00000002;
            public const int FILE_ATTRIBUTE_SYSTEM = 0x00000004;
            public const int FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
            public const int FILE_ATTRIBUTE_ARCHIVE = 0x00000020;
            public const int FILE_ATTRIBUTE_DEVICE = 0x00000040;
            public const int FILE_ATTRIBUTE_NORMAL = 0x00000080;
            public const int FILE_ATTRIBUTE_TEMPORARY = 0x00000100;
            public const int FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200;
            public const int FILE_ATTRIBUTE_REPARSE_POint = 0x00000400;
            public const int FILE_ATTRIBUTE_COMPRESSED = 0x00000800;
            public const int FILE_ATTRIBUTE_OFFLINE = 0x00001000;
            public const int FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000;
            public const int FILE_ATTRIBUTE_ENCRYPTED = 0x00004000;
            public const int FILE_NOTIFY_CHANGE_FILE_NAME = 0x00000001;
            public const int FILE_NOTIFY_CHANGE_DIR_NAME = 0x00000002;
            public const int FILE_NOTIFY_CHANGE_ATTRIBUTES = 0x00000004;
            public const int FILE_NOTIFY_CHANGE_SIZE = 0x00000008;
            public const int FILE_NOTIFY_CHANGE_LAST_WRITE = 0x00000010;
            public const int FILE_NOTIFY_CHANGE_LAST_ACCESS = 0x00000020;
            public const int FILE_NOTIFY_CHANGE_CREATION = 0x00000040;
            public const int FILE_NOTIFY_CHANGE_SECURITY = 0x00000100;
            public const int FILE_ACTION_ADDED = 0x00000001;
            public const int FILE_ACTION_REMOVED = 0x00000002;
            public const int FILE_ACTION_MODIFIED = 0x00000003;
            public const int FILE_ACTION_RENAMED_OLD_NAME = 0x00000004;
            public const int FILE_ACTION_RENAMED_NEW_NAME = 0x00000005;
            public const int  MAILSLOT_NO_MESSAGE = -1;
            public const int  MAILSLOT_WAIT_FOREVER = -1;
            public const int FILE_CASE_SENSITIVE_SEARCH = 0x00000001;
            public const int FILE_CASE_PRESERVED_NAMES = 0x00000002;
            public const int FILE_UNICODE_ON_DISK = 0x00000004;
            public const int FILE_PERSISTENT_ACLS = 0x00000008;
            public const int FILE_FILE_COMPRESSION = 0x00000010;
            public const int FILE_VOLUME_QUOTAS = 0x00000020;
            public const int FILE_SUPPORTS_SPARSE_FILES = 0x00000040;
            public const int FILE_SUPPORTS_REPARSE_POintS = 0x00000080;
            public const int FILE_SUPPORTS_REMOTE_STORAGE = 0x00000100;
            public const int FILE_VOLUME_IS_COMPRESSED = 0x00008000;
            public const int FILE_SUPPORTS_OBJECT_IDS = 0x00010000;
            public const int FILE_SUPPORTS_ENCRYPTION = 0x00020000;
            public const int FILE_NAMED_STREAMS = 0x00040000;
            public const int FILE_READ_ONLY_VOLUME = 0x00080000;
            public const int CREATE_ALWAYS = 2;
            public const int OPEN_EXISTING = 3;
        }
    }


 

use the following code to test

using KunYiTools.Win32API;

int hFile = Files.CreateFile(@"test.txt",
    Constants4Files.FILE_GENERIC_READ | Constants4Files.FILE_GENERIC_WRITE,
    Constants4Files.FILE_SHARE_READ | Constants4Files.FILE_SHARE_WRITE,
    0,
    Constants4Files.CREATE_ALWAYS,
    Constants4Files.FILE_ATTRIBUTE_NORMAL,
    0);

Files.CloseHandle(hFile);

廣告

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s

%d 位部落客按了讚: