How to call postgresql methods from console application VS2015? - c++

I have created a new console application project.
I copied source "Example Program 1" from this link.
Then I go to "Project properties"->C/C++->General->"Additional Include Directories" and add path "C:\Program Files\PostgreSQL\9.5\include\".
Then I go to "Project properties"->Linker->General->"Additional Library Directories" and add path "C:\Program Files\PostgreSQL\9.5\lib".
Then I go to "Project properties"->Linker->Input->Additional Dependencies" and add name of the file "libpq.lib".
And then I compiled it.
1>------ Сборка начата: проект: ConsoleApplication1, Конфигурация: Debug Win32 ------
1>ConsoleApplication1.obj : error LNK2019: ссылка на неразрешенный внешний символ _PQconnectdb в функции _main
1>ConsoleApplication1.obj : error LNK2019: ссылка на неразрешенный внешний символ _PQfinish в функции "void __cdecl exit_nicely(struct pg_conn *)" (?exit_nicely##YAXPAUpg_conn###Z)
1>ConsoleApplication1.obj : error LNK2019: ссылка на неразрешенный внешний символ _PQstatus в функции _main
1>ConsoleApplication1.obj : error LNK2019: ссылка на неразрешенный внешний символ _PQerrorMessage в функции _main
1>ConsoleApplication1.obj : error LNK2019: ссылка на неразрешенный внешний символ _PQexec в функции _main
1>ConsoleApplication1.obj : error LNK2019: ссылка на неразрешенный внешний символ _PQresultStatus в функции _main
1>ConsoleApplication1.obj : error LNK2019: ссылка на неразрешенный внешний символ _PQntuples в функции _main
1>ConsoleApplication1.obj : error LNK2019: ссылка на неразрешенный внешний символ _PQnfields в функции _main
1>ConsoleApplication1.obj : error LNK2019: ссылка на неразрешенный внешний символ _PQfname в функции _main
1>ConsoleApplication1.obj : error LNK2019: ссылка на неразрешенный внешний символ _PQgetvalue в функции _main
1>ConsoleApplication1.obj : error LNK2019: ссылка на неразрешенный внешний символ _PQclear в функции _main
1>C:\Users\test\Documents\Visual Studio 2015\Projects\ConsoleApplication1\Debug\ConsoleApplication1.dll : fatal error LNK1120: неразрешенных внешних элементов: 11
========== Сборка: успешно: 0, с ошибками: 1, без изменений: 0, пропущено: 0 ==========
Compiler commands:
/GS /analyze- /W3 /Zc:wchar_t /I"C:\Program Files\PostgreSQL\9.5\include\" /ZI /Gm /Od /Fd"Debug\vc140.pdb" /Zc:inline /fp:precise /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_WINDLL" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /Oy- /MDd /Fa"Debug\" /nologo /Fo"Debug\" /Fp"Debug\ConsoleApplication1.pch"
Linker commands:
/OUT:"C:\Users\test\documents\visual studio 2015\Projects\ConsoleApplication1\Debug\ConsoleApplication1.dll" /MANIFEST /NXCOMPAT /PDB:"C:\Users\test\documents\visual studio 2015\Projects\ConsoleApplication1\Debug\ConsoleApplication1.pdb" /DYNAMICBASE "libpq.lib" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /IMPLIB:"C:\Users\test\documents\visual studio 2015\Projects\ConsoleApplication1\Debug\ConsoleApplication1.lib" /DEBUG /DLL /MACHINE:X86 /INCREMENTAL /PGD:"C:\Users\test\documents\visual studio 2015\Projects\ConsoleApplication1\Debug\ConsoleApplication1.pgd" /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"Debug\ConsoleApplication1.dll.intermediate.manifest" /ERRORREPORT:PROMPT /NOLOGO /LIBPATH:"C:\Program Files\PostgreSQL\9.5\lib" /TLBID:1
What I missed?

Related

C++ WinHTTP library generating symbol errors [duplicate]

Trying to compile a sample http class with the SDK, and getting some strange link errors... I am sure its something to do with a missing option, or directory...
I am no expert in c++ as you can see, but looking for any assistance.
I included my sample class. I also did install the Windows SDK. If you need any other information about my setups or anything, please ask. I'd prefer someone point me to a working WinHttp SDK sample project.
//START OF utils.cpp
#pragma once
#include "stdafx.h"
class http
{
public:
http();
~http();
std::string getText();
};
//END OF utils.cpp
//START OF utils.cpp
#include "stdafx.h"
#include "utils.h"
http::http()
{
}
http::~http()
{
}
std::string http::getText()
{
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen( L"WinHTTP Example/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0 );
// Specify an HTTP server.
if( hSession )
hConnect = WinHttpConnect( hSession, L"www.microsoft.com",
INTERNET_DEFAULT_HTTPS_PORT, 0 );
// Create an HTTP request handle.
if( hConnect )
hRequest = WinHttpOpenRequest( hConnect, L"GET", NULL,
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_SECURE );
// Send a request.
if( hRequest )
bResults = WinHttpSendRequest( hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0,
0, 0 );
// End the request.
if( bResults )
bResults = WinHttpReceiveResponse( hRequest, NULL );
// Keep checking for data until there is nothing left.
if( bResults )
{
do
{
// Check for available data.
dwSize = 0;
if( !WinHttpQueryDataAvailable( hRequest, &dwSize ) )
printf( "Error %u in WinHttpQueryDataAvailable.\n",
GetLastError( ) );
// Allocate space for the buffer.
pszOutBuffer = new char[dwSize+1];
if( !pszOutBuffer )
{
printf( "Out of memory\n" );
dwSize=0;
}
else
{
// Read the data.
ZeroMemory( pszOutBuffer, dwSize+1 );
if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded ) )
printf( "Error %u in WinHttpReadData.\n", GetLastError( ) );
else
printf( "%s", pszOutBuffer );
// Free the memory allocated to the buffer.
delete [] pszOutBuffer;
}
} while( dwSize > 0 );
}
// Report any errors.
if( !bResults )
printf( "Error %d has occurred.\n", GetLastError( ) );
// Close any open handles.
if( hRequest ) WinHttpCloseHandle( hRequest );
if( hConnect ) WinHttpCloseHandle( hConnect );
if( hSession ) WinHttpCloseHandle( hSession );
return "";
}
//END OF utils.cpp
1>------ Build started: Project: winagent, Configuration: Debug Win32 ------
1>Compiling...
1>utils.cpp
1>Linking...
1> Creating library C:\winagent\Debug\winagent.lib and object C:\winagent\Debug\winagent.exp
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpCloseHandle#4 referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" (?getText#http##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpReadData#16 referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" (?getText#http##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpQueryDataAvailable#8 referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" (?getText#http##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpReceiveResponse#8 referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" (?getText#http##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpSendRequest#28 referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" (?getText#http##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpOpenRequest#28 referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" (?getText#http##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpConnect#16 referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" (?getText#http##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpOpen#20 referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" (?getText#http##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
1>C:\winagent\Debug\winagent.exe : fatal error LNK1120: 8 unresolved externals
1>Build log was saved at "file://c:\winagent\Debug\BuildLog.htm"
1>winagent - 9 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
If you check the MSDN reference for the WinHttp* functions you will see that you need to link with the library Winhttp.lib.
Open the project settings, select the linker options then 'input' and add WinHttp.lib to the 'Additional Dependencies' list.
Or you could put
#pragma comment(lib, "winhttp.lib")
(as previously mentioned) in your source code.
You need to link to winhttp.lib
Change the project settings or add this line to your .cpp file
#pragma comment(lib, "winhttp")
You've not added the WinHttp library to your link list.
Make sure you are linking with Winhttp.lib.

