Solved

How to use CreateFile and WriteFile functions from "kernel32" in VB.NET ?

Posted on 2004-04-21
11
3,888 Views
Last Modified: 2012-05-04
Greetings !

I'm converting classic vb project to VB.NET. The application does Cheque scanning and store images in memory, so the developer has to save those images manually to the hard disk (HD).

I'm having a difficulty while saving to the HD using VB.NET

Please have a look at the function I wrote in Classic VB.

'// Saves an image from memory
Public Function SaveImage(ByVal strFileName$, ByVal lngMem&, ByVal lngBytes&) As Long
    Dim lngFile&
    Dim lngTemp&
   
    lngFile = CreateFile(strFileName, GENERIC_WRITE, FILE_SHARE_READ, 0, _
                         CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, 0)
    If lngFile = INVALID_HANDLE_VALUE Then
        SaveImage = -801 '// FSM Error opening TIFF/file
        Exit Function
    End If
   
    '// Write the image to the file
    If (WriteFile(lngFile, ByVal lngMem, lngBytes, lngTemp, ByVal 0&) = 0) Then
        SaveImage = -803 '// FSM Error writing to TIFF/file
    End If
    CloseHandle (lngFile)
End Function

In VB.NET, I'm writing this routine:

Public Function SaveImage(ByVal strFileName As String, ByVal lngMem As Int32, _
            ByVal lngBytes As Int32) As Int32

        If File.Exists(strFileName) Then File.Delete(strFileName)
        Dim fs As New FileStream(strFileName, FileMode.CreateNew, FileAccess.Write)
        Dim w As New BinaryWriter(fs)
        w.Write(lngMem)
        w.Close()
        fs.Close()
End Function

Image is getting created in the given path, but when i try to open in Imaging Preview, Error is prompting "The doument's format is invalid or not supported."

Can anyone of you please help me to convert that function in VB.NET. Your help will be highly appreciated. I'm stuck.

Cheers
YamihO
0
Comment
Question by:yamiho
  • 6
  • 5
11 Comments
 
LVL 12

Expert Comment

by:dfiala13
ID: 10885233
All you've written to the file is an integer.  Finish writing all the data and should be all set.

Your write code looks fine, you're just not writing enough.  here's a link to the MS site for reference.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconreadingwritingtonewlycreateddatafile.asp
0
 
LVL 1

Author Comment

by:yamiho
ID: 10885357
Hi,

Thanks for your response. But I'm not really getting what exactly I'm missing in the code. Can you please elaborate it ? I'm trying to write a tiff image (imgFront.tif).

Alternatively, can I use CreateFile and WriteFile?

'Declaration for CreateFile (VB.Net Syntax)
Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer,<MarshalAs(UnmanagedType.Struct)> ByRef   lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer

'Declaration for WriteFile (VB.Net Syntax)
Public Declare Function WriteFile Lib "kernel32" Alias "WriteFile" (ByVal hFile As Integer, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Integer, lpNumberOfBytesWritten As Integer,<MarshalAs(UnmanagedType.Struct)> ByRef   lpOverlapped As OVERLAPPED) As Integer

But I've no idea how to write error code free and implement it. Can you help me to solve this issue.

Thanks a lot.
Cheers
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10885438
You do not need to go to the WinAPI to create a file in .NET.  There are already classes in the .NET framework (System.IO namespace) to do this. See the link above.

All you need to do is get the byte respresentation of the TIFF file and write it out.  Something like this.

Public Sub SaveFile(sFilePath as String, bt as Byte())

        ' Create the new, empty data file.
        If File.Exists(sFilePath) Then
            File.Delete(sFilePath)
        End If
        Dim fs As New FileStream(sFilePath, FileMode.CreateNew)
        ' Create the writer for data.
        Dim w As New BinaryWriter(fs)
        ' Write data to the file
        w.Write(bt)
         w.Close()
        fs.Close()
End Sub
0
 
LVL 1

Author Comment

by:yamiho
ID: 10885526
Thanks a lot. Yes I'm beginning to understand from you. And extremely sorry for asking silly question. Once i scan the image is stored in the memory and i need to retrieve from the memory and save it in the hard disk. How do i retrieve from the memory. Any reference/example ?
0
 
