在Win7中将WriteFile写入USB [英] WriteFile to USB in Win7

查看:87
本文介绍了在Win7中将WriteFile写入USB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这个问题可能有点荒谬,因为我使用的是Borland C ++,而不是Visual Studio,但在这里。  我正在编写一个程序,使用WriteFile方法将特定数据写入USB 可移动媒体驱动器上的某个位置。
 它以特定格式写入单个字节。  这一切在Windows XP上完全正常。  但是,在Windows 7中,它没有。  我收到的错误代码是487或ERROR_INVALID_ADDRESS,我知道该位置不是无效的
地址,因为它在XP中运行正常。  我发现,如果有人使用的记忆棒没有位于其写入的特定扇区中的卷,它将会起作用。  这很好,但用户需要能够插入任何USB棒
并能够写入。  以下是我到目前为止所尝试的内容:


1。进入  administrator 模式。


2。使用Microsoft应用程序兼容性工具并将兼容性修补程序提升为"AdditiveRunAsHighest"和"AdditiveRunAsHighest"。和"AddWritePermissionsToDeviceFiles"。


3。适用于Windows XP SP 2的兼容模式。


4。完全脱离UAC。


我没有尝试过:


1。将清单文件写入RunAsHighest,uiAccess = true。  我没有尝试过这个,因为我不确定Borland是否使用了清单文件而我猜不是因为它们不在他们的帮助文件中。


如果你有任何使用WriteFile到可移动媒体的运气请发布!  任何帮助都会非常感激,因为在提升所有这些安全设置之后我无法解决这个问题。


提前致谢,


Mamoswine


 

解决方案

我认为你可以保证系统版本(x86或x64),以及类似数字类型或指针版本(x86或x64)的int。


