user2178841
user2178841

Reputation: 859

Difference Between WINAPI in C and C++

I am reading data from driver. The driver came with examples on how to develop applications based on the driver. The examples were written few years back. They use WINAPI and C. Now I will use their some header files. They have data structures and various other stuffs defined.

I tried creating a WINAPI in C++ and tried to link to those files. But as explained here, in the last answer, I very much believe that the same is problem with my code.

Now, I can't do as suggested there. My programs are long and I can't mess header files. They are complicated.

My option is to create my project entirely in C (I hope, it solves).

First, I renamed the file .cpp t0 .c. (I don't even know the difference between these two programming languages. Their difference made no difference so far.) MAIN QUESTION

I used file IO using API and used following:

HANDLE myFile=CreateFile("filename.txt",FILE_APPEND_DATA,FILE_SHARE_WRITE,0,\
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
int BufferNo=sprintf(body,"%.5f %[3].5f %[3].5f %[3].5f %[3].5f %[3].5f %[3].5f \
%[3].5f %[3].5f \n",a1,a2,a3,a4,a5,a6,a7,a8,a9);
WriteFile(myFile,body,lstrlen(body),0,NULL);
CloseHandle(myFile);

Problem is it does not compile. Says errors like:

error C2275: 'HANDLE' : illegal use of this type as an expression
error C2146: syntax error : missing ';' before identifier 'myFile'
error C2065: 'myFile' : undeclared identifier
warning C4047: '=' : 'int' differs in levels of indirection from 'HANDLE'
error C2143: syntax error : missing ';' before 'type'
error C2065: 'myFile' : undeclared identifier
warning C4022: 'WriteFile' : pointer mismatch for actual parameter 1
error C2065: 'myFile' : undeclared identifier
warning C4022: 'CloseHandle' : pointer mismatch for actual parameter 1

These were not errors in C++ and they compiled, just did not link. How do I solve this.

Note, there is similar function I have used before this. Which is not detected as error.

HANDLE myFile=CreateFile("filename.txt",GENERIC_WRITE,FILE_SHARE_WRITE,0,\
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,0);
char* HeadingStr="a1(m)   a2(m)   a3(m)   a4(m)   a5(m)   a6(m)   a7(m)\
a8(m) a9(m)\n";
WriteFile(myFile,HeadingStr,lstrlen(HeadingStr),0,NULL);
CloseHandle(myFile);

This does not show any error. It is in same file and is before, in different function above errors. This is in WinMain and above is in WndProc function.

Upvotes: 1

Views: 1213

Answers (1)

Drew McGowen
Drew McGowen

Reputation: 11706

The Visual Studio compiler does not support C99, which is when inline declarations were added. You need to declare all of your variables at the beginning of your functions, or switch to a compiler that supports C99.

Upvotes: 6

Related Questions