WriteFile error__Wrong Parameter

after i have compile successfully
i meet an excute error-- can not write a file,

below is my code , how to amend it? Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
DWORD End_of_file = write_stuff.length() ; 
	 
LockFile(create , 0, 0, End_of_file, 0);
BOOL write_success ;

DWORD Bytes_that_need_to_write  = write_stuff.length() ;
LPDWORD BytesWritten =0 ;
wstring write_stuff = generate_string(); //generate_string() is a sub-function

write_success = WriteFile(
create, 
(LPCVOID)&write_stuff[0],
Bytes_that_need_to_write, 
BytesWritten, 
NULL
);

if (write_success == 0) // write fail
 {
         LPWSTR Error_message2 = 0;
		 DWORD err2 = GetLastError();
       
         ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                    NULL,
                    err2,
                    0,
                    (LPWSTR)&Error_message2,
                    0,
                    NULL) ;  // to make Error_message 
    
	     MessageBox(NULL,Error_message2 , L"Access Error_46",  MB_OK|MB_ICONWARNING ); 
 }

the error message(Error_message2) is "Wrong Parameter"

handle create is generate by creatFile before, i haven't post it~ ~

i think the wrong parameter should be the last 3 parameter--

(LPCVOID)&write_stuff[0],Bytes_that_need_to_write, BytesWritten, NULL
Last edited on
write_stuff is a class with a hidden implementation, you can't just pass it's address as you don't know what's in it. You need to pass the string with write_stuff.c_str().

You may need to add a cast to LPCVOID. If you do, please use the C++ casts (reinterpret_cast in this case), rather than C casts.

[EDIT]
Also, you need to declare BytesWritten as:
DWORD BytesWritten;
and pass it's address to WriteFile as &BytesWritten
Last edited on
so do u mean
write_stuff.c_str()
already is C++ casts (reinterpret_cast)

actually, i don't know what is cast~ ~
You've used a cast when you wrote (LPCVOID)write_stuff;

A cast is used to overrite the compiler's type system. Anyway, here's some code that uses WriteFile.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <windows.h>
#include <string>

int main()
{
	std::string name = "kbw";
	HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);

	DWORD written;
	WriteFile(hout, name.c_str(), name.size(), &written, 0);

	return 0;
}
thanks

make a note "reinterpret_cast"
http://www.cplusplus.com/doc/tutorial/typecasting/
Last edited on
now i change my code to below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 wstring write_stuff = generate_string(); 

	  BOOL write_success ;
         
	  sizeof (wchar_t); 
      DWORD length = (DWORD)write_stuff.length() ;
	  DWORD array_each_bytes = (DWORD)sizeof (write_stuff[1] );

      DWORD Bytes_that_need_to_write  = (DWORD) length* array_each_bytes ; 
      DWORD End_of_file = Bytes_that_need_to_write ;

	  DWORD BytesWritten; 

     LockFile(create , 0, 0, End_of_file, 0);
	 
     write_success = WriteFile( 
                    create, 
                    write_stuff.c_str(), 
                    Bytes_that_need_to_write, 
                    &BytesWritten, 
                    0 ); 


	 if (write_success == 0) // write fail
	   {
         LPWSTR Error_message2 = 0;
		 DWORD err2 = GetLastError();
       
         ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                    NULL,
                    err2,
                    0,
                    (LPWSTR)&Error_message2,
                    0,
                    NULL) ;      
	     MessageBox(NULL,Error_message2 , L"Access Error_46",  MB_OK|MB_ICONWARNING ); 
	 


but still get the same error - The parameter is incorrect~ ~
how to amend?
Last edited on
hi, kbw
can u tell me how to use reinterrupt-cast to change wstring to LPCVOID (for write_stuff)

1
2
3
4
class A {};
class B {};
A * a = new A;
B * b = reinterpret_cast<B*>(a); 


This is valid C++ code, although it does not make much sense, since now we have a pointer that points to an object of an incompatible class, and thus dereferencing it is unsafe.

copy from http://www.cplusplus.com/doc/tutorial/typecasting/

std::wstring * pString = reinterpret_cast <std::wstring *>(lpVoid);
Last edited on
can anyone answer me how to use reinterpret_cast to change write_stuff from wstring to LPVOID to use WriteFile?

thnaks
i find out the error maybe cause by the handle which i generate like this

create = CreateFile(LP_filename, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_FLAG_NO_BUFFERING , NULL);

the FILE_FLAG_NO_BUFFERING should not be use~ ~
Topic archived. No new replies allowed.