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 4324062
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:04:56+00:00 2026-05-21T09:04:56+00:00

I started a new project with type of Windows Forms application, and I put

  • 0

I started a new project with type of Windows Forms application, and I put two textboxes (textbox1 and textbox2) and a button. I used OpenFileDialog to select a file from the system and put it’s path in textbox1, I put the following code for the button:

HANDLE hFile;
HANDLE hMap ;
LPVOID base;

hFile = ::CreateFile((LPCWSTR)Marshal::StringToHGlobalAnsi(this->textBox1->Text).ToPointer(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,0,OPEN_EXISTING , FILE_FLAG_SEQUENTIAL_SCAN, 0);

unsigned long sifi= ::GetFileSize(hFile,NULL);

if(hFile !=INVALID_HANDLE_VALUE){
hMap= ::CreateFileMapping(hFile, 0, PAGE_READONLY | SEC_COMMIT, 0, 0, 0);//create Mem mapping for the file in virtual memory
 }
if( hMap!=NULL){
base = ::MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);//load the mapped file into the RAM
                            }
this->textBox2->Text=sifi.ToString();

What I am trying to do with that code is to read the file path from textbox1 to use it for openning a file handle and then get the size of the file and put it into textbox2. The problem now is, textbox2 shows incorrect value of the file size. It seems always like 4294967295 for all files!

Edit:

Thanks guys, I have solved the problem. It was in the first parameter of CreateFile, it supposes to be:

(LPCWSTR)Marshal::StringToHGlobalUni(this->textBox1->Text).ToPointer()
  • 1 1 Answer
  • 1 View
  • 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-05-21T09:04:57+00:00Added an answer on May 21, 2026 at 9:04 am

    It’s recommended to use GetFileSizeEx instead of GetFileSize. But think your CreateFile call failed.

    CreateFile doesn’t accept an HGLOBAL. And you’re converting the string to ANSI, then passing it to a Unicode version of CreateFile, which is also broken.

    Just stay in Unicode, like this:

    pin_ptr<wchar_t> wszFilename = PtrToStringChars(textBox1->Text);
    HANDLE hFile = ::CreateFileW(wszFilename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,0,OPEN_EXISTING , FILE_FLAG_SEQUENTIAL_SCAN, 0);
    if (hFile == 0 || hFile == INVALID_HANDLE_VALUE) throw gcnew Win32Exception();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting a FileNotFoundException on a Windows Forms Application project, with the following
I just started using the new project type SQL Server 2008 Database. I can
We started a new project and the nature of the project is very interactive
I have just started a new project, and I am using the Delphi 2009
I started doing a new project in PHP / MySql . The Aim of
I have started a new mvc project and clicked on project\properties\web - use local
I recently started a new personal project to learn Entity Framework. My end goal
I've recently started my new private project. The main goal which I want to
I'm working on a fairly new project and we started from scratch. So not
I have started a a new project (to refactor some code), and just can't

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.