Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have several flaws in your code</p> <ol> <li><p>When you uses the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx" rel="noreferrer">CreateFile</a> function you must check the returned handle against the INVALID_HANDLE_VALUE.</p></li> <li><p>Always you must use try .. finally to free the resources.</p></li> <li><p>You are trying to use the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx" rel="noreferrer">ReadFile</a> function reading the contents inside of the <code>thefile</code> buffer using a index 1, this is wrong you must use a index 0 because the dynamic arrays are zero index based.</p></li> <li><p>you are using this code </p></li> </ol> <blockquote> <p>CloseHandle(CreateFileA (sfile, 0, 0,NIL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,0));</p> </blockquote> <p>To create an empty file and then using again the CreateFile function to open and write the file, you can do the same in one step </p> <pre><code> FileHandle := CreateFileA(sFile, GENERIC_WRITE, FILE_SHARE_WRITE, nil, CREATE_NEW, 0, 0); </code></pre> <p>Try this improved version of your code.</p> <p>Btw, there is nothing wrong with use SetLengh, the problem is explained in the point 3.</p> <pre><code>var thefile : array of AnsiChar; begin FileHandle := CreateFileA(paramstr0, GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0); if FileHandle &lt;&gt; INVALID_HANDLE_VALUE then try dwSize := GetFileSize(FileHandle, nil); SetFilePointer(FileHandle, 0, nil, FILE_BEGIN); SetLength(TheFile, dwsize); ReadFile(FileHandle, thefile[0], dwSize , dwRead, nil); finally CloseHandle (FileHandle); end; FileHandle := CreateFileA(sFile, GENERIC_WRITE, FILE_SHARE_WRITE, nil, CREATE_NEW, 0, 0); if FileHandle &lt;&gt; INVALID_HANDLE_VALUE then try WriteFile (FileHandle, thefile[0], dwSize, testCardinal, nil); finally CloseHandle (FileHandle); end; SetLength(TheFile, 0); end; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload