I have tried writing a simple program to get information from a website. I can't compile as I get the LNK2019 error for InternetReadFile, InternetOpenUrl, etc. and e.g.
1>GetInternetInfo.obj : error LNK2019: unresolved external symbol _imp_InternetReadFile#16 referenced in function _main
I assume that means I did not define these functions, that I did not include the correct library. I thought including #include would fix it, but it does not seem to help. I am running this on Visual Studio 2010 using C++. Below is my program. Any help is appreciated.
#include <string>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <wininet.h>
#include <winsock.h>
#include <stdio.h>
#include <stdarg.h>
using namespace std;
int main()
{
HINTERNET hOpen, hURL;
LPCWSTR NameProgram = L"Webreader"; // LPCWSTR == Long Pointer to Const Wide String
LPCWSTR Website;
char file[101];
unsigned long read;
//Always need to establish the internet connection with this funcion.
if ( !(hOpen = InternetOpen(NameProgram, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 )))
{
cerr << "Error in opening internet" << endl;
return 0;
}
Website = L"http://www.google.com";
hURL = InternetOpenUrl( hOpen, Website, NULL, 0, 0, 0 ); //Need to open the URL
InternetReadFile(hURL, file, 100, &read);
while (read == 100)
{
InternetReadFile(hURL, file, 100, &read);
file[read] = '\0';
cout << file;
}
cout << endl;
InternetCloseHandle(hURL);
return 0;
}
Please include "Wininet.lib" in your project settings.
Project->Properties->Configuration Properties->Linker->Input->Additional Dependencies
You can also add this line to your code after include section instead of adding library to the properties:
#pragma comment(lib, "wininet.lib")
Did you link to wininet.lib?
http://msdn.microsoft.com/en-us/library/aa385103(VS.85).aspx
Related
I am trying to check internet connection of the user by using internetcheckconnection().
The code:
#include <Wininet.h>
#include <iostream>
#include <string.h>
#include <windows.h>
#pragma comment(lib, "wininet.lib")
int main()
{
char url[128];
strcat(url, "http://www.techtoolbox.com");
bool bConnect = InternetCheckConnection(url, FLAG_ICC_FORCE_CONNECTION, 0);
if (bConnect) {
//internet connection exists !
std::cout << "yes";
}
else {
std::cout << "no ";
}
return 0;
}
But many error are coming up like
29 11 C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\Wininet.h [Error] 'LPVOID' does not name a type
30 11 C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\Wininet.h [Error] 'HINTERNET' does not name a type
32 11 C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\Wininet.h [Error] 'WORD' does not name a type
and 431 more .
I have already installed Wininet.lib, but still these error are coming . It would be kind of you if you could solve this easy problem :) .
LPVOID, HINTERNET and other types from your error messages are declared in windows.h. You should rearrange includes to fix these errors:
#include <windows.h>
#include <Wininet.h>
I would like to change user password on my Windows 7 PC using C++.
But when I compile it gets error:
undefined reference to 'NetUserChangePassword'
[Error] ld returned 1 exit status.`
How can I fix it?
Here is the MSDN page with the NetUserChangePassword function:
#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "netapi32.lib")
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <lm.h>
bool ChangeUserPassword(LPCWSTR OldPassword, LPCWSTR NewPassword)
{
NET_API_STATUS nStatus;
LPTSTR lp = new TCHAR[256];
DWORD dw = 256;
GetUserName(lp, &dw);
nStatus = NetUserChangePassword(NULL, lp, OldPassword, NewPassword);
delete[] lp;
if (nStatus == NERR_Success)
return true;
return false;
}
int main(int argc, char** argv)
{
LPCWSTR Old_P = L"C";
LPCWSTR New_P = L"D";
ChangeUserPassword(Old_P, New_P);
return 0;
}
I tried to link to the project the winapi32.dll in two ways
i tried to add using the project option
i tried to add following line
HINSTANCE hInst = LoadLibrary( L"C:\\Windows\\System32\\netapi32.dll ");
but i get always the same error
The requirements section of the MSDN topic you linked to states that you must link the Netapi32.lib library. That is the step that you have missed and explains the missing external error.
As to how to resolve the problem it is hard to say for sure. You are not using the MS compiler and so the #pragma approach won't work. Consult the docs for your compiler/linker to work out how to link this library.
It looks like you are using a GCC based compiler and so need to add -lnetapi32 to the options.
I am trying to run the following code to read the max processor throttle:
#include <iostream>
#include <string>
#include <tchar.h>
#include <windows.h>
#include <Powerbase.h>
#include <PowrProf.h>
using namespace std;
int main()
{
SYSTEM_POWER_CAPABILITIES sysc;
while (GetPwrCapabilities(&sysc)) {
cout << " Your Max Throttle is: " << static_cast<double> (sysc.ProcessorMaxThrottle) << endl;
}
Sleep(10000);
return 0;
}
However , I am receiving the following error:
...error LNK2019: unresolved external symbol _GetPwrCapabilities#4 referenced in function _main
...fatal error LNK1120: 1 unresolved externals
I tried the solution related to make sure the linker set to console program but it does not work. Any suggestion ?
I am using MSVS 2013.
Thanks
To make it work, you need to add PowrProf.lib to your project setting:
Project Properties -> Linker -> Input -> Additional Dependencies
I'm using the following code (Allegro 4, C++), and getting the following error:
#include <allegro.h>
//defines
#define MODE GFX_SAFE
#define WIDTH 640
#define HEIGHT 480
int main (void)
{
int ret;
int counter;
//initialize allegro
allegro_init();
install_keyboard();
install_timer();
srand(time(NULL));
//set up screen
//set video mode
ret = set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
if (ret != 0)
allegro_message(allegro_error);
allegro_exit();
return 0;
}
Error:
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
All the previous answers regarding that error tell me to switch to "Console" from "Windows"; but I already have "Console" in Properties->Linker->System->Subsystem.
If you don't have an answer, I'd be happy with something I could do to help narrow down the problem: I've used Allegro with C, but I want to use C++ to take advantage of OOP, and so I still have a lot of work to do.
Update:
#include <iostream>
#include <allegro.h>
using namespace std;
int main ()
{
cout << "Hello World";
return 0;
}
doesn't work, but
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World";
return 0;
}
does.
Now what? Answer: Start with Empty project.
Update2: restarted with an empty project, same code. First block (alleg.lib in linker, but allegro.h not included) works, second code (allegro.h included) doesn't. However, the bug is different:
1>LINK : fatal error LNK1561: entry point must be defined
What now?
Edit^2:Ignore all the following: I forgot to go back to including Allegro. It works now. Thanks everyone for the answers.
Edit: Adding:
END_OF_MAIN()
or
int END_OF_MAIN()
give the error "fatal error C1004: unexpected end-of-file found"
You are getting the error because you are attempting to integrate allegro into a project that is non-empty.
You must create the project as an EMPTY PROJECT type:
New... > Project... > Visual C++ > Empty Project
--EDIT FOR SECOND ERROR--
You must append END_OF_MAIN() after the closing brace of int main():
int main() {
//...
}
END_OF_MAIN()
I am really new to Opencv. After downloading and installing Opencv 2.4 according to the instruction, I began writing my first Opencv program, which was basically a copy of the tutorial on the web.
#include <stdio.h>
#include <iostream>
#include <vector>
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main( int argc, char** argv )
{
char* filename = "C:\\Research\abc.pgm";
IplImage *img0;
if( (img0 = cvLoadImage(filename,-1)) == 0 )
return 0;
cvNamedWindow( "image", 0 );
cvShowImage( "image", img0 );
cvWaitKey(0);
cvDestroyWindow("image");
cvReleaseImage(&img0);
return 0;
}
The codes work very well, but you may notice that in the above code invoking Opencv function is in a C code fashion. I therefore decide to proceed with C++ code fashion with the following codes:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
However, in this case the program has several link errors although compiling seems fine. The link errors I have received are as follows:
Error 2 error LNK2019: unresolved external symbol "void __cdecl cv::namedWindow(class stlp_std::basic_string<char,class stlp_std::char_traits<char>,class stlp_std::allocator<char> > const &,int)" (?namedWindow#cv##YAXABV?$basic_string#DV?$char_traits#D#stlp_std##V?$allocator#D#2##stlp_std##H#Z) referenced in function _main C:\Research\OpencvTest\OpencvTest.obj
Error 1 error LNK2019: unresolved external symbol "void __cdecl cv::imshow(class stlp_std::basic_string<char,class stlp_std::char_traits<char>,class stlp_std::allocator<char> > const &,class cv::_InputArray const &)" (?imshow#cv##YAXABV?$basic_string#DV?$char_traits#D#stlp_std##V?$allocator#D#2##stlp_std##ABV_InputArray#1##Z) referenced in function _main C:\Research\OpencvTest\OpencvTest.obj
I am quite sure that I have added necessary Opencv libraries in my program (I use VC10), and the additional libraries I have added are as follows:
stl_port.lib
opencv_highgui242d.lib
opencv_core242d.lib
I was wondering what's wrong with my setting. Why does it work for the first program but not the second one? Any ideas will be appreciated. Thanks!
It has something to do with mixing STLPort and MSVC STL. You probably didn't build OpenCV libraries yourself, so they're using VC10 STL. With C interface there's just char* but with C++ interface linker gets confused with std::string in methods. You should see the same result with imread if you cast it's input to string too.
Can I mix STL implementations in my project?