http://stackoverflow.com/questions/3753261/ writefile-call-works-on-x86-but-not-x64-getting-error-code-6-the-handle-is


         [DllImport(" kernel32.dll")] 
         私人 静态 的extern  IntPtr的 的CreateFile(
            字符串  lpFileName的对象,
             UInt32的  dwDesiredAccess,
        &NBSP ;    UInt32的  dwShareMode,
             IntPtr的  lpSecurityAttributes,
 &NBSP ;           UInt32的  CREATE_NEW标志,
          &NBSP ;  UInt32的  dwFlagsAndAttributes,
             IntPtr的  hTemplateFile
            &NBSP);
         [的DllImport(QUOT;的Kernel32.dll")]
       &NBSP ;民营 静态 的extern 布尔  ReadFile的(
             IntPtr的  HFILE,
 &NBSP ;           byte [] lpBuffer,
              UINT  nNumberOfBytesToRead,
             REF  UINT  lpNumberOfBytesRead,
              IntPtr的  lpOverlapped的
           &NBSP );
         [的DllImport(QUOT;的Kernel32.dll")]
       &NBSP ;民营 静态 的extern 布尔 的WriteFile(
             IntPtr的  HFILE,
 &NBSP ;           byte [] lpBuffer,
              UINT  nNumberOfBytesToWrite,
             REF  UINT  lpNumberOfBytesWritten,
              IntPtr的  lpOverlapped的
           &NBSP );
         [的DllImport(QUOT; KERNEL32.DLL")]
       &NBSP ;民营 静态 的extern 布尔  CloseHandle的(
             IntPtr的  hObject
            &NBSP);
         // ---------------------------- -------------------------------------------------- -
         IntPtr  hFile;
         private  const  UInt32  GENERIC_READ = 0x80000000;
         private  const  UInt32  GENERIC_WRITE = 0x40000000;
         private  const  UInt32  OPEN_EXISTING = 3;
         private  const  Int32  INVALID_HANDLE_VALUE = -1;
         private  const  int  USB_WRITENUM = 8;
         private  const  int  USB_READNUM = 8;
         private  byte [] m_rd_data =  new  byte [USB_READNUM];
         public  byte [] rd_data
         {
             get  {  return  m_rd_data; }
             set  {m_rd_data =  value; }
        }
         private  byte [] m_wr_data =  ;新 字节[USB_WRITENUM];
         public  byte [] wr_data
         {
             get  {  return  m_wr_data; }
             set  {m_wr_data =  value; }
        }
         public  bool  OnInitUSB()
         {
             hFile =   IntPtr.Zero;
             string  deviceName =  string.Empty;
             deviceName = " your device path" ;;
           
             hFile = CreateFile(
                  deviceName,
                  GENERIC_READ | GENERIC_WRITE,
                 0,
                 IntPtr.Zero,
                  OPEN_EXISTING,
                  0,
                 IntPtr.Zero
                );
            回归  hFile.ToInt32()== INVALID_HANDLE_VALUE 假 : 真实的;
        }
         public  bool  USBDataRead()
         {
             uint  read = 0;
            返回  ReadFile的(HFILE,m_rd_data,(UINT)USB_READNUM,  REF 读  IntPtr的。零);
        }
         public  bool  USBDataWrite()
         {
             UINT 用= 0;
            返回  WriteFile的(HFILE,m_wr_data,(UINT)USB_WRITENUM,  REF 用,  IntPtr的。零);
        }
         public  void  CloseConnection()
         {
            如果 ( hFile.ToInt32()= INVALID_HANDLE_VALUE)
 !            {
                  CloseHandle的(HFILE);
                 hFile =  IntPtr.Zero;
            }
        }
    }


ref:   http://dev.tot.name/asp/html/2009028/20090208004253 .htm


这是一个C#版本代码,也许可以给你任何帮助或方向来检查你的代码。





然后我认为您可以使用"以管理员身份运行"选项运行此应用程序或切换到管理员帐户以运行此应用程序。


如果问题也无法解决,我认为如果您可以将代码分享给我。


如果有任何疑虑,请随时告诉我。


祝福,


Alright, this question may be a little absurd because I'm using Borland C++ and not Visual Studio, but here it goes.  I'm writing a program that writes specific data to a position on a USB Removable Media Drive by using the method WriteFile.  It writes individual bytes in a specific format.  This all works completely fine on Windows XP.  In Windows 7, however, it does not.  The error code I'm receiving is 487 or ERROR_INVALID_ADDRESS, which I know the location is not an invalid address since it works fine in XP.  I found out that it will work if anyone uses a memory stick that does not have a volume located in the specific sectors that it's writing to.  And that's good, but the user needs to be able to plug in any USB stick and be able to write.  Here is what I've tried so far:

1. Ran in administrator mode.

2. Used the Microsoft Application Compatibility Tool and elevated the compatibility fixes to "AdditiveRunAsHighest" and "AddWritePermissionsToDeviceFiles".

3. Ran in Compatibility Mode for Windows XP SP 2.

4. Completely taken off UAC.

What I haven't tried:

1. Writing a manifest file to RunAsHighest and uiAccess = true.  I haven't tried this because I'm unsure if Borland uses manifest files and I'm guessing not because they're not in their help files.

If you've had any luck with using WriteFile to removable media please post!  Any help would be greatly appreciated since I have no clue to fix this after elevating all these security settings.

Thanks in Advance,

Mamoswine

 

解决方案

I think you can ensure the System version(x86 or x64), and the int like numeric type or pointer version(x86 or x64).

http://stackoverflow.com/questions/3753261/writefile-call-works-on-x86-but-not-x64-getting-error-code-6-the-handle-is

        [DllImport("kernel32.dll")]
        private static extern IntPtr CreateFile(
            String lpFileName,
            UInt32 dwDesiredAccess,
            UInt32 dwShareMode,
            IntPtr lpSecurityAttributes,
            UInt32 dwCreationDisposition,
            UInt32 dwFlagsAndAttributes,
            IntPtr hTemplateFile
            );
        [DllImport("Kernel32.dll")]
        private static extern bool ReadFile(
            IntPtr hFile,
            byte[] lpBuffer,
            uint nNumberOfBytesToRead,
            ref uint lpNumberOfBytesRead,
            IntPtr lpOverlapped
            );
        [DllImport("Kernel32.dll")]
        private static extern bool WriteFile(
            IntPtr hFile,
            byte[] lpBuffer,
            uint nNumberOfBytesToWrite,
            ref uint lpNumberOfBytesWritten,
            IntPtr lpOverlapped
            );
        [DllImport("kernel32.dll")]
        private static extern bool CloseHandle(
            IntPtr hObject
            );
        //--------------------------------------------------------------------------------
        IntPtr hFile;
        private const UInt32 GENERIC_READ = 0x80000000;
        private const UInt32 GENERIC_WRITE = 0x40000000;
        private const UInt32 OPEN_EXISTING = 3;
        private const Int32 INVALID_HANDLE_VALUE = -1;
        private const int USB_WRITENUM = 8;
        private const int USB_READNUM = 8;
        private byte[] m_rd_data = new byte[USB_READNUM];
        public byte[] rd_data
        {
            get { return m_rd_data; }
            set { m_rd_data = value; }
        }
        private byte[] m_wr_data = new byte[USB_WRITENUM];
        public byte[] wr_data
        {
            get { return m_wr_data; }
            set { m_wr_data = value; }
        }
        public bool OnInitUSB()
        {
            hFile = IntPtr.Zero;
            string deviceName = string.Empty;
            deviceName = "your device path";
           
            hFile = CreateFile(
                deviceName,
                GENERIC_READ | GENERIC_WRITE,
                0,
                IntPtr.Zero,
                OPEN_EXISTING,
                0,
                IntPtr.Zero
                );
            return hFile.ToInt32() == INVALID_HANDLE_VALUE ? false : true;
        }
        public bool USBDataRead()
        {
            uint read = 0;
            return ReadFile(hFile, m_rd_data, (uint)USB_READNUM, ref read, IntPtr.Zero);
        }
        public bool USBDataWrite()
        {
            uint written = 0;
            return WriteFile(hFile, m_wr_data, (uint)USB_WRITENUM, ref written, IntPtr.Zero);
        }
        public void CloseConnection()
        {
            if (hFile.ToInt32() != INVALID_HANDLE_VALUE)
            {
                CloseHandle(hFile);
                hFile = IntPtr.Zero;
            }
        }
    }

ref: http://dev.tot.name/asp/html/2009028/20090208004253.htm

This is a C# version code, maybe can give you any help or the direction to check your code.


Then I think you can use Run as Administrator option to run this application or switch to the Administrator account to run this application.

If the problem also cannot solved, I think it will be helpful if you can share the code to me.

If there's any concern, please feel free to let me know.

Best wishes,


这篇关于在Win7中将WriteFile写入USB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