473,323 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Keeping changes after WriteFile, why it doesn' happen

I'm trying to Write to my flash memory,, I've read the firts 512 bytes, modified that array changing some bytes for others and then using WriteFile API function, wrote (or tried) again those 512 first bytes into my memory again.. this WriteFile API function return 1 , after that I use CloseHandle API function closing the handle I used to interact with the memory...but, when I see my memory using WinHex(hex editor) the bytes there are the same before my program tries to write. why is this if I am even closing the handle and after close it my buffer(array of bytes read from my memory) seems modified.
My code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. SafeFileHandle ptrFile = CreateFile("\\\\.\\u:", DesiredAccess.GENERIC_READ | DesiredAccess.GENERIC_WRITE, ShareMode.FILE_SHARE_READ_AND_WRITE, IntPtr.Zero, CreationDisposition.OPEN_EXISTING, FlagsAndAttributes.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
  3. byte[] buffer = new byte[512];   
  4. uint cantleidos = 0;
  5.  
  6. try
  7. {      
  8.     bool retor = SetFilePointerEx(ptrFile, 5, IntPtr.Zero, MoveMethod.FILE_BEGIN);
  9.     bool b = ReadFile(ptrFile, buffer,512 , out cantleidos, IntPtr.Zero);
  10. }
  11. catch (Exception)
  12. {
  13.     throw new Win32Exception(Marshal.GetLastWin32Error());
  14. }
  15.  
  16. Buffer.BlockCopy(System.Text.Encoding.UTF8.GetBytes("SOMETEXT".ToCharArray()), 0, buffer,3,"SOMETEXT".ToCharArray().Length);
  17.  
  18. uint wrote = 0;
  19. bool c = WriteFile(ptrFile, buffer, (uint)buffer.Length, ref wrote, IntPtr.Zero);
  20. int closed = CloseHandle(ptrFile);
  21.  
Jul 8 '11 #1
3 1775
Plater
7,872 Expert 4TB
You probably need to do some sort of FLUSH mechanism?
Jul 11 '11 #2
Christian Binder
218 Expert 100+
Is the content of wrote-variable after line 19 correct? (should be 512, the same as buffer.Length)

you could print the buffer using string.Join(",", buffer), comparing it with the results in your hex-editor (maybe the file's bytes are already changed ... just a thought)

you could also try to use a file on your local hard drive to see if their is a difference.
Jul 12 '11 #3
Plater
7,872 Expert 4TB
wrote is passed by reference, meaning the function is changing it. It would be worthwhile to check the results of that value and value of c
Jul 12 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: dim | last post by:
Copied directly from exaple book but not working.... All i get is an empty 0 byte file Call to GetLastError directly after the call to WriteFile returns 0 (NO_ERROR) but no data sees to be written...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
4
by: Egil Hansen | last post by:
Hi all Do C# and .net have some way to monitor a folder in Windows XP/Windows 2000. I want to mirror the content of the folder, including subfolders (if any exsists) to another place, and I want...
1
by: Paul | last post by:
Hi, I am extending an existing MFC app to use Unicode (for a Japanese version of the interface elements). The app's purpose is to control a peripheral device through the serial port, and the...
2
by: Carter | last post by:
i have a treeview on a webform (.aspx). when the user selects an appropriate node on the tvw. and clicks on a link button, i'm downloading a corresponding file to the client (from the server). so far...
30
by: Charles Law | last post by:
Here's one that should probably have the sub-heading "I'm sure I asked this once before, but ...". Two users are both looking at the same data, from a database. One user changes the data and...
6
by: Ian Boyd | last post by:
Every time during development we had to make table changes, we use Control Center. Most of the time, Control Center fails. If you try to "undo all", it doesn't, and you end up losing your identity...
3
by: The Coding Queen | last post by:
Okay guys....I have a combo box that I have made show red when I set the value to "archived" it works (hoorah!) BUT when I load the form it's all back to plain Jane black writing even though the...
5
by: Khafancoder | last post by:
Hi guys, i am building a FileSharing website, i wanna allow users to be able using download managers such as DAP to download files from webserver but don't allow them to retrive files url......
3
by: tshad | last post by:
Using asp.net 2.0, I am finding that at times, the old javascript will still be there. I was working with it for a couple of hours and the changes seem to happen. But at the end of the day, I...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.