🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.delphi.nativeapi.win32
14 messages
14 total messages Started by Ian Boyd Wed, 23 Jul 2008 20:48
Can someone else run this code and see if it fails?
#139
Author: Ian Boyd
Date: Wed, 23 Jul 2008 20:48
45 lines
2122 bytes
i have assurances that this code should work. i've tried it on 3 computers 
here, it fails every time


procedure TForm1.Button4Click(Sender: TObject);
var
    dwSharingMode: DWORD;
    hCreatedFile: THandle;
    hFile: THandle;
begin
    //Sharing mode when we create the file
    dwSharingMode :=
            FILE_SHARE_READ or
            FILE_SHARE_WRITE;

    hFile := CreateFile(
            'c:\tempFile.dat',
            GENERIC_WRITE,
            dwSharingMode,
            nil,
            CREATE_ALWAYS,
            FILE_FLAG_DELETE_ON_CLOSE,
            0);
    Assert(hFile <> INVALID_HANDLE_VALUE,
                SysErrorMessage(GetLastError));
    hFile := 0; //Leave the handle open

    hFile := CreateFile(
            'c:\tempFile.dat',
            GENERIC_READ,
            FILE_SHARE_READ or FILE_SHARE_WRITE,
            nil,
            OPEN_EXISTING,
            0,
            0);
    if hFile <> INVALID_HANDLE_VALUE then
    begin
        CloseHandle(hFile);
        MessageDlg('Sucessfully opened temporary file.',
                    mtInformation, [mbOk], 0)
    end
    else
        MessageDlg('Could not open temporary file: '+
                SysErrorMessage(GetLastError), mtError, [mbOk], 0);
end;
Re: Can someone else run this code and see if it fails?
#140
Author: David Cornelius
Date: Wed, 23 Jul 2008 21:03
10 lines
1353 bytes
>i have assurances that this code should work. i've tried it on 3 computers 
>here, it fails every time


It fails for me on the Assert line.  If I commented out that line, I get 
"Could not open temporary file..."

-- 
David Cornelius
CorneliusConcepts.com 
Re: Can someone else run this code and see if it fails?
#142
Author: Pieter Zijlstra
Date: Wed, 23 Jul 2008 22:53
81 lines
3043 bytes
Ian Boyd wrote:

> i have assurances that this code should work. i've tried it on 3
> computers here, it fails every time
> 
> 
> procedure TForm1.Button4Click(Sender: TObject);
> var
>    dwSharingMode: DWORD;
>    hCreatedFile: THandle;
>    hFile: THandle;
> begin
>    //Sharing mode when we create the file
>    dwSharingMode :=
>            FILE_SHARE_READ or
>            FILE_SHARE_WRITE;
> 
>    hFile := CreateFile(
>            'c:\tempFile.dat',
>            GENERIC_WRITE,
>            dwSharingMode,
>            nil,
>            CREATE_ALWAYS,
>            FILE_FLAG_DELETE_ON_CLOSE,
>            0);
>    Assert(hFile <> INVALID_HANDLE_VALUE,
>                SysErrorMessage(GetLastError));
>    hFile := 0; //Leave the handle open
> 
>    hFile := CreateFile(
>            'c:\tempFile.dat',
>            GENERIC_READ,
>            FILE_SHARE_READ or FILE_SHARE_WRITE,
>            nil,
>            OPEN_EXISTING,
>            0,
>            0);
>    if hFile <> INVALID_HANDLE_VALUE then
>    begin
>        CloseHandle(hFile);
>        MessageDlg('Sucessfully opened temporary file.',
>                    mtInformation, [mbOk], 0)
>    end
>    else
>        MessageDlg('Could not open temporary file: '+
>                SysErrorMessage(GetLastError), mtError, [mbOk], 0);
> end;


It fails the second call needs to specify FILE_SHARE_DELETE...

   hFile := CreateFile(
           'c:\tempFile.dat',
           GENERIC_READ,
           FILE_SHARE_DELETE or FILE_SHARE_READ or FILE_SHARE_WRITE,
           nil,
           OPEN_EXISTING,
           0,
           0);


