Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] Stdafx.h And Const Char* Error

stdafx.h const char* error

  • This topic is locked This topic is locked
6 replies to this topic

#1 Prasanna

Prasanna

    CC Lurker

  • New Member
  • Pip
  • 8 posts
  • Location:Kathmandu, Nepal
  • Learning:C

Posted 07 June 2012 - 04:27 AM

I was creating a C program for creating Windows Socket, using Visual Studio 2010. I got the following errors while trying to compile it:

1. error C1083: Cannot open include file: 'stdafx.h': No such file or directory [For the line, #include <stdafx.h>]

2. IntelliSense: cannot open source file "stdafx.h" [For the line, #include <stdafx.h>]

3. IntelliSense: a value of type "const char *" cannot be assigned to an entity of type "char" [For the line, buf[0] = " ";]

How to fix those problems, I tried google and solved other 4 errors but these three are being headache for me, I have just started Win32 Application in C so I don't have deep knowledge. I am waiting for your help.
  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 07 June 2012 - 04:35 AM

I guess you created empty 'Win32 Project' and added other files. So you can just remove the line "#include <stdafx.h>". About the 3'rd error, can you post your code here?

What I guess is that you have used a variable of type char but you are assigning a 'const char *' type value to that variable. Try declaring that variable as 'const char *'. That is your 'buf' should be declared as 'char *' or 'const char*'.
  • 0

#3 Prasanna

Prasanna

    CC Lurker

  • New Member
  • Pip
  • 8 posts
  • Location:Kathmandu, Nepal
  • Learning:C

Posted 07 June 2012 - 06:59 AM

Hello kernelcoder
Yes I have created empty 'Win32 Project'. But I'm not getting you clearly. Are you saying that I have to remove the line "#include <stdfx.h>" from the source code? And confused in the declaration of 'buf'. The code is like this:

#include <stdafx.h>
#include <windows.h>
#include <winsock.h>
#include <stdio.h>

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR LpCmdLine, int nCmdShow)
{
WSADATA ws;

char buf[1000], buf1[100], buf2[100], buf3[100], buf4[100], buf5[100], buf6[100];

WSAStartup (0x0101, &ws);

buf[0] = "";

sprintf (buf1, "\nWinsock Ver Requested: %d.%d", HIBYTE(ws.wVersion), LOBYTE(ws.wVersion));
sprintf (buf2, "\nWinsock Ver Available: %d.%d", HIBYTE(ws.wHighVersion), LOBYTE(ws.wHighVersion));
sprintf (buf3, "\nCurrent WinSock Implementation: %s", &ws.szDescription);
sprintf (buf4, "\nSystem Status: %s", &ws.szSystemStatus);
sprintf (buf5, "\nMaximum Sockets: %u", ws.iMaxSockets);
sprintf (buf6, "\nMaximum message size: %u", ws.iMaxUdpDg);

strcat (strcat (strcat (strcat (strcat (strcat (buf, buf1), buf2), buf3), buf4),buf5), buf6);

MessageBoxA(0, buf, "Info", 0);

WSACleanup();

return 0;
}
  • 0

#4 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 07 June 2012 - 07:11 AM

The following code compile for me successfully. But you need to link to library Ws2_32.lib. Read this tutorial to learn how to link a library -- read section 'How to Link to 3'rd Party Libraries'.


#include <windows.h>
#include <winsock.h>
#include <stdio.h>

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR LpCmdLine, int nCmdShow)
{
  WSADATA ws;

  char buf[1000], buf1[100], buf2[100], buf3[100], buf4[100], buf5[100], buf6[100];

  WSAStartup (0x0101, &ws);

  buf[0] = "";

  sprintf (buf1, "\nWinsock Ver Requested: %d.%d", HIBYTE(ws.wVersion), LOBYTE(ws.wVersion));
  sprintf (buf2, "\nWinsock Ver Available: %d.%d", HIBYTE(ws.wHighVersion), LOBYTE(ws.wHighVersion));
  sprintf (buf3, "\nCurrent WinSock Implementation: %s", &ws.szDescription);
  sprintf (buf4, "\nSystem Status: %s", &ws.szSystemStatus);
  sprintf (buf5, "\nMaximum Sockets: %u", ws.iMaxSockets);
  sprintf (buf6, "\nMaximum message size: %u", ws.iMaxUdpDg);

  strcat (strcat (strcat (strcat (strcat (strcat (buf, buf1), buf2), buf3), buf4),buf5), buf6);

  MessageBoxA(0, buf, "Info", 0);

  WSACleanup();

  return 0;
} 

  • 0

#5 Prasanna

Prasanna

    CC Lurker

  • New Member
  • Pip
  • 8 posts
  • Location:Kathmandu, Nepal
  • Learning:C

Posted 08 June 2012 - 05:44 AM

I searched my hard drive and got the path of 'WS2_32.Lib', after reading the tutorial I linked project to that library file.

The compilation was fine but I got a little bit of weird output. There appeared a MessageBox with full of strange characters upto 7th line,

and in 8th line there were few of them. After that only there were the actual info that I was waiting.

Then again I created a new project but this time I did not linked it to any library file. Instead of that I added following line just above

WinMain().

#pragma comment(lib, "WS2_32.Lib")

But again the output was same.

At last I edited the code. I removed 'A' from the line:

MessageBoxA(0, buf, "Info", 0);

And the coded line became like this:

MessageBox (0, buf, "Info", 0);

After compiling it, I got a message box full of strange characters without any english alphabate and without any numbers. I think I am having

problem with MessageBox();

Now how to solve that problem. Am I having trouble with MessageBox(), or there is any other problem that I am unable to figure it out.
  • 0

#6 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 08 June 2012 - 05:56 AM

Use the code
MessageBox (0, buf, L"Info", 0);

Note that, your buf variable needs to be declared as

wchar_t*

type.
  • 0

#7 Prasanna

Prasanna

    CC Lurker

  • New Member
  • Pip
  • 8 posts
  • Location:Kathmandu, Nepal
  • Learning:C

Posted 08 June 2012 - 11:55 PM

After the line - char buf[1000], buf1[100], buf2[100], buf3[100], buf4[100], buf5[100], buf6[100];
I added - strcpy(buf, " ");

Finally got the desired output. Problem Solved. :sleep:
  • 0





Also tagged with one or more of these keywords: stdafx.h, const char*, error

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download