LVL 1

Author Comment

by:yamiho
ID: 10885557
This is a API function documentation prepared by vendor for your better understand what i'm talking about. I'm unable to understand, coz he is talking in low level language. I'm a beginner. :(

he is talking about scanning image and capturing front and rear image.

------------------------------------------

function name: fsmSortGetFrontMemory, fsmSortGetBackMemory

NOTE:  fsmSortGetFrontMemory should only be called by the above mentioned call back functions.
NOTE:  fsmSortGetBackMemory should only be called by the above mentioned call back functions.

Get the memory locations for the front from struct SortCallBack pointed to by lParam.  fsnSortGetBackMemory gets the memory location for the back image.
NOTE:  It is the user’s responsibility to allocate enough memory for the images.  We suggest 128,000 for TIFF (bitonal, black/white images) and 8 * 128,000 for gray scale (JPEG, 25% quality).  Obviously, these are extremely generous sizes for the files.  Smaller will work but we do not guarantee the results.  

NOTE:  On the 5180 when fsmScanImageFirst is called, the image call back function will return the image in memory.
Syntax

#include <fsmfuji.h>

char * PASCAL fsmSortGetFrontMemory(long lParam, long *plFrontSize)
char * PASCAL fsmSortGetBackMemory(long lParam, long *plBackSize)


Description

Gets the memory pointers for the document from struct SortCallBack pointed to by lParam.

Arguments

lParam            lParam passed to the call back function.  Pass lParam in the call back function directly to this function without modification.

plFrontFileSize             Size in bytes of front image in memory.
plBackFileSize             Size in bytes of back image in memory.

Returns

Return functions points to pcFront or memory location for the front image or pcBack or memory location for the back image.  If return is NULL, then no image is available in memory or if  *plFrontSize or *plBackSize = 0, then an image is not available.


NULL             Image memory not found or not valid.


Functions Called

NONE




------------------------------------------------------------------
I'm implementing like this :

Dim lngMemFront, lngMemBack As Int32
lngMemBack = fsmSortGetBackMemory(LParam, lngSizeBack)



plz help..
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Expert Comment

by:dfiala13
ID: 10885561
That's a good question.  How do you access it in classic VB? Do you have a handle or a pointer to the image?  I'm not sure what you are using to do the scanning or the API to the scanning program.  That's where you fill find the answer.

You can use the System.Drawing.Image class

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawingimageclasstopic.asp

to work with the image once you know how to get a hold of its memory representation
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10885576
Who makes the scanner?  
0
 
LVL 1

Author Comment

by:yamiho
ID: 10886250
This scanner was developed by Fuji Machine Systems and DLL written by different vendor. I'm getting to understand now.

Can you give an example of System.Drawing.Image which reads image from the memory and write to a hard disk in TIFF Format ?
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10888579
Reading from memory requires accessing the vendor's dll, which looks like to returns a pointer to two parts of the image (front and back).  The function also returns the size of the image.

How are you calling the methods to the dll in VB6?
0
 
LVL 12

Accepted Solution

by:
dfiala13 earned 500 total points
ID: 10888879
Ok,
I think this is about how you would do it.  Since I don't have the scanner or API I can't test it, but something like this should work to get the front or back image.  I have no idea how you combine them, but this is how you would get the data out of memory and into a Byte array

Dim bufPtr as IntPtr
Dim lParam as Int32
Dim lFrontSize as Int32

bufPtr = fsmSortGetFrontMemory(lParam, lFrontSize)
Dim bt(lFrontSize) as Byte

'this will copy the memory into the bt Byte array
Marshal.Copy(bufPtr, bt, 0, lFrontSize)

Here's the doc for the Marshal.Copy function...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemruntimeinteropservicesmarshalclasscopytopic7.asp

You'll need to import the System.Runtime.InteropServices namespace into your class to use it.


0
 
LVL 1

Author Comment

by:yamiho
ID: 10895443
thanks a lot for your help...
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

11 Experts available now in Live!

Get 1:1 Help Now