Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Thu Nov 21, 2019 4:08 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Win32-API Readfile Example?
PostPosted: Tue Jul 10, 2012 11:45 am 
Offline

Joined: Sun Nov 26, 2006 11:01 am
Posts: 58
Hello all,

has anyone an example using Readfile from the Win32-API? I'm always getting Nil-Object-Exception on the line
result = ReadFile(filehandle, lpBuffer.Ptr(0), nNumberOfBytesToRead, lpNumberOfBytesRead, Nil)

whereas Readfile was declared as
Declare Function ReadFile Lib "kernel32" (hFile As Integer, lpBuffer As Ptr, nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, lpOverlapped As Ptr) As Integer

and the variables were defined as the following:

Const buffersize = 1024
Dim filehandle, result As Integer
Dim lpBuffer As MemoryBlock = New MemoryBlock(buffersize)
Dim nNumberOfBytesToRead As Integer = buffersize
Dim lpNumberOfBytesRead As Integer

Thanks,
Alex


Top
 Profile  
Reply with quote  
 Post subject: Re: Win32-API Readfile Example?
PostPosted: Tue Jul 10, 2012 11:50 am 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
Is there a reason you want/need to use the API to read a file?

RS has plenty of built-in file management functions

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
Reply with quote  
 Post subject: Re: Win32-API Readfile Example?
PostPosted: Tue Jul 10, 2012 11:58 am 
Offline

Joined: Sun Nov 26, 2006 11:01 am
Posts: 58
DaveS wrote:
Is there a reason you want/need to use the API to read a file?

RS has plenty of built-in file management functions


