Solved

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

Posted on 1997-06-25
2
413 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

803 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