能否具体一点,介绍一下用哪几个内存操作的API函数?

解决方案 »

  1.   

    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    就有他然后用string读他
      

  2.   

    你是怎么声请的?如果是GMEM_MOVEABLE
    还需要用GlobalLock得到地址
      

  3.   

    m_MemHandle = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, m_MemSize)
    m_MemPointer = GlobalLock(m_MemHandle)
    ...............
    Dim N As Long  '从文件中读出的数据字节数
    'Read the file.ReadFile handles reading the data into a global memory block which we have a long pointer value to.
    Throw = ReadFile(m_FilePointer, m_MemPointer, Size, N, 0&)///////////////////////////////////////////////////////
    结果不能将文件读入m_MemPointer指定的内存区域,为什么?
      

  4.   

    用CopyMemory复制内存到变量,再用它复制变量到内存或 修改数组的SafeArray结构,使其指向内存,具体可参考:
    http://www.csdn.net/develop/author/netauthor/AdamBear/
      

  5.   

    -----------------------------文件1-----------------------------
    ’-----------------------C_APIFILE类-----------------------------------
    Option Explicit
    '**********************  API函数声明  ******************************************************************************************************************************************************************************************************************************************************************************************
    Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
    Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Any) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
    'SetFilePoniter移动文件指针,该函数VB的声明如下:
    Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, ByVal lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
    '*********************** 常数 *********************************************************************************Private Const GENERIC_READ = &H80000000
    Private Const GENERIC_WRITE = &H40000000
    Private Const CREATE_NEW = 1
    Private Const OPEN_EXISTING = 3
    Private Const FILE_ATTRIBUTE_NORMAL = &H80'*********************** 类型 *********************************************************************************
    Private Type Overlapped
            Internal As Long
            InternalHigh As Long
            offset As Long
            OffsetHigh As Long
            hEvent As Long
    End TypePrivate Type SECURITY_ATTRIBUTES
            nLength As Long
            lpSecurityDescriptor As Long
            bInheritHandle As Long
    End Type
    '**************        全局变量    ***********************************************************************************************
    Dim m_FileName As String   '文件名称
    Dim m_FilePointer As Long  '文件指针
    '********************************************************************************************************************************************************************************************************************************************************************************************************************************
    '**********************************************************************************************
    'FileName 属性设置并取回要操作的文件名
    '**********************************************************************************************
    Public Property Let FileName(File_Name As String)
       m_FileName = File_Name
    End PropertyPublic Property Get FileName() As String
       FileName = m_FileName
    End Property'**********************************************************************************************
    'FilePointer 属性设置并取回指向文件的指针
    '**********************************************************************************************
    Public Property Let FilePointer(File_Pointer As String)
       m_FilePointer = File_Pointer
    End PropertyPublic Property Get FilePointer() As String
       FilePointer = m_FilePointer
    End Property'***************************************************************************************************
    'OpenFile方法使用CreateFile函数,用OPEN_EXISTING 标志来标识当前文件已存在。它返回一个指向打开文件的指针
    '***************************************************************************************************
    Public Function OpenFile()
       Dim Security As SECURITY_ATTRIBUTES
       m_FilePointer = CreateFile(m_FileName, GENERIC_READ Or GENERIC_WRITE, 0&, Security, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&)
    End Function
    '***************************************************************************************************
    'APIRead函数将Size指定的字节数读入Pointer参数指向的内存块。
    '***************************************************************************************************
    Public Function APIRead(Pointer As Long, Size As Long)
       Dim Throw As Long
       Dim N As Long  '从文件中读出的数据字节数
       'Read the file.ReadFile handles reading the data into a global memory block which we have a long pointer value to.
       Throw = ReadFile(m_FilePointer, Pointer, Size, N, 0&)
    End Function
    '***************************************************************************************************
    'APIWrite函数将Size指定的字节数从Pointer参数指向的内存块中取出。
    '***************************************************************************************************
    Public Function APIWrite(Pointer As Long, Size As Long)
       Dim Throw As Long
       Dim N As Long '写入文件的数据字节数
       'Write the file.WriteFile handles reading the data into a global memory block which we have a long pointer value to.
       Throw = WriteFile(m_FilePointer, Pointer, Size, N, 0&)
    End Function'***************************************************************************************************
    'FileCreate 方法利用CREATE_NEW 标志建立一个新文件。可与使用OPEN_EXISTING标志作比较
    '***************************************************************************************************
    Public Function FileCreate()
    'CreateFile uses the CREATE_NEW constant to create a new file.
       Dim Security As SECURITY_ATTRIBUTES
       m_FilePointer = CreateFile(m_FileName, GENERIC_READ Or GENERIC_WRITE, 0&, Security, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0&)
    End Function
    '***************************************************************************************************
    'CloseFile利用CloseHandle函数来关闭指定的文件
    '***************************************************************************************************
    Public Function CloseFile()
       Dim Throw As Long
       Throw = CloseHandle(m_FilePointer)
    End Function'***************************************************************************************************
    'GetSize方法调用GetFileSize来确定打开的文件的大小,这样才能给读入的文件数据分配大小合适的内存块
    '***************************************************************************************************
    Public Function GetSize() As Long
       'Return the size of the file.Note that for huge file sizes,the second parameter of the function would have to be used.
       GetSize = GetFileSize(m_FilePointer, 0)
    End Function'***************************************************************************************************
    'Delete 方法调用DeleteFile删除指定的文件
    '***************************************************************************************************
    Public Function Delete()
      Dim Throw As Long
      'Delete the specified file
      Throw = DeleteFile(m_FileName)
    End Function
      

  6.   

    -----------------------------文件2-----------------------------
    ’-----------------------C_APIMEMORY类-----------------------------------
    Option Explicit'-----------------API函数声明---------------------------
    Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
    Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUS)'-----------------类型---------------------------
    Private Type MEMORYSTATUS
            dwLength As Long
            dwMemoryLoad As Long
            dwTotalPhys As Long
            dwAvailPhys As Long
            dwTotalPageFile As Long
            dwAvailPageFile As Long
            dwTotalVirtual As Long
            dwAvailVirtual As Long
    End Type'-----------------常数---------------------------
    Private Const GMEM_ZEROINIT = &H40
    Private Const GMEM_MOVEABLE = &H2'-----------------变量---------------------------
    Dim m_MemSize As Long
    Dim m_MemHandle As Long
    Dim m_MemPointer As Long
    Dim m_MemStat As MEMORYSTATUS'*******************************************************************************************************************************
    '以下属性从MEMORYSTATUS结构中返回内存状态变量。这一结构通过调用GlobalMemoryStatus API函数来填入数据
    '*******************************************************************************************************************************
    Public Property Get dwLength() As Long
      dwLength = m_MemStat.dwLength
    End PropertyPublic Property Get dwMemoryLoad() As Long
      dwMemoryLoad = m_MemStat.dwMemoryLoad
    End PropertyPublic Property Get dwTotalPhys() As Long
      dwTotalPhys = m_MemStat.dwTotalPhys
    End PropertyPublic Property Get dwAvailPhys() As Long
      dwAvailPhys = m_MemStat.dwAvailPhys
    End Property
    Public Property Get dwTotalPageFile() As Long
      dwTotalPageFile = m_MemStat.dwTotalPageFile
    End PropertyPublic Property Get dwAvailPageFile() As Long
      dwAvailPageFile = m_MemStat.dwAvailPageFile
    End Property
    Public Property Get dwTotalVirtual() As Long
      dwTotalVirtual = m_MemStat.dwTotalVirtual
    End Property
    Public Property Get dwAvailVirtual() As Long
      dwAvailVirtual = m_MemStat.dwAvailVirtual
    End Property'*****************************************************************************
    'MemSize属性设置并取回内存块的大小
    '*****************************************************************************
    Public Property Let MemSize(Mem_Size As Long)
       m_MemSize = Mem_Size
    End PropertyPublic Property Get MemSize() As Long
       MemSize = m_MemSize
    End Property
    '*****************************************************************************
    'MemHandle属性设置并取回全局内存块的句柄
    '*****************************************************************************
    Public Property Let MemHandle(Mem_Handle As Long)
       m_MemHandle = Mem_Handle
    End PropertyPublic Property Get MemHandle() As Long
       MemHandle = m_MemHandle
    End Property
    '*****************************************************************************
    'MemPointer属性设置并恢复内存指针的句柄,内存指针用来确定在内存块中读写数据的位置
    '*****************************************************************************
    Public Property Let MemPointer(Mem_Pointer As Long)
       m_MemPointer = Mem_Pointer
    End PropertyPublic Property Get MemPointer() As Long
       MemPointer = m_MemPointer
    End Property
    '*****************************************************************************
    'GAlloc方法按指定大小分配内存块。注意,内存块用GMEM_MOVEABLE常量分配成可移动
    '块,并用GMEM_ZEROINIT常量将其初始化为零。内存块被设置成可移动块是为了适应Windows
    '的内存管理
    '*****************************************************************************
    Public Function GAlloc()
    'Allocates the memory with as moveable and initializes the memory with 0S
       m_MemHandle = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, m_MemSize)
    End Function'*****************************************************************************
    'Glock函数用来锁定用于读写的全局内存块,返回指向内存的指针。当内存块锁定时,
    'Windows的内存管理系统就不能移动内存块,也就无法改变它的位置
    '*****************************************************************************
    Public Function GLock()
       m_MemPointer = GlobalLock(m_MemHandle)
    End Function'*****************************************************************************
    'GUnlock方法为了内存管理对加锁的内存块开锁
    '*****************************************************************************
    Public Function GUnLock()
       Dim Throw As Long
       'Unlock the memory block
       Throw = GlobalUnlock(m_MemHandle)
    End Function'*****************************************************************************
    'GFree函数用于内存块的释放
    '*****************************************************************************
    Public Function GFree()
       Dim Throw As Long
       'Free the memory block
       Throw = GlobalFree(m_MemHandle)
    End Function'*****************************************************************************
    'GSize函数取回内存块大小
    '*****************************************************************************
    Public Function GSize()
       GSize = GlobalSize(m_MemHandle)
    End Function
    '*****************************************************************************
    'GetMemStat方法返回系统当前内存状态
    '*****************************************************************************
    Public Function GetMemStat()
       Dim MemStat As MEMORYSTATUS
       'set the length of the structure
       MemStat.dwLength = Len(m_MemStat)
       'fill the structure
       Call GlobalMemoryStatus(m_MemStat)
    End Function
      

  7.   

    -----------------------------文件3-----------------------------
    ’---------------------------FrmMain.frm-------------------------
    Private Sub Command1_Click()
      CopyFile App.Path + "\1.txt", App.Path + "\2.txt"
    End Sub'*******************************************
    ' 拷贝文件
    '*******************************************
    Private Function CopyFile(Source, Destination)
      Dim ApiMemory As New C_APIMemory
      Dim ApiFile As New C_APIFILE
      Dim FileSize As Long
      ApiFile.FileName = Source
      ApiFile.OpenFile
      FileSize = ApiFile.GetSize
      ApiMemory.MemSize = FileSize  '要申请的内存块大小
      ApiMemory.GAlloc              '申请内存块
      ApiMemory.GLock               '锁定内存块,并取得内存首地址
      ApiFile.APIRead ApiMemory.MemPointer, FileSize    '读取文件内容,放入内存空间
      ApiFile.CloseFile             '关闭文件
      ApiFile.FileName = Destination  '
      ApiFile.FileCreate            '建立新文件
      ApiFile.APIWrite ApiMemory.MemPointer, FileSize    '将内存块内容写入文件
      ApiFile.CloseFile              '关闭文件
      ApiMemory.GUnLock              '解锁内存块
      ApiMemory.GFree                '释放内存块空间
    End Function问题所在:
       不能实现正确拷贝文件,文件2.txt中得到的内容希奇古怪,不可读
    请大家帮忙调试,看看问题所在谢谢了。
      

  8.   

    我看了一下,不知怎么回事情,readfile老是读不到值,如果把这个函数,改成一般的读文件,然后用copymemory也是可以copy到那个内存地址的。这样可以吗
      

  9.   

    Public Function OpenFile()
        Dim Security As SECURITY_ATTRIBUTES
    '    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
        Const FILE_SHARE_READ = &H1
        Const FILE_SHARE_WRITE = &H2
        m_FilePointer = CreateFile(m_FileName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
    End Function
    '***************************************************************************************************
    'APIRead函数将Size指定的字节数读入Pointer参数指向的内存块。
    '***************************************************************************************************
    Public Function APIRead(Pointer As Long, Size As Long)
       Dim Throw As Long
       Dim N As Long  '从文件中读出的数据字节数
       'Read the file.ReadFile handles reading the data into a global memory block which we have a long pointer value to.
       Throw = ReadFile(m_FilePointer, ByVal Pointer, Size, N, ByVal 0&)
    End Function
    '***************************************************************************************************
    'APIWrite函数将Size指定的字节数从Pointer参数指向的内存块中取出。
    '***************************************************************************************************
    Public Function APIWrite(Pointer As Long, Size As Long)
       Dim Throw As Long
       Dim N As Long '写入文件的数据字节数
       'Write the file.WriteFile handles reading the data into a global memory block which we have a long pointer value to.
       Throw = WriteFile(m_FilePointer, ByVal Pointer, Size, N, ByVal 0&)
    End Function'***************************************************************************************************
    'FileCreate 方法利用CREATE_NEW 标志建立一个新文件。可与使用OPEN_EXISTING标志作比较
    '***************************************************************************************************
    Public Function FileCreate()
    'CreateFile uses the CREATE_NEW constant to create a new file.
       Dim Security As SECURITY_ATTRIBUTES
       Const FILE_SHARE_READ = &H1
        Const FILE_SHARE_WRITE = &H2
       m_FilePointer = CreateFile(m_FileName, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, CREATE_NEW, 0, 0)
    '   m_FilePointer = CreateFile(m_FileName, GENERIC_READ Or GENERIC_WRITE, 0&, Security, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0&)
    End Function
    主要是这些地方有点不对,在创建createfile的时候不能创建,你用我注释掉的声明。
    还有就是readfile和writefile函数的不一样,就是一个byval 和byref的区别因为byref是默认的。我调试过已经可以了
      

  10.   

    谢谢了,就因为APIWrite中一个参数要用传值的方式传递