Failing SamConnect, SamOpenDomain, SamrQueryInformationDomain failing to link - winapi

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.

Related

unresolved external symbol WdmlibIoCreateDeviceSecure referenced in function DriverEntry

I write a windows 10 driver. And following is the code, actually the code is a sample of a book.
is there anyone know what shold i do to deal with this issue.
#include <ntifs.h>
#include <wdmsec.h>
PDEVICE_OBJECT g_cdo = NULL;
const GUID CWK_GUID_CLASS_MYCDO =
{ 0x17a0d1e0L, 0x3249, 0x12e1, {0x92,0x16, 0x45, 0x1a, 0x21, 0x30, 0x29, 0x06} };
#define CWK_CDO_SYB_NAME L"\\??\\slbkcdo_3948d33e"
void DriverUnload(PDRIVER_OBJECT driver)
{
// ...
}
NTSTATUS DriverEntry(PDRIVER_OBJECT driver, PUNICODE_STRING reg_path)
{
NTSTATUS status;
ULONG i;
UCHAR mem[256] = { 0 };
UNICODE_STRING sddl = RTL_CONSTANT_STRING(L"D:P(A;;GA;;;WD)");
UNICODE_STRING cdo_name = RTL_CONSTANT_STRING(L"\\Device\\cwk_3948d33e");
UNICODE_STRING cdo_syb = RTL_CONSTANT_STRING(CWK_CDO_SYB_NAME);
KdBreakPoint();
status = IoCreateDeviceSecure(
driver,
0, &cdo_name,
FILE_DEVICE_UNKNOWN,
FILE_DEVICE_SECURE_OPEN,
FALSE, &sddl,
(LPCGUID)&CWK_GUID_CLASS_MYCDO,
&g_cdo);
if (!NT_SUCCESS(status))
return status;
// ...
return STATUS_SUCCESS;
}
the error message is:
LNK2019 unresolved external symbol WdmlibIoCreateDeviceSecure referenced in function DriverEntry MyDriver7 C:\Users\xxx\source\repos\MyDriver7\MyDriver7\Source.obj
You need to compile your project with Wdmsec.lib.
In Visual Studio, right click the project and go to Configuration Properties > Linker > Input > Additional Dependencies and add Wdmsec.lib.

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.

error calling vc++ method error LNK2019: unresolved external symbol referenced in function _wmain

shows error calling vc++ 2010 express sp1 method of a object:
error LNK2019: unresolved external symbol "public: int __thiscall
oper::ShowMyResult(void)" (?ShowMyResult#oper##QAEHXZ) referenced in
function _wmain
ooad_oops_exer.h
#include "stdafx.h"
class oper {
public:
void DisplayDefault( );
void DisplayWidth( int n );
void DisplayLongs( );
int ShowMyResult();
};
ooad_oops_exer.cpp
#include "ooad_oops_exer.h"
//
#ifndef SOCKET_CLIENT_CLASS
#define SOCKET_CLIENT_CLASS
const double d1 = 1.23456789;
const long l1 = 16;
int base = 10;
void oper::DisplayDefault( )
{
cout << endl << "default display" << endl;
}
void oper::DisplayWidth( int n )
{
cout << endl << "fixed width display set to " << n << ".\n";
}
void oper::DisplayLongs( )
{
cout <<endl<<"hello";
}
int oper::ShowMyResult()
{
DisplayWidth(15);
DisplayDefault( );
cout <<endl<<"ShowMyResult";
return 0;
}
//
#endif
main.cpp
#include "stdafx.h"
#include "ooad_oops_exer.h"
int _tmain(int argc, _TCHAR* argv[])
{
oper op;
int k = op.ShowMyResult();
cin.get();
return 0;
}
EDIT
1> Generating Code... 1>Debug\ooad_oops_exer.obj : warning LNK4042:
object specified more than once; extras ignored 1>main.obj : error
LNK2019: unresolved external symbol "public: int __thiscall
oper::ShowMyResult(void)" (?ShowMyResult#oper##QAEHXZ) referenced in
function _wmain 1>C:\xxx\OOAD_OOPS_PROJ\Debug\OOAD_OOPS_PROJ.exe :
fatal error LNK1120: 1 unresolved externals 1>

Compiling a working 32bit .dll for 64bit, errors

