Solved

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

759 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