Question about WINAPI funcation!

Apr 12, 2009 at 2:05pm
Hi,guys!

I want to write a advance I/O class,that can copy file as the OVERLAPPED_COMPLETION_ROUTINE,
the class's snippet:
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
//myclass.h
class AsynCopy{
HANDLE fin;
HANDLE fou;
bool done;
DWORD startsize;
DWORD destsize;
BYTE* buf;
int bufSize;

public:

AsynCopy(HANDLE from,HANDLE to,DWORD start,DWORD dest):fin(from),fou(to),startsize(start),destsize(dest),done(false),buf(0),bufSize(0){};
~AsynCopy();

void Go();
bool my_read();
void my_write(DWORD);
void WINAPI readCompletion(DWORD, DWORD, LPOVERLAPPED);  //typing error, should be readCompFuc(...)
void WINAPI writeCompletion(DWORD, DWORD, LPOVERLAPPED);
}


//the cpp file
boo AsynCopy::my_read(){
.....
BOOL fRead = ReadFileEx(fin,buf,bufMax,ov1,readCompFuc);  //error 2664
.....
}

void AsynCopy::my_write(){
.....
BOOL fWrite = WriteFileEx(fin,buf,bufMax,ov1,writeCompFuc);  //error 2440
.....
}


There are two errors when I complie it with vs2003,
one is :
1
2
BOOL wret=WriteFileEx(fout,buf,bufMax,ov1,(LPOVERLAPPED_COMPLETION_ROUTINE)writeCompFuc);
d:\c++\vs23\filePro\spd2\AsynFileCopy.cpp(76): error C2440: can't convert type from “overloaded-function”to“LPOVERLAPPED_COMPLETION_ROUTINE” 


another is same:
1
2
BOOL fRead = ReadFileEx(fin,buf,bufMax,ov1,readCompFuc);
d:\c++\vs23\filePro\spd2\AsynFileCopy.cpp(76): error C2664: “ReadFileEx” : can't convert the type from“void (DWORD,DWORD,LPOVERLAPPED)”to “LPOVERLAPPED_COMPLETION_ROUTINE” 


I wrote these WINAPI funcations as MSND's example! :(

So I want to ask:

Is there any rules to write the WINAPI funcation? what are them? and what's the rule writing them in class?

thanks!

Last edited on Apr 14, 2009 at 3:22pm
Apr 12, 2009 at 5:33pm
what is readCompFuc??

i think it should be like this:

BOOL wret=WriteFileEx(fout,buf,bufMax,ov1, (LPOVERLAPPED_COMPLETION_ROUTINE)writeCompletion);
Apr 13, 2009 at 1:44am
Sorry!

I wrote these questions, and forgot to check them!

It appear the first error when I use this code:

1
2
3
4
5
6
7
8
....
BOOL fRead = ReadFileEx(fin,buf,bufMax,ov1,readCompFuc);
.....

Error:

d:\c++\vs23\filePro\spd2\AsynFileCopy.cpp(76): error C2664: “ReadFileEx” : can't convert the type from“void (DWORD,DWORD,LPOVERLAPPED)”to “LPOVERLAPPED_COMPLETION_ROUTINE”
 


Then I change to:

1
2
3
4
5
6
7
8

.....
BOOL wret=WriteFileEx(fout,buf,bufMax,ov1,(LPOVERLAPPED_COMPLETION_ROUTINE)writeCompFuc);
......

Error:
d:\c++\vs23\filePro\spd2\AsynFileCopy.cpp(76): error C2440: can't convert type from “overloaded-function”to“LPOVERLAPPED_COMPLETION_ROUTINE”
 


How to write this kind code?
Apr 13, 2009 at 4:44am
Actually i am asking where is writeCompFuc function written. whats the signature of that function?
for WriteFileEx to work the signature of the last parameter should be:

void (*fn)(DWORD, DWORD, LPOVERLAPPED);
Apr 13, 2009 at 6:44am
hi writetonsharma!

I declared writeCompFuc function in header file, and omitted it in cpp file, because it hatos a regular struction to be written!

Someone told me the problem is: The WINAPI Function like as writeCompFuc() is a C program, not C++. so it's not appropriate for writing it in a c++ class!

thanks





Apr 13, 2009 at 7:00am
thats what i am asking,, how it is declared.. can you paste the declaration of the function.
because your code looks fine, just want to see the declaration of writeCompFuc() function.


for an example how to use it see this:
http://msdn.microsoft.com/en-us/library/aa365601(VS.85).aspx


here they have used ReadFileEx and WriteFileEx functions.

Apr 14, 2009 at 3:35pm
hi writetonsharma!

The writeCompFuc() is same as the routine of the msdn's web page which you referred to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
VOID WINAPI CompletedReadRoutine(DWORD dwErr, DWORD cbBytesRead, LPOVERLAPPED lpOverLap) 
{
    
if ((dwErr == 0) 
  my_read();
else {

if(GetLastError == Eof)
  done=true;
else
  throw("error in write!");
}   
      
} 
 


Hope it can help you!

:)
Last edited on Apr 14, 2009 at 3:36pm
Apr 14, 2009 at 4:03pm
looks like a dead end for me..

LPOVERLAPPED_COMPLETION_ROUTINE is declared like this:
1
2
3
4
5
typedef VOID (*LPOVERLAPPED_COMPLETION_ROUTINE) (
        [in] DWORD  dwErrorCode,
        [in] DWORD  dwNumberOfBytesTransfered,
        [in] LPVOID lpOverlapped
);


and you have declared as: void (DWORD, DWORD,
LPOVERLAPPED
);
try changing lpoverlapped to lpvoid,it may help.

otherwise i am not able to think why its giving error??!!!
Apr 14, 2009 at 6:36pm
Your completion routine has the wrong signature. If it's going to be a member of a class it must be static, as it doesn't know about your class or it's this pointer.

Using async WIN32 I/O is tricky at best. I can't see how your class makes it any easier from what you've published to date.
Apr 15, 2009 at 5:32am
kbw:


he already said the function is not a member of class, its an independent function.

I declared writeCompFuc function in header file, and omitted it in cpp file, because it hatos a regular struction to be written!

Someone told me the problem is: The WINAPI Function like as writeCompFuc() is a C program, not C++. so it's not appropriate for writing it in a c++ class!


Apr 15, 2009 at 9:58am
Ah, I missed that.
Apr 15, 2009 at 1:44pm
:)

thanks, writetonsharma!

Another discussion page about my this problem is in msdn:

http://social.msdn.microsoft.com/Forums/en-US/windowsgeneraldevelopmentissues/thread/d17ef1d9-98d7-4afd-909c-ac9f117d4e88

You can visit it if you want to see more detail.


Apr 15, 2009 at 2:05pm
did you find any solution??
Apr 15, 2009 at 2:31pm
i saw that post.. those guys are just bullshitting.. i dont think you will find any solution there..
atleast i didnt find anyone who is trying to solve your problem apart from showing they are very talented people..

this is for you...might help you in implementing what you want..what you are trying to do in your first post..

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
#include <iostream>
using namespace std;
typedef void (*FN)(const char*);

class test
{
public:
        void print(const char* msg)
        {
                cout << msg << endl;
        }

        void call_print(FN fn_addr, const char *msg)
        {
                test *t = (test*)fn_addr;
                t->print(msg);
        }

        void c_call_print()
        {
                FN fn = (FN)this;
                call_print(fn, "Hello World");
        }
private:

};

int main()
{
        test t;
        t.c_call_print();
        return 0;
}
Apr 16, 2009 at 10:45am
Hi writetonsharma!

Your code get me a great help to understand the "static"!

haaaa..

My program is done! thanks a lot

The snippet is:

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

//the header file
class AsynFileCopy
{ 
	struct m_ov{
		OVERLAPPED ov;
		AsynFileCopy* maddr;
	}  *Rov,* Wov;
	HANDLE fin;
	HANDLE fout;
	unsigned long Rstart;
	unsigned long Wstart;
	unsigned long fileSize;
	bool done;
	unsigned char* buf;
	int  bufMax;
	unsigned long t1;
....

public:
	AsynFileCopy(HANDLE in,HANDLE ou ,unsigned long s1,unsigned long s2,unsigned long ds):fin(in),fout(ou),Rstart(s1),Wstart(s2),fileSize(ds),done(false),Rov(0),Wov(0),buf(0),bufMax(4096){};
	~AsynFileCopy(void);

	unsigned long go();
	bool isDone(){ return done;}
	unsigned long  runTime() {return t1;}

	bool readBuf();
    static void WINAPI readCompFuc(DWORD,DWORD, LPOVERLAPPED);
	void writeBuf(DWORD);
	static void WINAPI writeCompFuc(DWORD,DWORD, LPOVERLAPPED);
};


//cpp.file

void WINAPI AsynFileCopy::readCompFuc(DWORD dwErr,DWORD dwBytes, LPOVERLAPPED pOv){
	m_ov* Ov1 = (m_ov*)pOv;
	AsynFileCopy* me= Ov1->maddr;
	if(dwErr ==0){
		me->writeBuf(dwBytes);
	}
	else if(dwErr == ERROR_HANDLE_EOF)
		me->done= true;
	else
		std::cout <<"Error in Read Completion!\n";
}

void WINAPI AsynFileCopy::writeCompFuc(DWORD dwErr,DWORD dwBytes, LPOVERLAPPED pOv){
	m_ov* Ov1= (m_ov*)pOv;
	AsynFileCopy* me= Ov1->maddr;
	if(dwErr ==0){
		Ov1->ov.Offset += me->bufMax;
		me->readBuf();
	}
	else if(dwErr == ERROR_HANDLE_EOF)
		me->done= true;
	else
		std::cout <<"Error in Write Completion!\n";
}

..........

void AsynFileCopy::writeBuf(DWORD dwBytes){
.........
	BOOL wret=WriteFileEx(fout,buf,dwBytes,(LPOVERLAPPED)Wov,writeCompFuc);
.........
}


bool AsynFileCopy::readBuf(){
.......
	BOOL fRead = ReadFileEx(fin,buf,bufMax,(LPOVERLAPPED)Rov,readCompFuc);
.........
}


:)

The lucky thing is the LPOVERLAPPED_COMPLETION_ROUTINE need a pointer as its argument!

The pointer to address is armusing powrful !
Last edited on Apr 22, 2009 at 7:21am
Apr 18, 2009 at 7:26am
There are some problems in my code!

Now it's corrected!

Apr 19, 2009 at 9:57am
oh.. thats great..

was busy since a couple of days thats why could not answer..
anyways good to hear you solved the problem.. :)
Apr 22, 2009 at 7:19am
:)

Thanks!

Is it a great business to you! Share it to the firends of online!

haaaa.....

Good lucky!
Topic archived. No new replies allowed.