I have with a lot of help put together a 32bit plugin (.dll) for the 32bit Teamspeak program which works great.
People are now asking me to compile this for the 64bit Teamspeak client.
But...........
When I do I get the following errors.
1>------ Build started: Project: GetFSComFreqs, Configuration: Release x64 ------
Creating library C:\Users\Roo\Desktop\pluginsdk\src\x64\Release\GetFSComFreqs.lib and object C:\Users\Roo\Desktop\pluginsdk\src\x64\Release\GetFSComFreqs.exp
GetFSComFreqs.obj : error LNK2019: unresolved external symbol SimConnect_Close referenced in function "void __cdecl DLLStop(void)" (?DLLStop##YAXXZ)
GetFSComFreqs.obj : error LNK2019: unresolved external symbol SimConnect_Open referenced in function "void __cdecl DLLStart(void)" (?DLLStart##YAXXZ)
SimConnectProcs.obj : error LNK2001: unresolved external symbol SimConnect_Open
GetFSComFreqs.obj : error LNK2019: unresolved external symbol SimConnect_CallDispatch referenced in function "void __cdecl DLLStart(void)" (?DLLStart##YAXXZ)
SimConnectProcs.obj : error LNK2019: unresolved external symbol SimConnect_AddToDataDefinition referenced in function "void __cdecl OnRecvOpen(struct SIMCONNECT_RECV_OPEN *,unsigned long,void *)" (?OnRecvOpen##YAXPEAUSIMCONNECT_RECV_OPEN##KPEAX#Z)
SimConnectProcs.obj : error LNK2019: unresolved external symbol SimConnect_RequestDataOnSimObject referenced in function "void __cdecl OnRecvOpen(struct SIMCONNECT_RECV_OPEN *,unsigned long,void *)" (?OnRecvOpen##YAXPEAUSIMCONNECT_RECV_OPEN##KPEAX#Z)
SimConnectProcs.obj : error LNK2019: unresolved external symbol SimConnect_GetNextDispatch referenced in function "unsigned int __cdecl MessageRoutine(void *)" (?MessageRoutine##YAIPEAX#Z)
C:\Users\Roo\Desktop\pluginsdk\src\x64\Release\GetFSComFreqs.dll : fatal error LNK1120: 6 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
A part of the Code...
#include "stdafx.h"
#define FSAPI __stdcall
HANDLE hSimConnect = 0;
HANDLE hMsgThread = 0;
HINSTANCE ThisModule = 0;
HRESULT hr = 0;
char ThisModuleName[MAX_PATH];
void FSAPI StartSimConnect()
{
if (NULL == hSimConnect)
{
GetModuleFileName(ThisModule, ThisModuleName, MAX_PATH);
hr = SimConnect_Open(&hSimConnect, ThisModuleName, NULL, NULL, NULL, 0);
hr = SimConnect_CallDispatch(hSimConnect, SimConnectDispatch, NULL);
if (FAILED(hr)) hSimConnect = 0;
else hMsgThread = (HANDLE)_beginthreadex(NULL, NULL, MessageRoutine, NULL, NULL, NULL);
}
}
void FSAPI StopSimConnect()
{
if (NULL != hSimConnect)
{
hr = SimConnect_Close(hSimConnect);
hSimConnect = 0;
}
}
int main() {}
void FSAPI DLLStart(void)
{
if (NULL == hSimConnect)
{
GetModuleFileName(ThisModule, ThisModuleName, MAX_PATH);
hr = SimConnect_Open(&hSimConnect, ThisModuleName, NULL, NULL, NULL, 0);
hr = SimConnect_CallDispatch(hSimConnect, SimConnectDispatch, NULL);
if (FAILED(hr)) hSimConnect = 0;
else hMsgThread = (HANDLE)_beginthreadex(NULL, NULL, MessageRoutine, NULL, NULL, NULL);
}
}
void FSAPI DLLStop(void)
{
if (NULL != hSimConnect)
{
hr = SimConnect_Close(hSimConnect);
hSimConnect = 0;
}
}
You are getting unresolved external symbols errors.
If all those SimConnect_* functions are coming from an external source, you need to link you program with the appropriate import library.
If you just changed the build configuration from win32 to x64, it might be the case that these import libraries were not changed into your x64 configuration.

stack select() function doesn't work in Visual Studio

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")

Resources