I cant get this email program that I found to work

I found this email program and I can't get it to run. I am using visual studio 2013 c++. I would take any other programs too if you know of any. All I need is to be able to put this in my existing code to email a number to a gmail account. My error is:
error:
1>------ Build started: Project: sending email, Configuration: Debug Win32 ------
1>cl : Command line warning D9007: '/Gm' requires '/Zi or /ZI'; option ignored
1> Source.cpp
1>c:\users\kyle\documents\visual studio 2013\projects\sending email\sending email\source.cpp(127): warning C4715: 'MailIt' : not all control paths return a value
1>Source.obj : error LNK2019: unresolved external symbol _closesocket#4 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>Source.obj : error LNK2019: unresolved external symbol _connect#12 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>Source.obj : error LNK2019: unresolved external symbol _htons#4 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>Source.obj : error LNK2019: unresolved external symbol _recv#16 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>Source.obj : error LNK2019: unresolved external symbol _send#16 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>Source.obj : error LNK2019: unresolved external symbol _socket#12 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>Source.obj : error LNK2019: unresolved external symbol _gethostbyname#4 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>Source.obj : error LNK2019: unresolved external symbol _WSAStartup#8 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>Source.obj : error LNK2019: unresolved external symbol _WSACleanup#0 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\kyle\documents\visual studio 2013\Projects\sending email\Debug\sending email.exe : fatal error LNK1120: 10 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
code:
#include <windows.h>
#include <stdio.h>
#include <winuser.h>
#include <windowsx.h>
#include <time.h>
/*If you don't know the mail exchange server for an address for the following
"nslookup -querytype=mx gmail.com" but replace gmail.com with the domain for
whatever email address you want. YOU MUST CHANGE THESE SETTINGS OR
IT WILL NOT WORK!!! */
#define BUFSIZE 800
#define waittime 500
#define cmailserver "gmail-smtp-in.l.google.com"
#define cemailto "kmtompkinscode#gmail.com"
#define cemailfrom "kmtompkinscode#gmail.com"
#define LogLength 100
#define SMTPLog "smtp.log"
#define cemailsubject "pin"
int MailIt(char *mailserver, char *emailto, char *emailfrom, char *emailsubject,
char *emailmessage)
{
SOCKET sockfd;
WSADATA wsaData;
FILE *smtpfile;
#define bufsize 300
int bytes_sent; /* Sock FD */
int err;
struct hostent *host; /* info from gethostbyname */
struct sockaddr_in dest_addr; /* Host Address */
char line[1000];
char *Rec_Buf = (char*)malloc(bufsize + 1);
smtpfile = fopen(SMTPLog, "a+");
if (WSAStartup(0x202, &wsaData) == SOCKET_ERROR) {
fputs("WSAStartup failed", smtpfile);
WSACleanup();
return -1;
}
if ((host = gethostbyname(mailserver)) == NULL) {
perror("gethostbyname");
exit(1);
}
memset(&dest_addr, 0, sizeof(dest_addr));
memcpy(&(dest_addr.sin_addr), host->h_addr, host->h_length);
/* Prepare dest_addr */
dest_addr.sin_family = host->h_addrtype; /* AF_INET from gethostbyname */
dest_addr.sin_port = htons(25); /* PORT defined above */
/* Get socket */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
/* Connect !*/
fputs("Connecting....\n", smtpfile);
if(connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) == -1){
perror("connect");
exit(1);
}
Sleep(waittime);
err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
fputs(Rec_Buf, smtpfile);
strcpy(line, "helo me.somepalace.com\n");
fputs(line, smtpfile);
bytes_sent = send(sockfd, line, strlen(line), 0);
Sleep(waittime);
err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
fputs(Rec_Buf, smtpfile);
strcpy(line, "MAIL FROM:<");
strncat(line, emailfrom, strlen(emailfrom));
strncat(line, ">\n", 3);
fputs(line, smtpfile);
bytes_sent = send(sockfd, line, strlen(line), 0);
Sleep(waittime);
err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
fputs(Rec_Buf, smtpfile);
strcpy(line, "RCPT TO:<");
strncat(line, emailto, strlen(emailto));
strncat(line, ">\n", 3);
fputs(line, smtpfile);
bytes_sent = send(sockfd, line, strlen(line), 0);
Sleep(waittime);
err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
fputs(Rec_Buf, smtpfile);
strcpy(line, "DATA\n");
fputs(line, smtpfile);
bytes_sent = send(sockfd, line, strlen(line), 0);
Sleep(waittime);
err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
fputs(Rec_Buf, smtpfile);
Sleep(waittime);
strcpy(line, "To:");
strcat(line, emailto);
strcat(line, "\n");
strcat(line, "From:");
strcat(line, emailfrom);
strcat(line, "\n");
strcat(line, "Subject:");
strcat(line, emailsubject);
strcat(line, "\n");
strcat(line, emailmessage);
strcat(line, "\r\n.\r\n");
fputs(line, smtpfile);
bytes_sent = send(sockfd, line, strlen(line), 0);
Sleep(waittime);
err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
fputs(Rec_Buf, smtpfile);
strcpy(line, "quit\n");
fputs(line, smtpfile);
bytes_sent = send(sockfd, line, strlen(line), 0);
Sleep(waittime);
err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
fputs(Rec_Buf, smtpfile);
fclose(smtpfile);
#ifdef WIN32
closesocket(sockfd);
WSACleanup();
#else
close(sockfd);
#endif
}
I guess the problem is that in the project setting->C/C++->General Treat Warnings as Errors was set to "Yes".
To solve it you can set it to "No", And add in project setting->C/C++->Preprocessor in Preprocessor definitions the _CRT_SECURE_NO_WARNINGS
EDIT
this link can also help:

multiple LNK2019 errors ( __wassert, RegisterClassEx, CreateWindowEx,..)

I try to compile and link my program using command-line link tool comming with Vsiaul Studio:
D:\Программы\textalign\textalign>"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link"/LIBPATH:"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\x64" /LIBPATH:"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib\amd64" /VERBOSE /NODEFAULTLIB /Entry:WinMain Kernel32.lib User32.lib Gdi32.lib libcpmt.lib main.obj
I use 64-bit libraries.
I get multiple linker errors: unresoved externals:
Запуск 1-го прохода (1-st pass)
Поиск библиотек (Libraries search)
Поиск C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\x64\Kernel32.lib:
Поиск C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\x64\User32.lib:
Поиск C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\x64\Gdi32.lib:
Поиск C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib\amd64\libcpmt.lib:
Поиск библиотек завершен (Libraries search is finished)
1-ый проход завершен (1-st pass is finished)
Неиспользуемые библиотеки: (Unused libraries:)
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\x64\Kernel32.lib
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\x64\User32.lib
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\x64\Gdi32.lib
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib\amd64\libcpmt.lib
main.obj : error LNK2019: ссылка на неразрешенный внешний символ __imp__GetLastError#0 функции _WinMain#16
main.obj : error LNK2019: ссылка на неразрешенный внешний символ __imp__FormatMessageW#28 в функции "void __cdecl OutputErrorMessage(unsigned long)" (?OutputErrorMessage##YAXK#Z)
...
main.obj : error LNK2019: ссылка на неразрешенный внешний символ __imp__MoveToEx#16 в функции "int __cdecl PrintText(struct HDC__ *,struct tagRECT)" (?PrintText##YAHPAUHDC__##UtagRECT###Z)
main.obj : error LNK2019: ссылка на неразрешенный внешний символ __imp__TextOutW#20 в функции "int __cdecl PrintText(struct HDC__ *,struct tagRECT)" (?PrintText##YAHPAUHDC__##UtagRECT###Z)
...
main.obj : error LNK2019: ссылка на неразрешенный внешний символ __imp__RegisterClassExW#4 в функции _WinMain#16
main.obj : error LNK2019: ссылка на неразрешенный внешний символ __imp__CreateWindowExW#48 в функции _WinMain#16
main.obj : error LNK2019: ссылка на неразрешенный внешний символ __imp__ShowWindow#8 в функции _WinMain#16
...
main.obj : error LNK2019: ссылка на неразрешенный внешний символ __wassert в функции _WinMain#16
main.obj : error LNK2019: ссылка на неразрешенный внешний символ #__security_check_cookie#4 в функции "long __stdcall WindowProcedure(struct HWND__ *,unsigned int,unsigned int,long)" (?WindowProcedure##YGJPAUHWND__##IIJ#Z)
main.obj : error LNK2019: ссылка на неразрешенный внешний символ ___security_cookie в функции "long __stdcall WindowProcedure(struct HWND__ *,unsigned int,unsigned int,long)" (?WindowProcedure##YGJPAUHWND__##IIJ#Z)
main.exe : fatal error LNK1120: неразрешенных внешних элементов: 28
(main.exe : fatal error LNK1120: unresolved external elements: 28)
I don't like those "unused libraries" section output, because it does look like it ignores my Kernel32.lib and other libraries.
What's even more cryptic:
main.obj : error LNK2019: ссылка на неразрешенный внешний символ __imp__FormatMessageW#28 в функции "void __cdecl OutputErrorMessage(unsigned long)" (?OutputErrorMessage##YAXK#Z)
That's not external function. Here's its code:
void OutputErrorMessage(DWORD err);
...
void OutputErrorMessage(DWORD err)
{
LPTSTR buffer = NULL;
DWORD bufSize = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&buffer,
0,
NULL);
assert(bufSize);
if (bufSize)
{
MessageBox(NULL, buffer, _T("Could not create hwnd"), MB_OK|MB_ICONERROR);
}
LocalFree(buffer);
}
If this can help, here is the command I used to compile main.cpp into main.obj file:
D:\Программы\textalign\textalign>"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\cl" /c /I"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include" /I"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Include" "main.cpp"
All the libraries on those two paths are 64-bit libs.
EDIT: Now I have built the application but it does not run:
I've fixed the lib path and it linked fine with 32-bit libs.
I also tried to link with 64-bit libs but I get error, "no smdb120.dll" library. And there's only a 32-bit version of it on my PC. (I couldn't find anything in the Internet.) It links if I give a 64-bit linker this 32-bit library, but isn't it wrong to do so?
The problem is: the application (both 32-bit and 64-bit) doesn't create GUI, it runs as a background process with 0% processor time, so it does nothing at all.
The same application built with mingw (codeblocks ide) runs ok.
What can be a problem? How can I fix it?
I tried to remove /NODEFAULTLIB and add /SUBSYSTEM:WINDOWS but that doesn't solve the problem.

