Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4192
  • Last Modified:

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

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
yamiho
Asked:
yamiho
  • 6
  • 5
1 Solution
 
dfiala13Commented:
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
 
yamihoAuthor Commented:
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
 
dfiala13Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
yamihoAuthor Commented:
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
 
yamihoAuthor Commented:
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
 
dfiala13Commented:
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
 
dfiala13Commented:
Who makes the scanner?  
0
 
yamihoAuthor Commented:
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
 
dfiala13Commented:
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
 
dfiala13Commented:
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
 
yamihoAuthor Commented:
thanks a lot for your help...
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now