Rieccomi ... Ok vi allego qui' il codice che uso per salvare un'immagine in memoria e poi leggerla ..Originariamente inviato da oregon
Ma passi dei puntatori in memoria tra processi diversi?
In ogni caso, il codice, ben spiegato, e' necessario ...
'In un modulo ...
' OpenFile() Structure
Type OFSTRUCT
cBytes As String * 1
fFixedDisk As String * 1
nErrCode As Integer
reserved As String * 4
szPathName As String * 128
End Type
' OpenFile() Flags
Global Const OF_READ = &H0
Global Const OF_WRITE = &H1
Global Const OF_READWRITE = &H2
Global Const OF_SHARE_COMPAT = &H0
Global Const OF_SHARE_EXCLUSIVE = &H10
Global Const OF_SHARE_DENY_WRITE = &H20
Global Const OF_SHARE_DENY_READ = &H30
Global Const OF_SHARE_DENY_NONE = &H40
Global Const OF_PARSE = &H100
Global Const OF_DELETE = &H200
Global Const OF_VERIFY = &H400
Global Const OF_CANCEL = &H800
Global Const OF_CREATE = &H1000
Global Const OF_PROMPT = &H2000
Global Const OF_EXIST = &H4000
Global Const OF_REOPEN = &H8000
'Nota personale ... non usare le le dichiarazioni rilasciate da "API text viewer" per queste API ... sono cannate !
' Enter each of the following Declare statements on one, single line:
Declare Function OpenFile Lib "kernel32" (ByVal lpFilename As String, lpReOpenBuff As _ OFSTRUCT, ByVal wStyle As Integer) As Long
Declare Function hRead Lib "kernel32" Alias "_hread" (ByVal hFile As Integer, lpMem As _ Any, ByVal lSize As Long) As Long
Declare Function hWrite Lib "kernel32" Alias "_hwrite" (ByVal hFile As Integer, lpMem As _ Any, ByVal lSize As Long) As Long
Declare Function lClose Lib "kernel32" Alias "_lclose" (ByVal hFile As Integer) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As _ Any, Source As Any, ByVal Length As Long)
' API per allocazione
' Global Memory Flags
Global Const GMEM_FIXED = &H0
Global Const GMEM_MOVEABLE = &H2
Global Const GMEM_NOCOMPACT = &H10
Global Const GMEM_NODISCARD = &H20
Global Const GMEM_ZEROINIT = &H40
Global Const GMEM_MODIFY = &H80
Global Const GMEM_DISCARDABLE = &H100
Global Const GMEM_NOT_BANKED = &H1000
Global Const GMEM_SHARE = &H2000
Global Const GMEM_DDESHARE = &H2000
Global Const GMEM_NOTIFY = &H4000
Global Const GMEM_LOWER = GMEM_NOT_BANKED
Global Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)
Global Const GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)
' Enter each of the following Declare statements on one, single line:
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As _ Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
'In una Form
'Dichiarazione Generale ....
Dim InpFile As String
Dim OutFile As String
Dim hFile As Long
Dim fileStruct As OFSTRUCT
Dim FSize As Long
Dim BytesRead As Long
Dim BytesWritten As Long
Dim hMem As Long
Dim lpMem As Long
Dim r As Integer
Sub Form_Load()
'Leggo un file ... anche bitmap ... funziona con qualsiasi file
InpFile = "C:\TEST.BMP"
'File per l'output di test !
OutFile = "C:\TEST_256color.BMP"
'Get the size of the file to be read
FSize = FileLen(InpFile)
If FSize > 0 Then
'Allocate a block of memory equal to the size of the input file.
hMem = GlobalAlloc(GPTR, FSize)
If hMem <> 0 Then
lpMem = GlobalLock(hMem)
'Read the file into memory
hFile = OpenFile(InpFile, fileStruct, OF_READ Or OF_SHARE_DENY_NONE)
BytesRead = hRead(hFile, ByVal lpMem, FSize)
MsgBox Format(BytesRead) & " bytes read into memory"
r = lClose(hFile)
'Write the file back to disk to verify the file was read correctly
hFile = OpenFile(OutFile, fileStruct, OF_CREATE Or OF_WRITE Or OF_SHARE_DENY_NONE)
BytesWritten = hWrite(hFile, ByVal lpMem, FSize)
MsgBox Format(BytesWritten) & " bytes written to output file"
r = lClose(hFile)
'Free resources
r = GlobalUnlock(hMem)
r = GlobalFree(hMem)
Else
MsgBox "Not enough memory to store file"
End If
Else
MsgBox "Input file is zero bytes in length"
End If
End Sub
'//*******************************************
Nell'esempio su scritto all'interno di uno stesso progetto, l'indirizzo "lpmem", essendo dichiarato globale, usato in un qualsiasi punto del proggeto legge e scrive il file che ho inserito in memoria.
Il problema e' che se lpmem viene rilasciato (tramite OCX) l'applicazione che usa l'oCX non legge una ceppa !
Provate !
![]()
Ciao
Filippo