Why am I getting an "unresolved external symbol" message?

I am attempting to use IBM's EHLLAPI to interface with their Personal Communicator Terminal Emulator. I have copied their sample code from this page, but it's giving me an error when I try to build it.
1>------ Build started: Project: PCOMAPI, Configuration: Debug Win32 ------
1> Source.cpp
1>Source.obj : error LNK2019: unresolved external symbol _hllapi#16 referenced in function _main
1>C:\Users\[username]\Documents\Visual Studio 2013\Projects\VPARSAPI\Debug\PCOMAPI.exe : fatal error LNK1120: 1 unresolved externals
I'm not entirely certain what this _hllapi#16 is, and I'm not seeing it in the code. It has been a while since I've worked with C++, so it may be something simple I'm missing. The code is as follows:
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include "hapi_c.h"
int main(char **argv, int argc) {
int HFunc, HLen, HRc;
char HBuff[1];
struct HLDConnectPS ConnBuff;
// Send Key string for HOME+string+ENTER:
char SendString[] = "#0Hello World!#E";
HFunc = HA_RESET_SYSTEM;
HLen = 0;
HRc = 0;
hllapi(&HFunc, HBuff, &HLen, &HRc);
if (HRc != HARC_SUCCESS) {
printf("Unable to access EHLLAPI.\n");
return 1;
}
HFunc = HA_CONNECT_PS;
HLen = sizeof(ConnBuff);
HRc = 0;
memset(&ConnBuff, 0x00, sizeof(ConnBuff));
ConnBuff.stps_shortname = 'A';
hllapi(&HFunc, (char *)&ConnBuff, &HLen, &HRc);
switch (HRc) {
case HARC_SUCCESS:
case HARC_BUSY:
case HARC_LOCKED: // All these are OK
break;
case HARC_INVALID_PS:
printf("Host session A does not exist.\n");
return 1;
case HARC_UNAVAILABLE:
printf("Host session A is in use by another EHLLAPI application.\n");
return 1;
case HARC_SYSTEM_ERROR:
printf("System error connecting to session A.\n");
return 1;
default:
printf("Error connecting to session A.\n");
return 1;
}
HFunc = HA_SENDKEY;
HLen = strlen(SendString);
HRc = 0;
hllapi(&HFunc, SendString, &HLen, &HRc);
switch (HRc) {
case HARC_SUCCESS:
break;
case HARC_BUSY:
case HARC_LOCKED:
printf("Send failed, host session locked or busy.\n");
break;
default:
printf("Send failed.\n");
break;
}
HFunc = HA_DISCONNECT_PS;
HLen = 0;
HRc = 0;
hllapi(&HFunc, HBuff, &HLen, &HRc);
printf("EHLLAPI program ended.\n");
return 0;
}
My linker flags are:
/OUT:"C:\Users[username]\Documents\Visual Studio
2013\Projects\VPARSAPI\Debug\PCOMAPI.exe" /MANIFEST /NXCOMPAT
/PDB:"C:\Users[username]\Documents\Visual Studio
2013\Projects\VPARSAPI\Debug\PCOMAPI.pdb" /DYNAMICBASE "kernel32.lib"
"user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib"
"shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib"
"odbccp32.lib" /DEBUG /MACHINE:X86 /INCREMENTAL
/PGD:"C:\Users[username]\Documents\Visual
Studio2013\Projects\VPARSAPI\Debug\PCOMAPI.pgd" /SUBSYSTEM:CONSOLE
/MANIFESTUAC:"level='asInvoker' uiAccess='false'"
/ManifestFile:"Debug\PCOMAPI.exe.intermediate.manifest"
/ERRORREPORT:PROMPT /NOLOGO /TLBID:1
That is a linker error. You need to pass to the linker the .lib file, the import library, for the EHLLAPI library.
In fact, looking at the documentation, there are a host of .lib files with this library. You'll need to study the documentation carefully to work out which ones you need.
As stated under the section Compiling and Linking you have to include pcscal32.lib for static linking, so the symbols in *hapi_c.h* can be resolved.