Got the error now. It must be
ReadFile(filehandle, lpBuffer, ...

instead of
ReadFile(filehandle, lpBuffer.Ptr(0), ...


I want to read a logfile which is write-locked by another program. RB won't let me read from it
via the built-in commands, I always get an error. So I thouht I try it via Win32-API.

Alex


Top
 Profile  
Reply with quote  
 Post subject: Re: Win32-API Readfile Example?
PostPosted: Tue Jul 10, 2012 12:06 pm 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
doubtful.... since RS is most likely internally calling exactly the same routines you are trying to call manually.

Have you tried doing a FILECOPY, and then reading the copy instead of the original?

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
Reply with quote  
 Post subject: Re: Win32-API Readfile Example?
PostPosted: Tue Jul 10, 2012 2:17 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
Use the CreateFile API to acquire a file handle to the file you want and with the sharemode/create disposition/etc. you specifically require. Take the file handle and pass it to the TextInputStream's Constructor to get a TextInputStream to the file with the specified permissions.

e.g. (untested):

Declare Function CreateFileW Lib "Kernel32" (name As WString, access As Integer, sharemode As Integer, SecAtrribs As Integer, CreateDisp As Integer, flags As Integer, template As Integer) As Integer
Dim fhandle As Integer = CreateFileW(TheFile.AbsolutePath, FILE_READ_DATA, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0)
Dim tis As TextInputStream = New TextInputStream(fhandle, TextInputStream.HandleTypeWin32Handle)

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: Win32-API Readfile Example?
PostPosted: Tue Jul 10, 2012 3:26 pm 
Offline

Joined: Sun Nov 26, 2006 11:01 am
Posts: 58
DaveS wrote:
doubtful.... since RS is most likely internally calling exactly the same routines you are trying to call manually.

Have you tried doing a FILECOPY, and then reading the copy instead of the original?

I thought of a filecopy already as a workaround, but the problem is that I have to analyze the log several times per second, so this won't be a good solution.

RB is calling the same routines I'm using here, but is not offering the options I require (shared read).

charonn0 wrote:
Use the CreateFile API to acquire a file handle to the file you want and with the sharemode/create disposition/etc. you specifically require. Take the file handle and pass it to the TextInputStream's Constructor to get a TextInputStream to the file with the specified permissions.

I was not aware that I can pass the filehandle I get via Win32-API to a RB function. Thank you very much for this hint, I'll try it.


Top
 Profile  
Reply with quote  
 Post subject: Re: Win32-API Readfile Example?
PostPosted: Wed Jul 11, 2012 12:55 am 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
Alex2k wrote:
I was not aware that I can pass the filehandle I get via Win32-API to a RB function. Thank you very much for this hint, I'll try it.

Certain classes in the RB runtime expose the underlying OS handles/file pointers/etc. The TextInput/TextOutPut/BinaryStream classes all accept OS-specific file handles via their Constructor method, the Picture class exposes OS-specific graphics handles (e.g. Win32 device contexts), every control and window exposes its OS handle, and so on.

Under most circumstances, you would never call the TextInput/TextOutPut/BinaryStream Constructors yourself but rather would use the appropriate shared method to get an instance of the Stream class you want (e.g. TextInputStream.Open(FolderItem)) These shared methods abstract the OS-specific file I/O boilerplate (CreateFile, WriteFile, etc.) but do limit us somewhat to the common subset of I/O features available on all the supported platforms. Luckily REALsoftware leaves us the tools to make up for these edge cases.

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: Win32-API Readfile Example?
PostPosted: Wed Jul 11, 2012 3:17 am 
Offline

Joined: Sun Nov 26, 2006 11:01 am
Posts: 58
Thank you very much charonn0 for this valuable information.

By my tests I discovered that the same programm compiled with the most recent version of RS and an older version has a different behaviour. I can open the write-locked logfile with RS2011, but not with the older one - so it must be a bug the old version of RS. All who need easy access to the CreateFile-Function of Win32-API will find below a module which bundles everything.

Download-Links TextInputStreamExtension.xml: http://www.sendspace.com/file/gokuk1
http://www.xup.in/dl,18517847/TextInput ... nsion.xml/
http://www.file-upload.net/download-453 ... n.xml.html
http://netload.in/dateiwuhudhn0Zo/TextI ... on.xml.htm
http://www.your-filehosting.com/al5tvbj ... n.xml.html
Copy & Paste TextInputStreamExtension.xml:

<?xml version="1.0"?>
<RBProject>
<block type="Module" ID="1419519999">
<ObjName>TextInputStreamExtension</ObjName>
<ObjContainerID>0</ObjContainerID>
<IsClass>0</IsClass>
<ItemFlags>1</ItemFlags>
<IsInterface>0</IsInterface>
<Compatibility></Compatibility>
<Method>
<ItemName>OpenExtended</ItemName>
<Compatibility></Compatibility>
<Visible>1</Visible>
<ItemSource>
<TextEncoding>134217984</TextEncoding>
<SourceLine>Function OpenExtended(File As FolderItem, DesiredAccess As Integer, ShareMode As Integer, SecurityAttributes As Integer, CreationDisposition As Integer, FlagsAndAttributes AS Integer) As TextInputStream</SourceLine>
<SourceLine>' CreateFile: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx</SourceLine>
<SourceLine>Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" ( lpFileName As CString, dwDesiredAccess As Integer, dwShareMode As Integer, lpSecurityAttributes As Integer, dwCreationDisposition As Integer, dwFlagsAndAttributes As Integer, hTemplateFile As Integer ) As Integer</SourceLine>
<SourceLine>Dim FileHandle As Integer = CreateFile(File.AbsolutePath , DesiredAccess , ShareMode , SecurityAttributes, CreationDisposition, FlagsAndAttributes , 0)</SourceLine>
<SourceLine>If FileHandle &lt;&gt; -1 Then</SourceLine>
<SourceLine>Return New TextInputStream(FileHandle, TextInputStream.HandleTypeWin32Handle)</SourceLine>
<SourceLine>Else</SourceLine>
<SourceLine>Return Nil</SourceLine>
<SourceLine>End If</SourceLine>
<SourceLine></SourceLine>
<SourceLine>End Function</SourceLine>
</ItemSource>
<TextEncoding>134217984</TextEncoding>
<AliasName></AliasName>
<ItemFlags>0</ItemFlags>
<IsShared>0</IsShared>
<ItemParams>File As FolderItem, DesiredAccess As Integer, ShareMode As Integer, SecurityAttributes As Integer, CreationDisposition As Integer, FlagsAndAttributes AS Integer</ItemParams>
<ItemResult>TextInputStream</ItemResult>
</Method>
<Note>
<ItemName>Readme</ItemName>
<Compatibility></Compatibility>
<Visible>1</Visible>
<ItemSource>
<TextEncoding>134217984</TextEncoding>
<NoteLine>Readme</NoteLine>
<NoteLine>Open files via CreateFile-function of Win32-API with additional options like shared-read</NoteLine>
<NoteLine>For Options see http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx</NoteLine>
<NoteLine></NoteLine>
<NoteLine>Example:</NoteLine>
<NoteLine>Dim file As FolderItem = GetFolderItem("C:\temp\helloworld.txt")</NoteLine>
<NoteLine>Dim filestream As TextInputStream = OpenExtended(file, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL)</NoteLine>
<NoteLine></NoteLine>
</ItemSource>
<ItemFlags>0</ItemFlags>
</Note>
<ViewBehavior>
<ViewProperty>
<ObjName>Name</ObjName>
<Visible>1</Visible>
<PropertyGroup>ID</PropertyGroup>
<InheritsFrom>Object</InheritsFrom>
</ViewProperty>
<ViewProperty>
<ObjName>Index</ObjName>
<Visible>1</Visible>
<PropertyGroup>ID</PropertyGroup>
<PropertyValue>-2147483648</PropertyValue>
<InheritsFrom>Object</InheritsFrom>
</ViewProperty>
<ViewProperty>
<ObjName>Super</ObjName>
<Visible>1</Visible>
<PropertyGroup>ID</PropertyGroup>
<InheritsFrom>Object</InheritsFrom>
</ViewProperty>
<ViewProperty>
<ObjName>Left</ObjName>
<Visible>1</Visible>
<PropertyGroup>Position</PropertyGroup>
<PropertyValue>0</PropertyValue>
<InheritsFrom>Object</InheritsFrom>
</ViewProperty>
<ViewProperty>
<ObjName>Top</ObjName>
<Visible>1</Visible>
<PropertyGroup>Position</PropertyGroup>
<PropertyValue>0</PropertyValue>
<InheritsFrom>Object</InheritsFrom>
</ViewProperty>
</ViewBehavior>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_SHARE_NOT</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H0</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_SHARE_READ</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H1</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_SHARE_WRITE</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H2</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_SHARE_DELETE</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H4</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>CREATE_NEW</ItemName>
<ItemType>2</ItemType>
<ItemDef>1</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>CREATE_ALWAYS</ItemName>
<ItemType>2</ItemType>
<ItemDef>2</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>OPEN_EXISTING</ItemName>
<ItemType>2</ItemType>
<ItemDef>3</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>OPEN_ALWAYS</ItemName>
<ItemType>2</ItemType>
<ItemDef>4</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>TRUNCATE_EXISTING</ItemName>
<ItemType>2</ItemType>
<ItemDef>5</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_ATTRIBUTE_ARCHIVE</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H20</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_ATTRIBUTE_ENCRYPTED</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H4000</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_ATTRIBUTE_HIDDEN</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H2</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_ATTRIBUTE_NORMAL</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H80</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_ATTRIBUTE_OFFLINE</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H1000</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_ATTRIBUTE_READONLY</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H1</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_ATTRIBUTE_SYSTEM</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H4</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_ATTRIBUTE_TEMPORARY</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H100</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_FLAG_BACKUP_SEMANTICS</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H02000000</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_FLAG_DELETE_ON_CLOSE</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H04000000</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_FLAG_NO_BUFFERING</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H20000000</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_FLAG_OPEN_NO_RECALL</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H00100000</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_FLAG_OPEN_REPARSE_POINT</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H00200000</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_FLAG_OVERLAPPED</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H40000000</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_FLAG_POSIX_SEMANTICS</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H0100000</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_FLAG_RANDOM_ACCESS</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H10000000</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_FLAG_SESSION_AWARE</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H00800000</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_FLAG_SEQUENTIAL_SCAN</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H08000000</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>FILE_FLAG_WRITE_THROUGH</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H80000000</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>GENERIC_WRITE</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H40000000</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>GENERIC_READ</ItemName>
<ItemType>2</ItemType>
<ItemDef>&amp;H80000000</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
<Constant>
<TextEncoding>134217984</TextEncoding>
<ItemName>INVALID_HANDLE_VALUE</ItemName>
<ItemType>2</ItemType>
<ItemDef>-1</ItemDef>
<ItemFlags>0</ItemFlags>
</Constant>
</block>
</RBProject>


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group