Solved

Api.... lRead, lWrite....

Posted on 1997-06-25
2
408 Views
Last Modified: 2012-06-21
VB-5 on Win NT
Hello.  I am having a problem using these API functions.
The problem is that I can use lRead to read a file into memory.  However when I try to write the data back into a file something is going wrong.
What is happening is that the file is created however, the
file contains the address of the pointer I am using followed buy garbage.

Here is a sniplet of the code to read the file into memory

    mlng_MemoryHandle = GlobalAlloc(GMEM_ZEROINIT, FileSize)
   
    mlng_MemoryAddress = GlobalLock(mlng_MemoryHandle)
    hFile = CreateFile(FileName, GENERIC_READ,                                           FILE_SHARE_READ, ByVal 0&, OPEN_EXISTING,                                           FILE_ATTRIBUTE_ARCHIVE, ByVal 0&)
             
   retval = lread(hFile, ByVal mlng_MemoryAddress, FileSize)
   retval = CloseHandle(hFile)

Up until here everthing is fine.  the problem must be here.



hFile = CreateFile(FileName, GENERIC_WRITE,                    FILE_SHARE_WRITE, ByVal 0&, CREATE_ALWAYS,                    FILE_ATTRIBUTE_ARCHIVE, ByVal 0&)

MemSize = GlobalSize(mlng_MemoryHandle)
   
retval = lwrite(hFile, mlng_MemoryAddress, MemSize)

   
  'Close the File
  'Unlock Allocated memory
  'Deallocate memory
 etc etc...

What am I doing wrong????
0
Comment
Question by:stephaner
2 Comments
 
LVL 3

Accepted Solution

by:
rmichels earned 40 total points
ID: 1427700
Try putting a ByVal in front of of mlng_MemoryAddress in the lwrite call.   I think VB is doing something weird, converting your memory address to a string before pass it on.  In my Win 3.1 code, that;s how I called lwrite
0
 

Author Comment

by:stephaner
ID: 1427701
This sis not work.  However, by changing the the function declaration from:
Private Declare Function lwrite Lib "kernel32" Alias "_lwrite" (ByVal hFile As Long, ByVal lpBuffer As string, ByVal wBytes As Long) As Long

to:
Private Declare Function lwrite Lib "kernel32" Alias "_lwrite" (ByVal hFile As Long, ByVal lpBuffer As Long, ByVal wBytes As Long) As Long

I have solved my problem
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now