Unresolved sockets references under Windows 8

I've installed Windows 8 64bits and Visual Sudio 2012.
Later, in class, the teacher gave us a .cpp file to try a udp server on our computer.
It should be just run and use but, in my case, VS is giving me some unresolved externals
#include <stdio.h>
#include <winsock.h>
#define GET_TIME "TIME\r\n"
#define MAX_MSG_SIZE 100
void processClient(LPVOID param);
void main(int argc, char **argv)
{
SOCKET s, cliSocket;
WSADATA wsaData;
int iResult, len;
struct sockaddr_in serv_addr, cli_addr;
SECURITY_ATTRIBUTES sa;
DWORD thread_id;
if(argc != 2){
printf("Usage: %s <time_server_port>\n", argv[0]);
getchar();
exit(1);
}
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
getchar();
exit(1);
}
if((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR){
printf("Unable to create socket (error: %d)!\n", WSAGetLastError());
getchar();
exit(1);
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));
if(bind(s, (struct sockaddr *)&serv_addr, sizeof(serv_addr))==SOCKET_ERROR){
printf("Unable to bind socket to port %s (error: %d)\n", argv[1], WSAGetLastError());
closesocket(s);
getchar();
exit(1);
}
listen(s, 5);
while(1){
printf("Waiting for a time request...\n");
len = sizeof(cli_addr);
if((cliSocket = accept(s, (struct sockaddr *)&cli_addr, &len))==SOCKET_ERROR){
printf("Unable to accept new connection (error: %d)\n", WSAGetLastError());
closesocket(s);
getchar();
exit(1);
}
printf("Connection from %s:%d accepted\n", inet_ntoa(cli_addr.sin_addr),
ntohs(cli_addr.sin_port));
sa.nLength=sizeof(sa);
sa.lpSecurityDescriptor=NULL;
if(CreateThread(&sa,0 ,(LPTHREAD_START_ROUTINE)processClient, (LPVOID)cliSocket, (DWORD)0, &thread_id)==NULL){
printf("Cannot start new slave thread (error: %d)\n", GetLastError());
closesocket(cliSocket);
}
printf("New slave thread started (id: %d)\n", thread_id);
}
}
void processClient(LPVOID param)
{
SOCKET s;
int nbytes, len;
struct sockaddr_in cli_addr;
char request[MAX_MSG_SIZE], response[MAX_MSG_SIZE];
SYSTEMTIME systemTime;
s = (SOCKET)param;
if((nbytes = recv(s, request, MAX_MSG_SIZE, 0)) == SOCKET_ERROR){
printf("Unable to receive request (error: %d)\n", WSAGetLastError());
closesocket(s);
return;
}
request[nbytes] = 0;
if(strcmp(GET_TIME, request)!=0){
printf("Unexpected request \"%s\" will be ignored\n", request);
closesocket(s);
return;
}
GetSystemTime(&systemTime);
sprintf(response, "%d:%d:%d", systemTime.wHour,
systemTime.wMinute, systemTime.wSecond);
if(send(s, response, strlen(response), 0) == SOCKET_ERROR){
printf("Unable to send reponse (error: %d)\n", WSAGetLastError());
closesocket(s);
return;
}
len = sizeof(cli_addr);
getpeername(s, (struct sockaddr *)&cli_addr, &len);
printf("\"%s\" sent to %s:%d\n", response, inet_ntoa(cli_addr.sin_addr),
ntohs(cli_addr.sin_port));
closesocket(s);
}
These are the errors:
1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _accept#12 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _bind#12 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _closesocket#4 referenced in function "void __cdecl processClient(void *)" (?processClient##YAXPAX#Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _getpeername#12 referenced in function "void __cdecl processClient(void *)" (?processClient##YAXPAX#Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _htonl#4 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _htons#4 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _inet_ntoa#4 referenced in function "void __cdecl processClient(void *)" (?processClient##YAXPAX#Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _listen#8 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _ntohs#4 referenced in function "void __cdecl processClient(void *)" (?processClient##YAXPAX#Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _recv#16 referenced in function "void __cdecl processClient(void *)" (?processClient##YAXPAX#Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _send#16 referenced in function "void __cdecl processClient(void *)" (?processClient##YAXPAX#Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _socket#12 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _WSAStartup#8 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _WSAGetLastError#0 referenced in function "void __cdecl processClient(void *)" (?processClient##YAXPAX#Z)
1>c:\users\brunoc\documents\visual studio 2012\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe : fatal error LNK1120: 14 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Does someone knows how to solve this?
Thanks
Open the Project properties, go to Linker -> Input, and the add the ws2_32.lib to Additional Dependencies edit line.