Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7743809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:37:47+00:00 2026-06-01T09:37:47+00:00

I’m having a problem with the SetLength command. It’s basically this problem: I work

  • 0

I’m having a problem with the SetLength command.

It’s basically this problem:

I work with WindowsAPI only as good as I can. My goal is to set a certain size to a dynamic array of chars:

Here is some code to understand:

var
thefile : array of char;
// or thefile : array [0..9999] of char; // <---- not really a good way, works tho

FileHandle := CreateFileA(paramstr0, GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
dwSize := GetFileSize(FileHandle,NIL);
SetFilePointer(FileHandle, 0, nil, FILE_BEGIN);
SetLength(TheFile, dwsize); // <--- I can't use this
ReadFile(FileHandle, thefile[1], dwSize , dwRead, nil);
CloseHandle (FileHandle);
CloseHandle(CreateFileA (sfile, 0, 0,NIL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,0));
FileHandle := CreateFileA(sFile, GENERIC_WRITE, FILE_SHARE_WRITE, nil, CREATE_ALWAYS, 0, 0);
WriteFile (FileHandle, thefile, dwSize, testCardinal, NIL);
CloseHandle (FileHandle);

How can I replace SetLength? I would like also to understand how Windows/Delphi allocates the array in memory etc. Thank you for your help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:37:48+00:00Added an answer on June 1, 2026 at 9:37 am

    You have several flaws in your code

    1. When you uses the CreateFile function you must check the returned handle against the INVALID_HANDLE_VALUE.

    2. Always you must use try .. finally to free the resources.

    3. You are trying to use the ReadFile function reading the contents inside of the thefile buffer using a index 1, this is wrong you must use a index 0 because the dynamic arrays are zero index based.

    4. you are using this code

    CloseHandle(CreateFileA (sfile, 0, 0,NIL, CREATE_NEW,
    FILE_ATTRIBUTE_NORMAL,0));

    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

      FileHandle := CreateFileA(sFile, GENERIC_WRITE, FILE_SHARE_WRITE, nil, CREATE_NEW, 0, 0);
    

    Try this improved version of your code.

    Btw, there is nothing wrong with use SetLengh, the problem is explained in the point 3.

    var
      thefile : array of AnsiChar;
    begin
    
      FileHandle := CreateFileA(paramstr0, GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
      if FileHandle <> 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 <> INVALID_HANDLE_VALUE then
      try
        WriteFile (FileHandle, thefile[0], dwSize, testCardinal, nil);
      finally
        CloseHandle (FileHandle);
      end;
    
      SetLength(TheFile, 0);    
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.