stack select() function doesn't work in Visual Studio - visual-studio-2010

The following is part of the example code from http://curl.haxx.se/libcurl/c/sendrecv.html:
static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
{
struct timeval tv;
fd_set infd, outfd, errfd;
int res;
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec= (timeout_ms % 1000) * 1000;
FD_ZERO(&infd);
FD_ZERO(&outfd);
FD_ZERO(&errfd);
FD_SET(sockfd, &errfd); /* always check for error */
if(for_recv)
{
FD_SET(sockfd, &infd);
}
else
{
FD_SET(sockfd, &outfd);
}
///* select() returns the number of signalled sockets or -1 */
res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
return res;
}
It worked fine on Linux, but when I brought it over Visual Studio 2010, it complains about the select() function. I compiled and linked all the cURL libraries/dlls correctly, and I even downloaded stuff as http request.
But when I tried to use the socket select() function, it complains with the following error:
error LNK2028: unresolved token (0A00034C) "extern "C" int __stdcall select(int,struct fd_set *,struct fd_set *,struct fd_set *,struct timeval const *)" (?select##$$J220YGHHPAUfd_set##00PBUtimeval###Z) referenced in function "int __cdecl `anonymous namespace'::wait_on_socket(unsigned int,int,long)" (?wait_on_socket#?A0x7d9db21b##$$FYAHIHJ#Z)
error LNK2019: unresolved external symbol "extern "C" int __stdcall select(int,struct fd_set *,struct fd_set *,struct fd_set *,struct timeval const *)" (?select##$$J220YGHHPAUfd_set##00PBUtimeval###Z) referenced in function "int __cdecl `anonymous namespace'::wait_on_socket(unsigned int,int,long)" (?wait_on_socket#?A0x7d9db21b##$$FYAHIHJ#Z)
Also, it seems that the select() function is compatible with Windows:
http://msdn.microsoft.com/en-us/library/ms740141(VS.85).aspx
Am I missing something? Can someone tell me how to get around this problem?
Thanks, --Rudy

You must link with the Microsoft socket library. Tell your linker to use Ws2_32.lib within your project.
In Visual Studio 2010 add this lib to Project->Properties->Linker->Input->Add. Dependencies.
Or add this lib inside your code with:
#pragma comment(lib, "Ws2_32.lib")

Related

Failing SamConnect, SamOpenDomain, SamrQueryInformationDomain failing to link

I have windows c++ application for fetching password complexity
somehow it is failing with linker error
Not able find documented header by msdn, even I copy the function declaration it is failing with linker error, may be possibly function signature mismatch of libs function. I do not find function signature from lib in Windows kits.
1>obj : error LNK2001: unresolved external symbol "long __cdecl SamQueryInformationDomain(void *,enum _DOMAIN_INFORMATION_CLASS,void * *)" (?SamQueryInformationDomain##YAJPEAXW4_DOMAIN_INFORMATION_CLASS##PEAPEAX#Z)
error LNK2001: unresolved external symbol "long __cdecl SamOpenDomain(void *,unsigned long,void *,void * *)" (?SamOpenDomain##YAJPEAXK0PEAPEAX#Z)
error LNK2001: unresolved external symbol "long __cdecl SamConnect(struct _LSA_UNICODE_STRING *,void * *,unsigned long,unsigned char)" (?SamConnect##YAJPEAU_LSA_UNICODE_STRING##PEAPEAXKE#Z)
error LNK2001: unresolved external symbol "long __cdecl SamCloseHandle(void *)" (?SamCloseHandle##YAJPEAX#Z)
but these functions are present in lib file, checked with dumpbin
#pragma comment(lib, "C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.19041.0\\um\\x64\\samlib.lib")
NTSTATUS SamQueryInformationDomain(SAMPR_HANDLE domain, DOMAIN_INFORMATION_CLASS val, PSAMPR_DOMAIN_INFO_BUFFER* Buffer);
extern NTSTATUS WINAPI SamConnect(IN PUNICODE_STRING ServerName, OUT SAMPR_HANDLE* ServerHandle, IN ACCESS_MASK DesiredAccess, IN BOOLEAN Trusted);
extern NTSTATUS WINAPI SamOpenDomain(IN SAMPR_HANDLE SamHandle, IN ACCESS_MASK DesiredAccess, IN PSID DomainId, OUT SAMPR_HANDLE* DomainHandle);
extern NTSTATUS WINAPI SamCloseHandle(IN SAMPR_HANDLE SamHandle);
void ReadPasswordComplexInfo()
{
NTSTATUS status, enumDomainStatus, enumUserStatus;
UNICODE_STRING serverName;
ACCESS_MASK mask = 0;
mask = SAM_SERVER_CONNECT | SAM_SERVER_ENUMERATE_DOMAINS | SAM_SERVER_LOOKUP_DOMAIN;
SAMPR_HANDLE hServerHandle, hBuiltinHandle = NULL, hDomainHandle, hUserHandle;
DWORD domainEnumerationContext = 0, domainCountRetourned, userEnumerationContext, userCountRetourned, groupsCountRetourned, i, j, k, aliasCountRetourned, *alias;
PSAMPR_RID_ENUMERATION pEnumDomainBuffer, pEnumUsersBuffer;
PSID domainSid, userSid;
PGROUP_MEMBERSHIP pGroupMemberShip;
//PSAMPR_DOMAIN_INFO_BUFFER *buff = NULL;
PSAMPR_DOMAIN_INFO_BUFFER buff;
SID builtin = {SID_REVISION, 1, SECURITY_NT_AUTHORITY, {SECURITY_BUILTIN_DOMAIN_RID}};
RtlInitUnicodeString(&serverName, L"");
//status = SamConnect(&serverName, &hServerHandle, SAM_SERVER_CONNECT | SAM_SERVER_ENUMERATE_DOMAINS | SAM_SERVER_LOOKUP_DOMAIN, FALSE);
status = SamConnect(&serverName, &hServerHandle, SAM_SERVER_CONNECT | SAM_SERVER_ENUMERATE_DOMAINS | SAM_SERVER_LOOKUP_DOMAIN, FALSE);
if (0 != status)
{
printf("SamConnect error (?) %08x\n", status);
return;
}
//status = SamOpenDomain(hServerHandle, DOMAIN_LIST_ACCOUNTS | DOMAIN_LOOKUP, &builtin, &hBuiltinHandle);
status = SamOpenDomain(hServerHandle, DOMAIN_LIST_ACCOUNTS | DOMAIN_LOOKUP, domainSid, &hDomainHandle);
if (0 != status)
{
printf("SamOpenDomain Builtin (?) %08x\n", status);
return;
}
//status = SamQueryInformationDomain(hBuiltinHandle, DomainPasswordInformation, (PVOID*)&buff);
status = SamrQueryInformationDomain(hBuiltinHandle, DomainPasswordInformation, (PVOID*)&buff);
if (0 != status)
{
printf("SamQueryInformation failed with %08x\n", status);
}ULONG properties = buff->Password.PasswordProperties; printf("SamQueryInformation success with password properties value : %ld\n", properties);
if(hBuiltinHandle)
{
SamCloseHandle(hBuiltinHandle);
SamCloseHandle(hServerHandle);
}
}
Also tried with SamrOpenDomain API same issues.

Error LNK2019 unresolved external symbol _cmd_ln_init referenced in function _main

I am trying to run my code using the library pocketsphinx. Even though I have used the .lib files for the library it still isn't working. My code:
#include "stdafx.h"
#include <pocketsphinx.h>
#define MODELDIR "c:/sphinx/model"
int main(int argc, char *argv[])
{
ps_decoder_t *ps = NULL;
cmd_ln_t *config = NULL;
config = cmd_ln_init(NULL, ps_args(), TRUE,
"-hmm", MODELDIR "/en-us/en-us",
"-lm", MODELDIR "/en-us/en-us.lm.bin",
"-dict", MODELDIR "/en-us/cmudict-en-us.dict",
NULL);
return 0;
}
Errors:
Error LNK2019 unresolved external symbol _cmd_ln_init referenced in function _main
Error LNK2019 unresolved external symbol _ps_args referenced in function _main

calling native(Nt) API in user mode

I am trying to call native API(NtOpenKey) in user mode. I am seeing linker problem. I am really confused, what is missing here. How can I achieve doing this? I am attaching my code here. ntdll.lib is added to the project(link)
Error 58 error LNK2001: unresolved external symbol "__declspec(dllimport) long __cdecl NtOpenKey(void * *,unsigned long,struct _OBJECT_ATTRIBUTES *)" (__imp_?NtOpenKey##YAJPEAPEAXKPEAU_OBJECT_ATTRIBUTES###Z) C:\Users\santhi.ragipati\documents\visual studio 2013\Projects\NtRegistry\NtRegistry\NtRegistry.obj NtRegistry
Thanks
Santhi
`// NtRegistry.cpp : Defines the entry point for the console application.
//
#include <tchar.h>
#include <Windows.h>
#include <Winternl.h>
#include <ntstatus.h>
NTSYSAPI NTSTATUS NTAPI NtOpenKey(
_Out_ PHANDLE KeyHandle,
_In_ ACCESS_MASK DesiredAccess,
_In_ POBJECT_ATTRIBUTES ObjectAttributes
);
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE handleRegKey = NULL;
for (int n = 0; n < 1; n++)
{
NTSTATUS status = NULL;
UNICODE_STRING RegistryKeyName;
OBJECT_ATTRIBUTES ObjectAttributes;
RtlInitUnicodeString(&RegistryKeyName, L"\\Registry\\Machine\\Software\\MyCompany\\MyApp");
InitializeObjectAttributes(&ObjectAttributes,
&RegistryKeyName,
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
NULL, // handle
NULL);
status = NtOpenKey(&handleRegKey, (ACCESS_MASK)KEY_READ, &ObjectAttributes);
if (NT_SUCCESS(status) == FALSE)
{
break;
}
} // Get the Frame location from the registry key.
// All done with the registry.
if (NULL != handleRegKey)
{
NtClose(handleRegKey);
}
return 0;
}
`
This was the giveaway:
NtOpenKey##YAJPEAPEAXKPEAU_OBJECT_ATTRIBUTES###Z
That's typical of C++ name mangling; since functions can be overloaded, but the function name used when exporting and importing must be unique, the name is modified to include a description of the argument list.
Adding extern "c" to the declaration will resolve the problem.
Incidentally, you probably don't want to set the OBJ_KERNEL_HANDLE flag, since it asks for a handle that you won't be able to use. I'm guessing Windows will ignore it, and give you a user-mode handle anyway, but better safe than sorry.

Relinking to a modified DLL in another solution in Visual Studio 2010

I coded a BitPacker object that takes in various types of data and packs them to a buffer using the fewest number of bits required to store their values. I created a separate solution to make this into a DLL for reuse-ability. I was successfully able to link to it from another solution and use it. However I ran into the problem of these two function prototypes in the DLL being ambiguous when trying to pass in a long long for "data" instead of an int:
static __declspec(dllexport) unsigned long long UnpackBits(char *srcBuffer, long long data, unsigned int &offsetBits, const unsigned int numBits);
static __declspec(dllexport) unsigned int UnpackBits(char *srcBuffer, int data, unsigned int &offsetBits, const unsigned int numBits);
static __declspec(dllexport) float UnpackBits(char *srcBuffer, float data, unsigned int &offsetBits);
There is no problem when I'm simply passing an int. Since I can't think of a way to remove the ambiguity to support both data types I decided to modify the DLL source by commenting out the "long long" versions from the header and source files and recompiling them (both for Debug and Release). However after doing that I'm receiving linker errors for pretty much every call to that DLL now. I don't know why since I also changed the instances of where I was using a long long data member to int to make sure there was no reference to the old long long version that I commented out.
1>ChessGame.obj : error LNK2019: unresolved external symbol "public: static unsigned int __cdecl BitPacker::UnpackBits(char *,int,unsigned int &,unsigned int)" (?UnpackBits#BitPacker##SAIPADHAAII#Z) referenced in function "public: class ChessGame * __thiscall ChessGame::LoadGame(char const *)" (?LoadGame#ChessGame##QAEPAV1#PBD#Z)
1>Date.obj : error LNK2001: unresolved external symbol "public: static unsigned int __cdecl BitPacker::UnpackBits(char *,int,unsigned int &,unsigned int)" (?UnpackBits#BitPacker##SAIPADHAAII#Z)
1>Turn.obj : error LNK2001: unresolved external symbol "public: static unsigned int __cdecl BitPacker::UnpackBits(char *,int,unsigned int &,unsigned int)" (?UnpackBits#BitPacker##SAIPADHAAII#Z)
1>ChessGame.obj : error LNK2019: unresolved external symbol "public: static int __cdecl BitPacker::ReadFile(char const *,char *,int)" (?ReadFile#BitPacker##SAHPBDPADH#Z) referenced in function "public: class ChessGame * __thiscall ChessGame::LoadGame(char const *)" (?LoadGame#ChessGame##QAEPAV1#PBD#Z)
1>ChessGame.obj : error LNK2019: unresolved external symbol "public: static int __cdecl BitPacker::FileSize(char const *)" (?FileSize#BitPacker##SAHPBD#Z) referenced in function "public: class ChessGame * __thiscall ChessGame::LoadGame(char const *)" (?LoadGame#ChessGame##QAEPAV1#PBD#Z)
1>ChessGame.obj : error LNK2019: unresolved external symbol "public: static void __cdecl BitPacker::PackBits(char *,int,unsigned int &,unsigned int)" (?PackBits#BitPacker##SAXPADHAAII#Z) referenced in function "public: int __thiscall ChessGame::SaveGame(char const *)" (?SaveGame#ChessGame##QAEHPBD#Z)
1>Date.obj : error LNK2001: unresolved external symbol "public: static void __cdecl BitPacker::PackBits(char *,int,unsigned int &,unsigned int)" (?PackBits#BitPacker##SAXPADHAAII#Z)
1>Turn.obj : error LNK2001: unresolved external symbol "public: static void __cdecl BitPacker::PackBits(char *,int,unsigned int &,unsigned int)" (?PackBits#BitPacker##SAXPADHAAII#Z)
1>ChessGame.obj : error LNK2019: unresolved external symbol "public: static int __cdecl BitPacker::CopyBits(char *,char *,unsigned int &,unsigned int &,unsigned int)" (?CopyBits#BitPacker##SAHPAD0AAI1I#Z) referenced in function "public: int __thiscall ChessGame::SaveGame(char const *)" (?SaveGame#ChessGame##QAEHPBD#Z)
1>Turn.obj : error LNK2001: unresolved external symbol "public: static int __cdecl BitPacker::CopyBits(char *,char *,unsigned int &,unsigned int &,unsigned int)" (?CopyBits#BitPacker##SAHPAD0AAI1I#Z)
1>ChessGameManager.obj : error LNK2019: unresolved external symbol "public: static bool __cdecl BitPacker::FileExists(char const *)" (?FileExists#BitPacker##SA_NPBD#Z) referenced in function "public: void __thiscall ChessGameManager::RenderGameMenuIcons(void)" (?RenderGameMenuIcons#ChessGameManager##QAEXXZ)
1>C:\Users\rvandyke\Documents\Visual Studio 2010\Projects\SuperChessW32_v003\Debug\SuperChessW32_v003.exe : fatal error LNK1120: 6 unresolved externals
I tried cleaning the calling project, manually deleting all the object files and doing a Rebuild All but the errors remain. I'm thinking it is somehow still using an old version of the DLL header file and/or lib but don't know how or why. Is there something I need to do in my calling Solution for it to "re recognize" the new DLL?
PS - And while I'm asking anyone have any advice on the ambiguity problem? I'd like to be able to support both 32 and 64 bit data types but not sure how. I thought about simply getting rid of the int version and only having the long long version but then I worry about the possibility of data becoming truncated when catching the return type back into an int.

OpenCV Build on Visual Studio LINK error

I have followed this tutorial here as mentioned exactly
I now try to run simple OpenCV code on Visual Studio but it I keep getting linker errors.
I am trying this OpenCV tutorial in particular
Here is the error I keep getting :
1>Linking...
1>LINK : warning LNK4067: ambiguous entry point; selected 'mainCRTStartup'
1>OpenCV_Proj.obj : error LNK2019: unresolved external symbol "int __cdecl cv::waitKey(int)" (?waitKey#cv##YAHH#Z) referenced in function _main
1>OpenCV_Proj.obj : error LNK2019: unresolved external symbol "void __cdecl cv::imshow(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class cv::_InputArray const &)" (?imshow#cv##YAXABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##ABV_InputArray#1##Z) referenced in function _main
1>OpenCV_Proj.obj : error LNK2019: unresolved external symbol "public: __thiscall cv::_InputArray::_InputArray(class cv::Mat const &)" (??0_InputArray#cv##QAE#ABVMat#1##Z) referenced in function _main
1>OpenCV_Proj.obj : error LNK2019: unresolved external symbol "void __cdecl cv::namedWindow(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int)" (?namedWindow#cv##YAXABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##H#Z) referenced in function _main
1>OpenCV_Proj.obj : error LNK2019: unresolved external symbol "class cv::Mat __cdecl cv::imread(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int)" (?imread#cv##YA?AVMat#1#ABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##H#Z) referenced in function _main
1>OpenCV_Proj.obj : error LNK2019: unresolved external symbol "void __cdecl cv::fastFree(void *)" (?fastFree#cv##YAXPAX#Z) referenced in function "public: __thiscall cv::Mat::~Mat(void)" (??1Mat#cv##QAE#XZ)
1>OpenCV_Proj.obj : error LNK2019: unresolved external symbol "public: void __thiscall cv::Mat::copySize(class cv::Mat const &)" (?copySize#Mat#cv##QAEXABV12##Z) referenced in function "public: class cv::Mat & __thiscall cv::Mat::operator=(class cv::Mat const &)" (??4Mat#cv##QAEAAV01#ABV01##Z)
1>OpenCV_Proj.obj : error LNK2019: unresolved external symbol "public: void __thiscall cv::Mat::deallocate(void)" (?deallocate#Mat#cv##QAEXXZ) referenced in function "public: void __thiscall cv::Mat::release(void)" (?release#Mat#cv##QAEXXZ)
1>C:\Users\Saher\Documents\Visual Studio 2008\Projects\OpenCV_Proj\Debug\OpenCV_Proj.exe : fatal error LNK1120: 8 unresolved externals``
For the following code :
// OpenCV_Proj.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#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;
}
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
I have been trying to get OpenCV to work for VS2008 for a while and any help regarding this issue will be apprectiated.
NOTE: In the readme file of the tutorial the following is what I followed:
1) Add build\bin and one of build\{x86|x64}\{vc9\vc10\mingw}\bin to your system path (to use DLLs)
Add build\{x86|x64}\{vc9\vc10\mingw}\lib or
build\{x86|x64}\{vc9\vc10\mingw}\staticlib as library directories to your linker settings,
Add build\include and build\include\opencv as include directories to your compiler settings.
Any help with getting this to work is really appreciated.
Those symbols are defined inside the OpenCV libraries, so you need to configure the project and let the linker know which OpenCV libraries you are using.
At the very least you should add: opencv_core230.lib and opencv_highgui230.lib (for OpenCV 2.3.0)
For more info on how to do this on VS2010, check this tutorial.
Go to properties->Linker->input and
add cv210.lib; cxcore210.lib; highgui210.lib;cvaux210.lib;
Your problem will be solved.
Have a happy coding....
I faced the same issue. After using dumpbin to see the exported symbols of highgui, I found that the linker is trying to link against a symbol that has debug_build_guard at the end.
If you built OPENCV in release mode, You cannot compile your application in DEBUG mode. Try flipping it to RELEASE and it magically works :)

Resources