From the MSDN docs 
(http://msdn.microsoft.com/en-us/library/aa363858.aspx):

<quote>
FILE_FLAG_DELETE_ON_CLOSE
0x04000000
 The file is to be deleted immediately after all of its handles are
closed, which includes the specified handle and any other open or
duplicated handles.

If there are existing open handles to a file, the call fails unless
they were all opened with the FILE_SHARE_DELETE share mode.

Subsequent open requests for the file fail, unless the
FILE_SHARE_DELETE share mode is specified.
</quote>
 

-- 
Pieter
Re: Can someone else run this code and see if it fails?
#141
Author: Ronaldo Souza
Date: Wed, 23 Jul 2008 23:02
13 lines
1929 bytes
Ian Boyd wrote:
> i have assurances that this code should work. i've tried it on 3 computers 
> here, it fails every time

On the first click, the if on line 57 fails ("file already in use by
another process"). On subsequent clicks, the assert on line 46 fails 
(same error).

Delphi 5 - XP Pro SP2

-- 
Rgds,
Ronaldo
Re: Can someone else run this code and see if it fails?
#143
Author: Ronaldo Souza
Date: Wed, 23 Jul 2008 23:23
19 lines
2129 bytes
Ronaldo Souza wrote:
> Ian Boyd wrote:
>> i have assurances that this code should work. i've tried it on 3 
>> computers here, it fails every time
> 
> On the first click, the if on line 57 fails ("file already in use by
> another process"). On subsequent clicks, the assert on line 46 fails 
> (same error).
> 
> Delphi 5 - XP Pro SP2
> 


With Pieter's suggestion, it works on the first click and the assert 
fails on subsequent clicks.

-- 
Rgds,
Ronaldo
Re: Can someone else run this code and see if it fails?
#144
Author: Pieter Zijlstra
Date: Wed, 23 Jul 2008 23:44
22 lines
1708 bytes
Ronaldo Souza wrote:

> Ronaldo Souza wrote:
> > Ian Boyd wrote:
> > > i have assurances that this code should work. i've tried it on 3
> > > computers here, it fails every time
> > 
> > On the first click, the if on line 57 fails ("file already in use by
> > another process"). On subsequent clicks, the assert on line 46
> > fails  (same error).
> > 
> > Delphi 5 - XP Pro SP2
> 
> 
> With Pieter's suggestion, it works on the first click and the assert
> fails on subsequent clicks.

Of course ;-) In that case the first call to CreateFile also needs to
specify FILE_SHARE_DELETE in the sharing mode (dwSharingMode).

-- 
Pieter
Re: Can someone else run this code and see if it fails?
#146
Author: Ronaldo Souza
Date: Thu, 24 Jul 2008 00:08
15 lines
2111 bytes
 > Pieter Zijlstra wrote:
> >Ronaldo Souza wrote:
> >
> > With Pieter's suggestion, it works on the first click and the assert
> > fails on subsequent clicks.
> 
> Of course ;-) In that case the first call to CreateFile also needs to
> specify FILE_SHARE_DELETE in the sharing mode (dwSharingMode).


<g>

-- 
Rgds,
Ronaldo
Re: Can someone else run this code and see if it fails?
#149
Author: Ian Boyd
Date: Thu, 24 Jul 2008 01:17
8 lines
1512 bytes
> It fails for me on the Assert line.  If I commented out that line, I get 
> "Could not open temporary file..."

That assert should only fail if you can't write to c:\. Which i know is the 
case if you login as a standard user.

i didn't want to include the GetTemporaryPath api call to confuse the 
example. 
Re: Can someone else run this code and see if it fails?
#147
Author: Ian Boyd
Date: Thu, 24 Jul 2008 01:19
18 lines
1771 bytes
> It fails the second call needs to specify FILE_SHARE_DELETE...
>
>   hFile := CreateFile(
>           'c:\tempFile.dat',
>           GENERIC_READ,
>           FILE_SHARE_DELETE or FILE_SHARE_READ or FILE_SHARE_WRITE,
>           nil,
>           OPEN_EXISTING,
>           0,
>           0);
>

Well, the goal was to try to create a "temporary file", spawn the associated 
application (e.g. Excel, Word, Notepad), and have the temporary file clean 
itself up when the last handle closes.

i can't control how these other programs decide to open files, but all of 
them do FILE_SHARE_READ|FILE_SHARE_WRITE. 
Re: Can someone else run this code and see if it fails?
#150
Author: Ian Boyd
Date: Thu, 24 Jul 2008 01:20
14 lines
1683 bytes
"Ronaldo Souza" <nospam@nospam.org> wrote in message 
news:4887b891@newsgroups.borland.com...
> Ian Boyd wrote:
>> i have assurances that this code should work. i've tried it on 3 
>> computers here, it fails every time
>
> On the first click, the if on line 57 fails ("file already in use by
> another process"). On subsequent clicks, the assert on line 46 fails (same 
> error).

You get the same error i do.

i would expect it to fail if it were clicked more than once - you'd have to 
close the app to have the file delete. 
Re: Can someone else run this code and see if it fails?
#148
Author: Ian Boyd
Date: Thu, 24 Jul 2008 01:22
6 lines
1216 bytes
Thank you, all.

You confirmed what i already knew: it can't work.


Although someone here swears it can be made to work.
Re: Can someone else run this code and see if it fails?
#156
Author: Ricardo Carmo
Date: Thu, 24 Jul 2008 15:57
34 lines
1846 bytes
try this:

procedure TForm1.Button1Click(Sender: TObject);
var b: Cardinal;
    hFile: THandle;
    Buffer,ReturnBuffer: Array[0..5] of Char;
begin
    Buffer:='teste'#0;
    hFile := CreateFile(
            'c:\tempFile.dat',
            GENERIC_READ or GENERIC_WRITE,
            FILE_SHARE_READ or FILE_SHARE_WRITE,
            nil,
            CREATE_ALWAYS,
            FILE_FLAG_DELETE_ON_CLOSE,
            0);
    WriteFile(hFile,Buffer,5,b,nil);   //write some text
    SetFilePointer(hFile,0,nil,file_begin);
    ReadFile(hFile,ReturnBuffer,5,b,nil);  //read back
    ShowMessage(string(ReturnBuffer));
    CloseHandle(hFile);
end;




"Ian Boyd" <ian.borlandnews008@zunblvlda1.dyndns.org.spamsucks> escreveu na
mensagem news:4887d966$1@newsgroups.borland.com...
> Thank you, all.
>
> You confirmed what i already knew: it can't work.
>
>
> Although someone here swears it can be made to work.
Re: Can someone else run this code and see if it fails?
#162
Author: Ricardo Carmo
Date: Thu, 24 Jul 2008 20:14
45 lines
2478 bytes
Here is what do you want:

procedure TForm1.Button1Click(Sender: TObject);
var b: Cardinal;
    hFile,hFile2: THandle;
    Buffer,ReturnBuffer: Array[0..5] of Char;
begin
    Buffer:='teste'#0;
    hFile := CreateFile(
            'c:\tempFile.dat',
            GENERIC_READ or GENERIC_WRITE,
            FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE,
            nil,
            CREATE_ALWAYS,
            FILE_FLAG_DELETE_ON_CLOSE,
            0);
    WriteFile(hFile,Buffer,5,b,nil);   //write some text
    SetFilePointer(hFile,0,nil,file_begin);
    ReadFile(hFile,ReturnBuffer,5,b,nil);  //read back
    ShowMessage(string(ReturnBuffer));

    hFile2:= CreateFile(
            'c:\tempFile.dat',
            GENERIC_READ,
            FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE,
            nil,
            OPEN_EXISTING,
            0,
            0);
    if hFile2 <> INVALID_HANDLE_VALUE then
    begin
        CloseHandle(hFile2);
        MessageDlg('Sucessfully opened temporary file.',
                    mtInformation, [mbOk], 0)
    end;
    CloseHandle(hFile);

"Ian Boyd" <ian.borlandnews008@zunblvlda1.dyndns.org.spamsucks> escreveu na
mensagem news:4887d966$1@newsgroups.borland.com...
> Thank you, all.
>
> You confirmed what i already knew: it can't work.
>
>
> Although someone here swears it can be made to work.
Re: Can someone else run this code and see if it fails?
#215
Author: Ian Boyd
Date: Thu, 24 Jul 2008 20:54
19 lines
1718 bytes
>    hFile2:= CreateFile(
>            'c:\tempFile.dat',
>            GENERIC_READ,
>            FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE,
>            nil,
>            OPEN_EXISTING,
>            0,
>            0);

Although, once again, i can't control the 2nd call to CreateFile, it's what 
Excel, Word, Notepad, would do.

i'm trying to make my initial CreateFile compatible with someone who calls 
CreateFile with READ|WRITE.


Which cannot be done, since i also want the DELETE_ON_CLOSE feature, which 
would require Excel to be re-written.  In other words, i'm want to achieve 
something which cannot be done.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads