I'm new to c++ and vc++ and i used 3rd party library bioapi. I successfully integrated it but when I tried to use the sample working program of bioapi, the misc.h and misc.cpp, i got an error LNK2005 AND LNK1160.
By the way I created this as CLR project in vc++ and it seems I cannot use /FORCE or /FORCE:MULTIPLE.
Here's the misc.h
/*---------------------------------------------------------------*/
/* */
/* Copyright (C) NEC Corporation 2011 */
/* NEC CONFIDENTIAL AND PROPRIETARY */
/* */
/* All rights reserved by NEC Corporation. This program must be */
/* used solely for the purpose for which it was furnished by NEC */
/* Corporation. No part of this program may be reproduced or */
/* disclosed to others, in any form, without the prior written */
/* permission of NEC Corporation. Use of copyright notice does */
/* not evidence publication of this program. */
/* */
/* NEC Corporation accepts no responsibility for any damages */
/* resulting from the use of this sample code. */
/* This sample code is provided "AS IS", and its user assume all */
/* risks when using it. */
/* */
/*---------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <sys/types.h> /* for stat() */
#include <sys/stat.h> /* for stat() */
#include <direct.h> /* for _mkdir() */
//#include "stdafx.h"
//#include "resource.h"
#include "BioAPI.h" /* include BioAPI Framework */
#include "BioAPI_util.h" /* include BioAPI Framework */
#include "hf-pid-bsp.h"
/*--------------------------------------------------------------------
*
* define
*
*--------------------------------------------------------------------*/
#define MaxN 1000 /* Max Identify Num */
#define MaxEnrollNum 999 /* Max Enroll Num */
#define MaxID 9999 /* Max Enroll ID */
#define MinID 0 /* Min Enroll ID */
#define Timeout 10000 /* Capture Timeout */
#define ERRCODE_ID_INPUT_ERROR -100
#define ERRCODE_NOT_ENROLLED -101
#define SECTION_IDENTIFYPARAM "IdentifyParam"
#define KEY_MAXNUMBEROFRESULTS "MaxNumberOfResults"
#define MESS_ALLEADY_STARTED "Application is already started."
#define MESS_USERREG "UserReg"
#define MESS_AP_ERROR "Error"
#define MESS_ID_INPUT_ERROR TEXT("Please input a value in the range of 0001-9999")
#define MESS_NOT_ENROLLED TEXT("The ID does not exist for")
#define MESS_BIOAPI_ERROR TEXT("BioAPI Error")
#define MESS_BIOAPI_ERROR_CODE TEXT("BioAPI Error Code: %ld")
#define MESS_ENROLL "Enrollment"
#define MESS_ENROLL_FAILED "Enroll process is failed."
#define MESS_ENROLL_SUCCESSED "Enroll process is successed."
#define MESS_VERIFY "Verification"
#define MESS_VERIFY_FAILED "Verification failed."
#define MESS_VERIFY_OK "Verification process is successed."
#define MESS_VERIFY_NG "Verification process is failed."
#define MESS_IDENTIFY "Identification"
#define MESS_IDENTIFY_FAILED "Identification failed."
#define MESS_IDENTIFY_OK "Identification process is successed."
#define MESS_IDENTIFY_NG "Identification process is failed."
#define MESS_CAPTION "Sample Application"
#define MESS_END "Exit"
#define MESS_MESSAGE1 "Please choose the function."
#define MESS_CLOSE "Close"
#define MESS_ENROLL1 "Enroll"
#define MESS_ENROLL2 "CreateTemplate"
#define MESS_MESSAGE2 "After entering ID(0001`9999), Please click [Enroll] button or [CreateTemplate] button."
#define MESS_ID "ID:"
#define MESS_ONETOONE "1:1 Verification"
#define MESS_ONETON "1:N Identification"
#define MESS_MESSAGE3 "Please choose the verification method."
#define MESS_VERIFY1 "VerifyMatch"
#define MESS_VERIFY2 "Verify"
#define MESS_MESSAGE4 "After entering ID(0001`9999), Please click [Verify] button or [Verification] button."
#define MESS_IDENTIFY1 "IdentifyMatch"
#define MESS_IDENTIFY2 "Identify"
#define MESS_MESSAGE5 "Please click [Identify] button or [Identification] button."
#define MESS_AUTH_MESSAGE "Identification process is successed.\r\nMaxNumberOfResults = (%d)\r\nNumberOfResults = (%d)\r\nID:"
#define MESS_INIFILE_GET_FAILED "sample.ini get failed."
#define PATH_ENROLLED_FILE ".\\enroll\\*.dat"
#define PATH_ENROLLED_FILE_IN_ID ".\\enroll\\%04d_*.dat"
#define PATH_ENROLLED_DIRECTORY ".\\enroll"
#define PATH_ENROLLED_NAME TEXT(".\\enroll\\%s_%03d.dat")
#define PATH_ENROLLED_DIRECTORY ".\\enroll"
#define INIFILE_NAME ".\\sample.ini"
/*--------------------------------------------------------------------
*
* global
*
*--------------------------------------------------------------------*/
static BioAPI_HANDLE gModuleHandle;
static BioAPI_RETURN bioReturn;
static BioAPI_BIR_HANDLE StoredTemplate;
static BioAPI_INPUT_BIR CapturedBIR;
static BioAPI_INPUT_BIR ProcessedBIR;
static BioAPI_INPUT_BIR StoredBIR;
static BioAPI_BIR_HANDLE CapturedTemplate;
static BioAPI_BOOL boolie = BioAPI_FALSE;
static BioAPI_BOOL result;
static BioAPI_FRR frr = HF_PID_BSP_LEVEL_MIDDLE; // Security Level
static BioAPI_FAR AchievedFAR = 1;
static uint32 MaxNumberOfResults = 1;
static uint32 NumberOfResults;
static BioAPI_CANDIDATE_ARRAY_PTR Candidates;
static BioAPI_INPUT_BIR InputBirProcessed_PopulationElem[MaxN];
static BioAPI_IDENTIFY_POPULATION *Population;
static BioAPI_BIR_PTR lpPopulationAryBIRData_Member;
static HANDLE hSearch;
static char fname_db[MAX_PATH + 1];
static char fid_db[MAX_PATH + 1];
static WIN32_FIND_DATA fd;
static BioAPI_BIR_ARRAY_POPULATION lPopulationAryBIRData;
static BioAPI_BIR_PTR FullBIR;
static BioAPI_UUID BSPUuid = HF_PID_BSP_UUID;
typedef struct stBIR_AND_FileName {
BioAPI_BIR* pPopulationBIRPointer;
char lptszUserName[_MAX_PATH];
}BIR_AND_FILENAME;
static BIR_AND_FILENAME RetBIR_And_FileName[MaxN];
static BioAPI_CANDIDATE_ARRAY lpWorkCandidates;
static char IdentificationMessage[MAX_PATH + 1];
/*--------------------------------------------------------------------
*
* function
*
*--------------------------------------------------------------------*/
void PrintErrorCode(BioAPI_RETURN bioReturn);
void LoadBSP(BioAPI_HANDLE_PTR gModuleHandle);
void ReleaseBSP(BioAPI_HANDLE_PTR gModuleHandle);
BioAPI_RETURN OutputToFile(BioAPI_HANDLE gModuleHandle, LPTSTR lptszUserName, BioAPI_BIR_HANDLE BirHandle);
BioAPI_RETURN InputFromFile(LPTSTR lptszUserName, BioAPI_INPUT_BIR * InputBir, uint32 count);
/*--------------------------------------------------------------------
*
* End of misc.h
*
*--------------------------------------------------------------------*/
misc.cpp
/*---------------------------------------------------------------*/
/* */
/* Copyright (C) NEC Corporation 2011 */
/* NEC CONFIDENTIAL AND PROPRIETARY */
/* */
/* All rights reserved by NEC Corporation. This program must be */
/* used solely for the purpose for which it was furnished by NEC */
/* Corporation. No part of this program may be reproduced or */
/* disclosed to others, in any form, without the prior written */
/* permission of NEC Corporation. Use of copyright notice does */
/* not evidence publication of this program. */
/* */
/* NEC Corporation accepts no responsibility for any damages */
/* resulting from the use of this sample code. */
/* This sample code is provided "AS IS", and its user assume all */
/* risks when using it. */
/* */
/*---------------------------------------------------------------*/
//#include "stdafx.h"
#include "misc.h"
/*--------------------------------------------------------------------
*
* PrintErrorCode
*
*--------------------------------------------------------------------*/
void PrintErrorCode(
BioAPI_RETURN bioReturn /* INPUT : BioAPI Error Code */
)
{
TCHAR szMessage[_MAX_PATH];
// ID Input Error
if (bioReturn == ERRCODE_ID_INPUT_ERROR)
{
wsprintf(szMessage, MESS_ID_INPUT_ERROR);
MessageBox(GetActiveWindow(), szMessage, MESS_BIOAPI_ERROR, MB_OK | MB_ICONSTOP);
}
// Not Enrolled Error
else if (bioReturn == ERRCODE_NOT_ENROLLED)
{
wsprintf(szMessage, MESS_NOT_ENROLLED);
MessageBox(GetActiveWindow(), szMessage, MESS_BIOAPI_ERROR, MB_OK | MB_ICONSTOP);
}
// BioAPI Error
else
{
wsprintf(szMessage, MESS_BIOAPI_ERROR_CODE, bioReturn);
MessageBox(GetActiveWindow(), szMessage, MESS_BIOAPI_ERROR, MB_OK | MB_ICONSTOP);
}
return;
}
/*--------------------------------------------------------------------
*
* LoadBSP
*
*--------------------------------------------------------------------*/
void LoadBSP(
BioAPI_HANDLE_PTR gModuleHandle /* INPUT : BSP Module Handle */
)
{
BioAPI_VERSION bioVersion;
bioVersion.Major = BioAPI_MAJOR;
bioVersion.Minor = BioAPI_MINOR;
bioReturn = BioAPI_Init(&bioVersion, 0, NULL, 0, NULL);
if (BioAPI_OK != bioReturn)
{
PrintErrorCode(bioReturn);
return;
}
bioReturn = BioAPI_ModuleLoad(&BSPUuid, 0, NULL, 0);
if (BioAPI_OK != bioReturn)
{
PrintErrorCode(bioReturn);
return;
}
bioReturn = BioAPI_ModuleAttach(&BSPUuid, &bioVersion, &BioAPIWinMemoryFuncs, 0, 0, 0, 0, NULL, 0, NULL, gModuleHandle);
if (BioAPI_OK != bioReturn)
{
PrintErrorCode(bioReturn);
return;
}
return;
}
/*--------------------------------------------------------------------
*
* ReleaseBSP
*
*--------------------------------------------------------------------*/
void ReleaseBSP(
BioAPI_HANDLE_PTR gModuleHandle /* INPUT : BSP Module Handle */
)
{
if (gModuleHandle != 0)
{
bioReturn = BioAPI_ModuleDetach(*gModuleHandle);
if (BioAPI_OK != bioReturn)
{
PrintErrorCode(bioReturn);
return;
}
gModuleHandle = 0;
}
bioReturn = BioAPI_ModuleUnload(&BSPUuid, NULL, 0);
if (BioAPI_OK != bioReturn)
{
PrintErrorCode(bioReturn);
return;
}
bioReturn = BioAPI_Terminate();
if (BioAPI_OK != bioReturn)
{
PrintErrorCode(bioReturn);
return;
}
return;
}
/*--------------------------------------------------------------------
*
* OutputToFile
*
*--------------------------------------------------------------------*/
BioAPI_RETURN OutputToFile(
BioAPI_HANDLE gModuleHandle, /* INPUT : BSP Module Handle */
LPTSTR lptszUserName, /* INPUT : Save File Neme */
BioAPI_BIR_HANDLE BirHandle /* INPUT : Save BIR Data */
)
{
TCHAR szFileName[_MAX_PATH];
HANDLE hFile;
DWORD dwBytesToWrite, dwBytesWritten;
BioAPI_BIR_PTR birData = NULL;
BioAPI_RETURN bioReturn;
uint32 count = 0;
struct stat buff;
// Retrieve the BIR from the BSP
bioReturn = BioAPI_GetBIRFromHandle(gModuleHandle, BirHandle, &birData);
if (bioReturn != BioAPI_OK)
{
return bioReturn;
}
// Search Save FileName
_mkdir(PATH_ENROLLED_DIRECTORY);
while (1)
{
if (++count > MaxEnrollNum)
{
return -1; // Can't make file
}
wsprintf(szFileName, PATH_ENROLLED_NAME, lptszUserName, count);
if ((stat(szFileName, &buff) != 0))
{
break;
}
}
// Open the file
hFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
return 0; // this return value is ignored anyway
}
// Write the header
WriteFile(hFile, &(birData->Header), sizeof(BioAPI_BIR_HEADER), &dwBytesWritten, NULL);
// Write the biometric data
dwBytesToWrite = LocalEndian4(birData->Header.Length) - sizeof(BioAPI_BIR_HEADER);
WriteFile(hFile, birData->BiometricData, dwBytesToWrite, &dwBytesWritten, NULL);
// Write the signature if present
if (birData->Signature)
{
WriteFile(hFile, &(birData->Signature->Length), 4, &dwBytesWritten, NULL);
dwBytesToWrite = LocalEndian4(birData->Signature->Length);
WriteFile(hFile, birData->Signature->Data, dwBytesToWrite, &dwBytesWritten, NULL);
}
// Close the file
CloseHandle(hFile);
// Free all the memory allocated for the BIR
if (birData != NULL) {
GlobalFree(birData->BiometricData);
if (birData->Signature)
{
GlobalFree(birData->Signature->Data);
GlobalFree(birData->Signature);
}
GlobalFree(birData);
}
return BioAPI_OK;
}
/*--------------------------------------------------------------------
*
* InputFromFile
*
*--------------------------------------------------------------------*/
BioAPI_RETURN InputFromFile(
LPTSTR lptszUserName, /* INPUT : Load File Name */
BioAPI_INPUT_BIR * InputBir, /* OUTPUT : Load BIR Data */
uint32 count /* INPUT : User Enroll Number */
)
{
TCHAR szFileName[_MAX_PATH];
HANDLE hFile;
DWORD dwBytesToRead, dwBytesRead;
BioAPI_BIR *bir;
// Open the file
wsprintf(szFileName, PATH_ENROLLED_NAME, lptszUserName, count);
hFile = CreateFile(szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
return BioAPIERR_BSP_RECORD_NOT_FOUND; // Return this BSP error if the file isn't present
}
// Read the header in, to determine the size
dwBytesToRead = sizeof(BioAPI_BIR_HEADER);
bir = (BioAPI_BIR*)GlobalAlloc(GPTR, sizeof(BioAPI_BIR));
if (!bir)
{
return !BioAPI_OK;
}
ReadFile(hFile, &(bir->Header), dwBytesToRead, &dwBytesRead, NULL);
// Read the biometric data
dwBytesToRead = LocalEndian4(bir->Header.Length) - sizeof(BioAPI_BIR_HEADER);
bir->BiometricData = (BioAPI_BIR_BIOMETRIC_DATA_PTR)GlobalAlloc(GPTR, dwBytesToRead);
if (!bir->BiometricData)
{
return !BioAPI_OK;
}
ReadFile(hFile, bir->BiometricData, dwBytesToRead, &dwBytesRead, NULL);
// Read the signature if present
dwBytesToRead = 0;
if (ReadFile(hFile, &dwBytesToRead, 4, &dwBytesRead, NULL) != 0 && dwBytesToRead != 0)
{
bir->Signature = (BioAPI_DATA_PTR)GlobalAlloc(GPTR, sizeof(BioAPI_DATA));
if (!bir->Signature)
{
return !BioAPI_OK;
}
bir->Signature->Length = dwBytesToRead;
dwBytesToRead = LocalEndian4(dwBytesToRead);
bir->Signature->Data = (uint8 *)GlobalAlloc(GPTR, dwBytesToRead);
if (!bir->Signature->Data)
{
return !BioAPI_OK;
}
ReadFile(hFile, bir->Signature->Data, dwBytesToRead, &dwBytesRead, NULL);
}
// Close the file
CloseHandle(hFile);
// Set up the return values
InputBir->InputBIR.BIR = bir;
InputBir->Form = BioAPI_FULLBIR_INPUT;
return BioAPI_OK;
}
/*--------------------------------------------------------------------
*
* End of misc.cpp
*
*--------------------------------------------------------------------*/
Errors:
1>libucrt.lib(exit.obj) : error LNK2005: _cexit already defined in ucrtd.lib(ucrtbased.dll)
1>libucrt.lib(onexit.obj) : error LNK2005: _crt_at_quick_exit already defined in ucrtd.lib(ucrtbased.dll)
1>libucrt.lib(onexit.obj) : error LNK2005: _crt_atexit already defined in ucrtd.lib(ucrtbased.dll)
1>libucrt.lib(onexit.obj) : error LNK2005: _execute_onexit_table already defined in ucrtd.lib(ucrtbased.dll)
1>libucrt.lib(onexit.obj) : error LNK2005: _initialize_onexit_table already defined in ucrtd.lib(ucrtbased.dll)
1>libucrt.lib(onexit.obj) : error LNK2005: _register_onexit_function already defined in ucrtd.lib(ucrtbased.dll)
1>libucrt.lib(environment_initialization.obj) : error LNK2005: _initialize_narrow_environment already defined in ucrtd.lib(ucrtbased.dll)
1>libucrt.lib(exception_filter.obj) : error LNK2005: _seh_filter_dll already defined in ucrtd.lib(ucrtbased.dll)
1>LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library
1>LINK : warning LNK4098: defaultlib 'ucrtd.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
1>C:\Users\Laptop-attendance\source\repos\Bioapitest\x64\Debug\Bioapitest.exe : fatal error LNK1169: one or more multiply defined symbols found
Related
hi guys i want read a memory mapped file created by C++ program , i use the MSDN example for create in C++ the memory mapped file , i want read the string in my metatrader 4 i write this code
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_buffers 1
#property indicator_color1 Blue
#define FILE_MAP_READ 4
#define BUF_SIZE 256
extern string szName = "MyFileMappingObject";
//extern string szName = "Global\\dllmemfilemap";
int handle = 0;
string Data;
#import "kernel32.dll"
int OpenFileMappingA(int dwDesiredAccess, bool bInheritHandle, string lpName);
string MapViewOfFile(int hFileMappingObject, int dwDesiredAccess, int dwFileOffsetHigh, int dwFileOffsetLow, int dwNumberOfBytestoMap);
int CloseHandle( int handle);
int UnmapViewOfFile(string lpBaseAddress);
#import
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
handle = OpenFileMappingA(FILE_MAP_READ, FALSE, szName);
if(handle == 0)
{
Alert("Could not open file mapping object", GetLastError());
}
else
{
Data = MapViewOfFile(handle, FILE_MAP_READ, 0, 0, BUF_SIZE);
Alert(Data);
UnmapViewOfFile(Data);
CloseHandle(handle);
}
return (0);
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
but not open the OpenFileMappingA, return always 0 , anyone have some idea ? thankz at all
You need to call CreateFileMapping before trying to OpenFileMapping.
See example here :
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#define BUF_SIZE 256
TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
TCHAR szMsg[]=TEXT("Message from first process.");
int _tmain()
{
HANDLE hMapFile;
LPCTSTR pBuf;
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
szName); // name of mapping object
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not create file mapping object (%d).\n"),
GetLastError());
return 1;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
return 1;
}
CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
_getch();
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}
strings are unicode in MQL, or similarly 2-bytes wchat_t in C++.
Here is a sample :
#include <cstdlib>
#include <cstring>
#include <string>
char* pBufMQL;
std::wstring ss = L"Sample string";
CopyMemory((PVOID)pBufMQL, ss.c_str(), ss.size() * sizeof(wchar_t));
in MQL:
handle = OpenFileMappingW( FILE_MAP_ALL_ACCESS, FALSE, szName);
if(handle == 0)
{
Alert("Could not open file mapping object", GetLastError());
}
string Data = MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
Print("Done, size: ", StringLen(Data), "-", Data);
p.s. 1: I'm currently struggling with writing from MQL to the buffer and string in MQL are a copy of pBuf(from C++), converted to string.
And I need to modify buffer from MQL.
p.s. 2: p.s. 1 solved, Here is a working example : https://www.mql5.com/en/articles/364#3
I am trying to regulate my console, specifically the screen buffer size and the actual size of the console, preferably by rows and columns of characters.
I have already successfully changed the screen buffer size with SetConsoleScreenBufferSize, but I am having issues resizing the actual window itself. I have tried using SetConsoleWindowInfo and it does not seem to affect anything, and even if it did I would like to change it with rows/columns.
Eventually I would like to make it so the user cannot resize it either. Anyway, here are my instructions that are related.
hConsole = CreateConsoleScreenBuffer(
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL
);
SMALL_RECT size = { 0, 0, 240, 240 }; // Test values
SetConsoleActiveScreenBuffer(hConsole);
SetConsoleScreenBufferSize(hConsole, { width, height });
SetConsoleWindowInfo(hConsole, TRUE, &size);
You can see the old demo function from MS : demoSizeInfo
I converted the functions to Unicode =>
#define WSTR1(x) L##x
#define WSTR2(x) WSTR1(x)
#define __WFILE__ WSTR2(__FILE__)
void perr(PWCHAR szFileName, int line, PWCHAR szApiName, DWORD dwError);
#define PERR(bSuccess, api) {if (!(bSuccess)) perr(__WFILE__, __LINE__, api, GetLastError());}
void resizeConBufAndWindow(HANDLE hConsole, SHORT xSize, SHORT ySize);
void demoSizeInfo(HANDLE hConOut);
// Test demo on your hConsole handle
demoSizeInfo(hConsole);
Functions =>
void resizeConBufAndWindow(HANDLE hConsole, SHORT xSize, SHORT ySize)
{
CONSOLE_SCREEN_BUFFER_INFO csbi; /* hold current console buffer info */
BOOL bSuccess;
SMALL_RECT srWindowRect; /* hold the new console size */
COORD coordScreen;
bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);
PERR(bSuccess, L"GetConsoleScreenBufferInfo");
/* get the largest size we can size the console window to */
coordScreen = GetLargestConsoleWindowSize(hConsole);
PERR(coordScreen.X | coordScreen.Y, L"GetLargestConsoleWindowSize");
/* define the new console window size and scroll position */
srWindowRect.Right = (SHORT)(min(xSize, coordScreen.X) - 1);
srWindowRect.Bottom = (SHORT)(min(ySize, coordScreen.Y) - 1);
srWindowRect.Left = srWindowRect.Top = (SHORT)0;
/* define the new console buffer size */
coordScreen.X = xSize;
coordScreen.Y = ySize;
/* if the current buffer is larger than what we want, resize the */
/* console window first, then the buffer */
if ((DWORD)csbi.dwSize.X * csbi.dwSize.Y > (DWORD)xSize * ySize)
{
bSuccess = SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect);
PERR(bSuccess, L"SetConsoleWindowInfo");
bSuccess = SetConsoleScreenBufferSize(hConsole, coordScreen);
PERR(bSuccess, L"SetConsoleScreenBufferSize");
}
/* if the current buffer is smaller than what we want, resize the */
/* buffer first, then the console window */
if ((DWORD)csbi.dwSize.X * csbi.dwSize.Y < (DWORD)xSize * ySize)
{
bSuccess = SetConsoleScreenBufferSize(hConsole, coordScreen);
PERR(bSuccess, L"SetConsoleScreenBufferSize");
bSuccess = SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect);
PERR(bSuccess, L"SetConsoleWindowInfo");
}
/* if the current buffer *is* the size we want, don't do anything! */
return;
}
/*********************************************************************
* FUNCTION: perr(PCHAR szFileName, int line, PCHAR szApiName, *
* DWORD dwError) *
* *
* PURPOSE: report API errors. Allocate a new console buffer, display *
* error number and error text, restore previous console *
* buffer *
* *
* INPUT: current source file name, current line number, name of the *
* API that failed, and the error number *
* *
* RETURNS: none *
*********************************************************************/
/* maximum size of the buffer to be returned from FormatMessage */
#define MAX_MSG_BUF_SIZE 512
void perr(PWCHAR szFileName, int line, PWCHAR szApiName, DWORD dwError)
{
WCHAR szTemp[1024];
DWORD cMsgLen;
WCHAR *msgBuf; /* buffer for message text from system */
int iButtonPressed; /* receives button pressed in the error box */
/* format our error message */
wsprintf(szTemp, L"%s: Error %d from %s on line %d:\n", szFileName,
dwError, szApiName, line);
/* get the text description for that error number from the system */
cMsgLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ALLOCATE_BUFFER | 40, NULL, dwError,
MAKELANGID(0, SUBLANG_ENGLISH_US), (LPTSTR)&msgBuf, MAX_MSG_BUF_SIZE,
NULL);
if (!cMsgLen)
wsprintf(szTemp + lstrlen(szTemp), L"Unable to obtain error message text! \n"
L"%s: Error %d from %s on line %d", __FILE__,
GetLastError(), "FormatMessage", __LINE__);
else
lstrcat(szTemp, msgBuf);
lstrcat(szTemp, L"\n\nContinue execution?");
MessageBeep(MB_ICONEXCLAMATION);
iButtonPressed = MessageBox(NULL, szTemp, L"Console API Error",
MB_ICONEXCLAMATION | MB_YESNO | MB_SETFOREGROUND);
/* free the message buffer returned to us by the system */
if (cMsgLen)
LocalFree((HLOCAL)msgBuf);
if (iButtonPressed == IDNO)
exit(1);
return;
}
/*************************************************************************
* FUNCTION: myPuts(HANDLE hConsole, PCHAR s) *
* *
* PURPOSE: write a string to the given console buffer, appending a cr/lf *
* *
* INPUT: the console to write to, and the string to write *
* *
* RETURNS: TRUE if success, FALSE if an error occured *
*************************************************************************/
BOOL myPuts(HANDLE hConsole, PWCHAR s)
{
BOOL bSuccess;
DWORD cCharsWritten;
const PWCHAR crlf = L"\n";
BOOL retflag = TRUE;
/* write the string to the console */
bSuccess = WriteConsole(hConsole, s, lstrlen(s), &cCharsWritten, NULL);
PERR(bSuccess, L"WriteConsole");
retflag = bSuccess;
/* put a carriage return & line feed after the string */
bSuccess = WriteConsole(hConsole, crlf, lstrlen(crlf), &cCharsWritten, NULL);
PERR(bSuccess, L"WriteConsole");
if (!bSuccess)
retflag = FALSE;
return(retflag);
}
/*********************************************************************
* FUNCTION: setConTitle *
* *
* PURPOSE: simply set the current console title. Called by each demo *
* function to specify the name of the source file that *
* contains the demo function. *
* *
* INPUT: null terminated string *
*********************************************************************/
void setConTitle(PCHAR szTitle)
{
BOOL bSuccess;
/* set the console title to the input string parameter */
bSuccess = SetConsoleTitleA(szTitle);
PERR(bSuccess, L"SetConsoleTitle");
return;
}
/*****************************************************************
* FUNCTION: myGetchar(void) *
* *
* PURPOSE: get a single character from the standard input handle *
* *
* INPUT: none *
* *
* RETURNS: the char received from the console *
*****************************************************************/
WCHAR myGetchar(void)
{
HANDLE hStdIn; /* standard input */
DWORD dwInputMode; /* to save the input mode */
BOOL bSuccess;
WCHAR chBuf; /* buffer to read into */
DWORD dwRead;
/* get the standard input handle to read from. There is only one */
/* instance of standard input per process at any given time */
hStdIn = GetStdHandle(STD_INPUT_HANDLE);
PERR(hStdIn != INVALID_HANDLE_VALUE, L"GetStdHandle");
/* save the console mode */
bSuccess = GetConsoleMode(hStdIn, &dwInputMode);
PERR(bSuccess, L"GetconsoleMode");
/* disable line input. Echo input must be disabled when disabling */
/* line input */
bSuccess = SetConsoleMode(hStdIn, dwInputMode & ~ENABLE_LINE_INPUT &
~ENABLE_ECHO_INPUT);
PERR(bSuccess, L"SetConsoleMode");
/* read a character from the console input */
bSuccess = ReadFile(hStdIn, &chBuf, sizeof(chBuf), &dwRead, NULL);
PERR(bSuccess, L"ReadFile");
/* restore the original console input mode */
bSuccess = SetConsoleMode(hStdIn, dwInputMode);
PERR(bSuccess, L"SetConsoleMode");
return(chBuf);
}
/********************************************************************
* FUNCTION: getConX(HANDLE hCon) *
* *
* PURPROSE: to get the current width of the console output buffer *
* *
* INPUT: the handle to get the information for *
* *
* RETURNS: the width of the current console output buffer, in chars *
********************************************************************/
SHORT getConX(HANDLE hCon)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
BOOL bSuccess;
bSuccess = GetConsoleScreenBufferInfo(hCon, &csbi);
PERR(bSuccess, L"GetConsoleScreenBufferInfo");
return(csbi.dwSize.X);
}
/*********************************************************************
* FUNCTION: getConY(HANDLE hCon) *
* *
* PURPROSE: to get the current height of the console output buffer *
* *
* INPUT: the handle to get the information for *
* *
* RETURNS: the height of the current console output buffer, in chars *
*********************************************************************/
SHORT getConY(HANDLE hCon)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
BOOL bSuccess;
bSuccess = GetConsoleScreenBufferInfo(hCon, &csbi);
PERR(bSuccess, L"GetConsoleScreenBufferInfo");
return(csbi.dwSize.Y);
}
void demoSizeInfo(HANDLE hConOut)
{
SHORT sConX, sConY; /* save the current console dimensions */
setConTitle(__FILE__);
myPuts(hConOut, L"Let's resize the console buffer and window to a 40 x 25\n"
L"size screen by using the SetConsoleScreenBufferSize and\n"
L"SetConsoleWindowInfo APIs. Hit enter to continue...\n");
myGetchar();
sConX = getConX(hConOut);
sConY = getConY(hConOut);
resizeConBufAndWindow(hConOut, (SHORT)40, (SHORT)25);
myPuts(hConOut, L"Now let's resize to a large size of\n"
L"200 x 200 - notice that the console\n"
L"window size will not grow larger than\n"
L"the physical screen size. Hit enter\n"
L"to continue...\n");
myGetchar();
resizeConBufAndWindow(hConOut, (SHORT)200, (SHORT)200);
myPuts(hConOut, L"Now let's resize back to our original size screen.\n"
L"Hit enter to continue...\n");
myGetchar();
resizeConBufAndWindow(hConOut, sConX, sConY);
myPuts(hConOut, L"Now we're back to our original size. Hit enter to return...");
myGetchar();
return;
}
I have been trying to do sip_init for multiple sip thread in pjsip stack, but i am not able init for multi threads and if possible then how can we get values of different threads from same variables.
I am adding here the code that i have tried with.
#include<stdio.h>
#include<stdlib.h>
#include <pthread.h>
#include <stdint.h>
#include <arpa/inet.h>
#include <inttypes.h>
#include <pjsip.h>
#include <pjmedia.h>
#include <pjmedia-codec.h>
#include <pjsip_ua.h>
#include <pjsip_simple.h>
#include <pjlib-util.h>
#include <pjlib.h>
static __thread trunk_t *sip_trunk;
static void *sip_init(trunk_t *trunk)
{
pj_pool_t *pool = NULL;
pj_status_t status;
pjmedia_codec_mgr *codec_mgr;
int i;
struct event *ev;
struct timeval tv;
struct in_addr listen_addr;
sip_trunk = trunk;
/* Must init PJLIB first: */
status = pj_init();
PJ_ASSERT_RETURN(status == PJ_SUCCESS, NULL);
pj_log_set_level(5);
/* Then init PJLIB-UTIL: */
status = pjlib_util_init();
PJ_ASSERT_RETURN(status == PJ_SUCCESS, NULL);
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
sip_thread_reg_process();
tv.tv_sec = 0;
tv.tv_usec = 100000;
ev = event_new(trunk->rt_thr_ess->proto_evbase, -1, EV_READ|EV_PERSIST, rx_data, NULL);
event_add(ev, &tv);
/* Create global endpoint: */
const pj_str_t *hostname;
const char *endpt_name;
/* Endpoint MUST be assigned a globally unique name.
* The name will be used as the hostname in Warning header.
*/
/* For this implementation, we'll use hostname for simplicity */
hostname = pj_gethostname();
endpt_name = hostname->ptr;
/* Create the endpoint: */
status = pjsip_endpt_create(&cp.factory, endpt_name, &g_endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, NULL);
/*
* Add UDP transport, with hard-coded port
* Alternatively, application can use pjsip_udp_transport_attach() to
* start UDP transport, if it already has an UDP socket (e.g. after it
* resolves the address with STUN).
*/
pj_sockaddr addr;
pjsip_tpfactory *tpfactory;
for(i=0; i < 1; ++i) {
listen_addr.s_addr = trunk->listen_ip;
pj_str_t ip_str = pj_str(strdup(inet_ntoa(listen_addr)));
pj_sockaddr_init(AF, &addr, &ip_str, (pj_uint16_t)trunk- >listen_port);
if(AF == pj_AF_INET()) {
status = pjsip_udp_transport_start( g_endpt, &addr.ipv4, NULL,
1, &transport[i]);
status = pjsip_tcp_transport_start( g_endpt, &addr.ipv4, 1, & tpfactory);
} else if(AF == pj_AF_INET6()) {
status = pjsip_udp_transport_start6(g_endpt, &addr.ipv6, NULL,
1, NULL);
} else {
status = PJ_EAFNOTSUP;
}
if(status != PJ_SUCCESS) {
app_perror(THIS_FILE, "Unable to start UDP transport", status);
return NULL;
}
}
/*
* Init transaction layer.
* This will create/initialize transaction hash tables etc.
*/
status = pjsip_tsx_layer_init_module(g_endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, NULL);
/*
* Initialize UA layer module.
* This will create/initialize dialog hash tables etc.
*/
status = pjsip_ua_init_module( g_endpt, NULL );
PJ_ASSERT_RETURN(status == PJ_SUCCESS, NULL);
/*
* Init invite session module.
* The invite session module initialization takes additional argument,
* i.e. a structure containing callbacks to be called on specific
* occurence of events.
*
* The on_state_changed and on_new_session callbacks are mandatory.
* Application must supply the callback function.
*
* We use on_media_update() callback in this application to start
* media transmission.
*/
pjsip_inv_callback inv_cb;
/* Init the callback for INVITE session: */
pj_bzero(&inv_cb, sizeof(inv_cb));
inv_cb.on_state_changed = &call_on_state_changed;
inv_cb.on_new_session = &call_on_forked;
inv_cb.on_media_update = &call_on_media_update;
inv_cb.on_rx_offer = &call_on_rx_offer;
/* Initialize invite session module: */
status = pjsip_inv_usage_init(g_endpt, &inv_cb);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, NULL);
/* Initialize 100rel support */
status = pjsip_100rel_init_module(g_endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, NULL);
/*
* Register our module to receive incoming requests.
*/
status = pjsip_endpt_register_module(g_endpt, &mod_ics);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, NULL);
/*
* Register message logger module.
*/
status = pjsip_endpt_register_module( g_endpt, &msg_logger);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, NULL);
/*
* Initialize media endpoint.
* This will implicitly initialize PJMEDIA too.
*/
status = pjmedia_endpt_create(&cp.factory,
pjsip_endpt_get_ioqueue(g_endpt),
0, &g_med_endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, NULL);
/* Initializing g711 codec */
#if CODEC_G711
status = pjmedia_codec_g711_init(g_med_endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, NULL);
/*
* Setting PCMA as the Highest priority codec */
codec_mgr = pjmedia_endpt_get_codec_mgr(g_med_endpt);
pj_str_t codec_id = pj_str("PCMA/8000/1");
pjmedia_codec_mgr_set_codec_priority(codec_mgr, &codec_id, PJMEDIA_CODEC_PRIO_HIGHEST);
#endif
/* Initializing g722 codec */
#if CODEC_G722
status = pjmedia_codec_g722_init(g_med_endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, NULL);
#endif
#if CODEC_G729
/* Initializing g729 codec */
status = pjmedia_codec_g729_init(g_med_endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, NULL);
#endif
#endif
pool = pjsip_endpt_create_pool(g_endpt, "Generic Pool", 512, 512);
pj_str_t realm_str = pj_str("google.com");
status = pjsip_auth_srv_init(pool, &auth_server, &realm_str, digest_lookup, PJSIP_AUTH_SRV_IS_PROXY);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, NULL);
#if ENABLE_VIDEO
/* Only for Video this video format manager creation done */
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
#endif
#if H264_CODE
PJ_ASSERT_RETURN(status == PJ_SUCCESS, NULL);
#endif
return NULL;
}
int main()
{
trunk_t *sip_trunk;
trunk_t *sip_anthr_trunk;
pthread_t sid;
pthread_t tid;
pthread_create(&sid, NULL, sip_init, sip_trunk);
pthread_join(sid, NULL);
pthread_create(&tid, NULL, sip_init, sip_anthr_trunk);
pthread_join(tid, NULL);
return 0;
}
I have an error and I don´t have idea what´s the problem.
I'm trying to compile one example of Open NFC for a project. The example is test_ndef_url. I do not know if the problem to a coding error or a library of windows is due, shellapi.h.
Test_ndef_url code is:
/*
* Copyright (c) 2007-2012 Inside Secure, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*******************************************************************************
Implementation of the Example 1 for Open NFC.
*******************************************************************************/
/* Allow use of features specific to Windows XP or later. */
#ifndef _WIN32_WINNT
/* Change this to the appropriate value to target other versions of Windows. */
#define _WIN32_WINNT 0x0601
#endif
/* Suppress many files from the compilation */
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#include "shellapi.h"
#include <stdio.h>
#include <stdlib.h>
#include "open_nfc.h"
#include "win32_test_core.h"
/* Type definititions */
/* ------------------ */
/* Local prototypes */
/* ---------------- */
/* Global variables */
/* ---------------- */
struct __testContext
{
char16_t* pURL;
bool_t bVirtualTag;
bool_t bReadTest;
W_HANDLE hRegistry;
} ;
struct __testContext g_testCtx = { null, W_FALSE, W_FALSE, W_NULL_HANDLE };
/* Callback function of type tWBasicGenericHandleCallbackFunction */
static void ReadMessageOnAnyTagCompletedURL(void *pCallbackParameter, W_HANDLE hMessage, W_ERROR nError)
{
if(nError == W_SUCCESS)
{
W_HANDLE hRecord = WNDEFGetRecord(hMessage, 0);
WBasicCloseHandle(hMessage);
if(WRTDIsURIRecord(hRecord))
{
char16_t aURIValue[256];
if(WRTDURIGetValue(hRecord, (char16_t*)&aURIValue, 256) == W_SUCCESS)
{
printf("The URL read in the tag is \"%S\"\n", aURIValue);
ShellExecute(NULL, L"open", (char16_t*)aURIValue, NULL, NULL, SW_SHOWNORMAL);
}
}
else
{
printf("Error URL: Error detected in the tag message.\n");
}
WBasicCloseHandle(hRecord);
}
else if(nError == W_ERROR_ITEM_NOT_FOUND)
{
printf("Error URL: This tag does not contain a message of the right type.\n");
}
else if(nError == W_ERROR_TIMEOUT)
{
printf("Error URL: Lost the communication with the tag.\n");
}
else
{
printf("Error URL: Error 0x%08X returned during the tag detection.\n", nError);
}
printf("Present a new tag to be read or press Ctrl+C to stop the application\n");
}
/* Callback function of type tWBasicGenericCallbackFunction */
static void WriteMessageOnAnyTagCompleted(void *pCallbackParameter, W_ERROR nError)
{
switch(nError)
{
case W_SUCCESS:
printf("The message is written in the tag.\n");
break;
case W_ERROR_LOCKED_TAG:
printf("The operation failed because the tag is locked.\n");
break;
case W_ERROR_TAG_FULL:
printf("The message is too large for the remaining free space in the tag.\n");
break;
default:
printf("An error occured during the writing operation (code 0x%x)\n", nError);
break;
}
StopApplication();
}
/* Receive the event of the virtual tag */
static void VirtualTagEventHandler(void *pCallbackParameter, uint32_t nEventCode)
{
switch(nEventCode)
{
case W_VIRTUAL_TAG_EVENT_SELECTION:
printf("The tag is selected by the reader.\n");
break;
case W_VIRTUAL_TAG_EVENT_READER_LEFT:
printf("The reader left the tag without reading the content.\n");
break;
case W_VIRTUAL_TAG_EVENT_READER_READ_ONLY :
printf("The reader read the tag.\nPresent again a reader to read the virtual tag or press Ctrl+C to stop the application\n");
break;
case W_VIRTUAL_TAG_EVENT_READER_WRITE:
default:
printf("This event should not occur for a read-only virtual tag.\n");
break;
}
}
/**
* GetSpecificTestSyntax
*
* #return String describing the command line syntax specific to this test
**/
char16_t * GetSpecificTestSyntax( void )
{
return L"[url <URL> | virtualurl <URL>\n"
L" - If the parameter is not present, the application waits to read a tag with an URL.\n"
L" - If \"url\" is present, the application waits for a Tag to write the URL.\n"
L" - If \"virtualurl\" is present, the application simulates a tag containing the URL.\n\n"
L"The following tags are supported: Type 2, Type 4-A, Type 4-B, Type 5-2K, Type 5-32K and Type 6.\n\n" ;
}
/**
* VerifyTestConditions
*
* #param[in] nArgc number of arguments
*
* #param[in] pArgv arguments array
*
* #return W_SUCCESS Arguments syntax is correct
* W_ERROR_xxx is syntax error is detected
**/
W_ERROR VerifyTestConditions( int32_t nArgc, char16_t* pArgv[] )
{
if(nArgc == 3)
{
if(wcscmp(pArgv[1], L"url") == 0)
{
g_testCtx.pURL = pArgv[2];
}
else if(wcscmp(pArgv[1], L"virtualurl") == 0)
{
g_testCtx.bVirtualTag = W_TRUE;
g_testCtx.pURL = pArgv[2];
}
else
{
nArgc = 0;
}
}
if((nArgc != 1) && (nArgc != 3))
{
return W_ERROR_BAD_PARAMETER;
}
g_testCtx.bReadTest = (nArgc == 1) ? W_TRUE : W_FALSE ;
return W_SUCCESS;
}
/**
* LaunchTest
*
**/
W_ERROR LaunchTest( void )
{
W_HANDLE hMessage = W_NULL_HANDLE;
if(g_testCtx.bReadTest != W_FALSE)
{
WNDEFReadMessageOnAnyTag(ReadMessageOnAnyTagCompletedURL, null, W_PRIORITY_MAXIMUM,
W_NDEF_TNF_WELL_KNOWN, L"U", &g_testCtx.hRegistry);
printf("Present the tag to be read or press Ctrl+C to stop the application\n");
}
else
{
if(WNDEFCreateNewMessage(&hMessage) != W_SUCCESS)
{
printf("Error: Cannot create the new message.\n");
goto return_function;
}
if(WRTDURIAddRecord(hMessage, g_testCtx.pURL) != W_SUCCESS)
{
printf("Error: Cannot add the URI record in the message.\n");
goto return_function;
}
if(g_testCtx.bVirtualTag == W_FALSE)
{
printf("Ready to write the URL \"%S\"\n", g_testCtx.pURL);
WNDEFWriteMessageOnAnyTag(WriteMessageOnAnyTagCompleted, null, W_PRIORITY_MAXIMUM, hMessage, W_NDEF_ACTION_BIT_ERASE | W_NDEF_ACTION_BIT_FORMAT_ALL, &g_testCtx.hRegistry);
printf("Present the tag to be written or press Ctrl+C to stop the application\n");
}
else
{
W_ERROR nError;
W_HANDLE hVirtualTag;
static const uint8_t aPUPI[] = { 0x01, 0x02, 0x03, 0x04 };
printf("Ready to simulate a tag with the URL \"%S\"\n", g_testCtx.pURL);
/* Create the virtual tag, the maximum length is set to the size of the message */
nError = WVirtualTagCreate(W_PROP_NFC_TAG_TYPE_4_B, aPUPI, sizeof(aPUPI),
WNDEFGetMessageLength(hMessage), &hVirtualTag);
if(nError != W_SUCCESS)
{
printf("Cannot create the virtual tag\n");
}
else
{
/* Write the message in the virtual tag */
nError = WNDEFWriteMessageSync(hVirtualTag, hMessage, W_NDEF_ACTION_BIT_ERASE | W_NDEF_ACTION_BIT_LOCK | W_NDEF_ACTION_BIT_FORMAT_ALL);
if(nError != W_SUCCESS)
{
printf("Cannot write the message in the virtual tag\n");
WBasicCloseHandle(hVirtualTag);
}
else
{
/* Start the tag simulation */
nError = WVirtualTagStartSync(hVirtualTag, VirtualTagEventHandler, null, W_TRUE);
if(nError != W_SUCCESS)
{
printf("Cannot activate the virtual tag\n");
WBasicCloseHandle(hVirtualTag);
}
else
{
printf("Present a reader to read the virtual tag or press Ctrl+C to stop the application\n");
}
}
}
}
}
return_function:
WBasicCloseHandle(hMessage);
/* Go pending on WaitForSingleObject (Card/Tag operation completed or Ctrl-C) */
return W_SUCCESS;
}
/**
* CloseTest
*
**/
void CloseTest( void )
{
WBasicCloseHandle( g_testCtx.hRegistry );
}
The part of the code is defined shellapi.h where ShellExecute is:
#include <winapifamily.h>
/*****************************************************************************\
* *
* shellapi.h - SHELL.DLL functions, types, and definitions *
* *
* Copyright (c) Microsoft Corporation. All rights reserved. *
* *
\*****************************************************************************/
#ifndef _INC_SHELLAPI
#define _INC_SHELLAPI
#include <SpecStrings.h>
//
// Define API decoration for direct importing of DLL references.
//
#ifndef WINSHELLAPI
#if !defined(_SHELL32_)
#define WINSHELLAPI DECLSPEC_IMPORT
#else
#define WINSHELLAPI
#endif
#endif // WINSHELLAPI
#ifndef SHSTDAPI
#if !defined(_SHELL32_)
#define SHSTDAPI EXTERN_C DECLSPEC_IMPORT HRESULT STDAPICALLTYPE
#define SHSTDAPI_(type) EXTERN_C DECLSPEC_IMPORT type STDAPICALLTYPE
#else
#define SHSTDAPI STDAPI
#define SHSTDAPI_(type) STDAPI_(type)
#endif
#endif // SHSTDAPI
#ifndef SHDOCAPI
#if !defined(_SHDOCVW_)
#define SHDOCAPI EXTERN_C DECLSPEC_IMPORT HRESULT STDAPICALLTYPE
#define SHDOCAPI_(type) EXTERN_C DECLSPEC_IMPORT type STDAPICALLTYPE
#else
#define SHDOCAPI STDAPI
#define SHDOCAPI_(type) STDAPI_(type)
#endif
#endif // SHDOCAPI
#if !defined(_WIN64)
#include <pshpack1.h>
#endif
#ifdef __cplusplus
extern "C" { /* Assume C declarations for C++ */
#endif /* __cplusplus */
#pragma region Desktop Family
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
DECLARE_HANDLE(HDROP);
_Success_(return != 0)
SHSTDAPI_(UINT) DragQueryFileA(_In_ HDROP hDrop, _In_ UINT iFile, _Out_writes_opt_(cch) LPSTR lpszFile, _In_ UINT cch);
_Success_(return != 0)
SHSTDAPI_(UINT) DragQueryFileW(_In_ HDROP hDrop, _In_ UINT iFile, _Out_writes_opt_(cch) LPWSTR lpszFile, _In_ UINT cch);
#ifdef UNICODE
#define DragQueryFile DragQueryFileW
#else
#define DragQueryFile DragQueryFileA
#endif // !UNICODE
SHSTDAPI_(BOOL) DragQueryPoint(_In_ HDROP hDrop, _Out_ POINT *ppt);
SHSTDAPI_(void) DragFinish(_In_ HDROP hDrop);
SHSTDAPI_(void) DragAcceptFiles(_In_ HWND hWnd, _In_ BOOL fAccept);
SHSTDAPI_(HINSTANCE) ShellExecuteA(_In_opt_ HWND hwnd, _In_opt_ LPCSTR lpOperation, _In_ LPCSTR lpFile, _In_opt_ LPCSTR lpParameters,
_In_opt_ LPCSTR lpDirectory, _In_ INT nShowCmd);
SHSTDAPI_(HINSTANCE) ShellExecuteW(_In_opt_ HWND hwnd, _In_opt_ LPCWSTR lpOperation, _In_ LPCWSTR lpFile, _In_opt_ LPCWSTR lpParameters,
_In_opt_ LPCWSTR lpDirectory, _In_ INT nShowCmd);
#ifdef UNICODE
#define ShellExecute ShellExecuteW
#else
#define ShellExecute ShellExecuteA
#endif // !UNICODE
_Success_(return > 32) // SE_ERR_DLLNOTFOUND
SHSTDAPI_(HINSTANCE) FindExecutableA(_In_ LPCSTR lpFile, _In_opt_ LPCSTR lpDirectory, _Out_writes_(MAX_PATH) LPSTR lpResult);
_Success_(return > 32) // SE_ERR_DLLNOTFOUND
SHSTDAPI_(HINSTANCE) FindExecutableW(_In_ LPCWSTR lpFile, _In_opt_ LPCWSTR lpDirectory, _Out_writes_(MAX_PATH) LPWSTR lpResult);
#ifdef UNICODE
#define FindExecutable FindExecutableW
#else
#define FindExecutable FindExecutableA
And that tells me the error Microsoft Visual Studio 2013 is:
Warning 1 warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/SAFESEH' specification File:win32_test_core.obj Project:test_ndef_url
Error 2 error LNK2019: unresolved external symbol __imp__ShellExecuteW#24 referenced in function _ReadMessageOnAnyTagCompletedURL File:win32_test_ndef_url.obj Project:test_ndef_url
Error 3 error LNK1120: 1 unresolved externals File:test_ndef_url.exe Project:test_ndef_url
Please, I need help with this.
You are missing the actual body (or a placeholder for a function body) of the ShellExecute function (OK, ShellExecuteW, but that's a detail you don't need to worry about now.) What's in the header is the declaration, but you still need the definition. In this case, that's in a library.
Do these:
Google for "MSDN ShellExecute". MSDN is Microsoft Developer Network, and the place to go for Windows API documentation.
The first link is to https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153%28v=vs.85%29.aspx (for now; Microsoft's websites have been awful at keeping links stable for future reference.)
That's the documentation of the function. Scroll down to the bottom of the page. There is a table that lists the "requirements" for this function. One of them is the "Library", which is "Shell32.lib" in this case.
You need to link to this library. The easiest way, in Visual C++ (which you are using) and for a standard Windows library (which this is) is to add the following line to one of your source files (maybe at the very top): #pragma comment (lib, "Shell32"). (Note the lack of a semicolon.)
and that's it. There are better (more scalable, flexible and portable) methods for larger projects, but you should be fine for this small sample.
Given a handle to a Windows Registry Key, such as the ones that are set by ::RegOpenKeyEx(), is it possible to determine the full path to that key?
I realize that in a simple application all you have to do is look up 5 or 10 lines and read... but in a complex app like the one I'm debugging, the key I'm interested in can be opened from a series of calls.
Use LoadLibrary and NtQueryKey exported function as in the following code snippet.
#include <windows.h>
#include <string>
typedef LONG NTSTATUS;
#ifndef STATUS_SUCCESS
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
#endif
#ifndef STATUS_BUFFER_TOO_SMALL
#define STATUS_BUFFER_TOO_SMALL ((NTSTATUS)0xC0000023L)
#endif
std::wstring GetKeyPathFromKKEY(HKEY key)
{
std::wstring keyPath;
if (key != NULL)
{
HMODULE dll = LoadLibrary(L"ntdll.dll");
if (dll != NULL) {
typedef DWORD (__stdcall *NtQueryKeyType)(
HANDLE KeyHandle,
int KeyInformationClass,
PVOID KeyInformation,
ULONG Length,
PULONG ResultLength);
NtQueryKeyType func = reinterpret_cast<NtQueryKeyType>(::GetProcAddress(dll, "NtQueryKey"));
if (func != NULL) {
DWORD size = 0;
DWORD result = 0;
result = func(key, 3, 0, 0, &size);
if (result == STATUS_BUFFER_TOO_SMALL)
{
size = size + 2;
wchar_t* buffer = new (std::nothrow) wchar_t[size/sizeof(wchar_t)]; // size is in bytes
if (buffer != NULL)
{
result = func(key, 3, buffer, size, &size);
if (result == STATUS_SUCCESS)
{
buffer[size / sizeof(wchar_t)] = L'\0';
keyPath = std::wstring(buffer + 2);
}
delete[] buffer;
}
}
}
FreeLibrary(dll);
}
}
return keyPath;
}
int _tmain(int argc, _TCHAR* argv[])
{
HKEY key = NULL;
LONG ret = ERROR_SUCCESS;
ret = RegOpenKey(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft", &key);
if (ret == ERROR_SUCCESS)
{
wprintf_s(L"Key path for %p is '%s'.", key, GetKeyPathFromKKEY(key).c_str());
RegCloseKey(key);
}
return 0;
}
This will print the key path on the console:
Key path for 00000FDC is
'\REGISTRY\MACHINE\SOFTWARE\Microsoft'.
I was excited to find this article and its well liked solution.
Until I found that my system's NTDLL.DLL did not have NtQueryKeyType.
After some hunting around, I ran across ZwQueryKey in the DDK forums.
It is in C#, but here is the solution that works for me:
enum KEY_INFORMATION_CLASS
{
KeyBasicInformation, // A KEY_BASIC_INFORMATION structure is supplied.
KeyNodeInformation, // A KEY_NODE_INFORMATION structure is supplied.
KeyFullInformation, // A KEY_FULL_INFORMATION structure is supplied.
KeyNameInformation, // A KEY_NAME_INFORMATION structure is supplied.
KeyCachedInformation, // A KEY_CACHED_INFORMATION structure is supplied.
KeyFlagsInformation, // Reserved for system use.
KeyVirtualizationInformation, // A KEY_VIRTUALIZATION_INFORMATION structure is supplied.
KeyHandleTagsInformation, // Reserved for system use.
MaxKeyInfoClass // The maximum value in this enumeration type.
}
[StructLayout(LayoutKind.Sequential)]
public struct KEY_NAME_INFORMATION
{
public UInt32 NameLength; // The size, in bytes, of the key name string in the Name array.
public char[] Name; // An array of wide characters that contains the name of the key.
// This character string is not null-terminated.
// Only the first element in this array is included in the
// KEY_NAME_INFORMATION structure definition.
// The storage for the remaining elements in the array immediately
// follows this element.
}
[DllImport("ntdll.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern int ZwQueryKey(IntPtr hKey, KEY_INFORMATION_CLASS KeyInformationClass, IntPtr lpKeyInformation, int Length, out int ResultLength);
public static String GetHKeyName(IntPtr hKey)
{
String result = String.Empty;
IntPtr pKNI = IntPtr.Zero;
int needed = 0;
int status = ZwQueryKey(hKey, KEY_INFORMATION_CLASS.KeyNameInformation, IntPtr.Zero, 0, out needed);
if ((UInt32)status == 0xC0000023) // STATUS_BUFFER_TOO_SMALL
{
pKNI = Marshal.AllocHGlobal(sizeof(UInt32) + needed + 4 /*paranoia*/);
status = ZwQueryKey(hKey, KEY_INFORMATION_CLASS.KeyNameInformation, pKNI, needed, out needed);
if (status == 0) // STATUS_SUCCESS
{
char[] bytes = new char[2 + needed + 2];
Marshal.Copy(pKNI, bytes, 0, needed);
// startIndex == 2 skips the NameLength field of the structure (2 chars == 4 bytes)
// needed/2 reduces value from bytes to chars
// needed/2 - 2 reduces length to not include the NameLength
result = new String(bytes, 2, (needed/2)-2);
}
}
Marshal.FreeHGlobal(pKNI);
return result;
}
I've only ever tried it while running as Administrator, which may be required.
The result is a bit oddly formatted: \REGISTRY\MACHINE\SOFTWARE\company\product for example, instead of HKEY_LOCAL_MACHINE\SOFTWARE\company\product.
Nominally no because it's just a handle and there is no API that I know of to let you do this in the normal Windows API's.
HOWEVER the Native API has lots of functions some of which can give you handles open for given files and the like so there maybe something similar for the Registry. That and RegMon by SysInternals may do something like this but you'll have to Google I'm afraid :/
You can use RegSaveKey and write it to a file, then look at the file.
Alternatively you can keep a global map of HKEYs to LPCWSTRs and add entries when you open them and do lookups whenever.
You may also be able to do something with the !reg command in WinDBG / NTSD, but you can't just give it the HKEY. You'll have to do some other trickery to get the info you want out of it.
Since std::wstring allows to construct string from pointer and count of characters, and the kernel string always return the count of bytes, it is not necessary to terminated the string with NUL. I do not suggest that to add size or to offset the pointer by constant number directly, it's better to use the real data type like the structure types instead, and std::vector<UCHAR> instead of new for dynamic memory allocating. I modified the code from highly upvoted answer as the followings.
The legacy way, obtaining the function pointer from ntdll.dll dynamically:
#include <ntstatus.h>
#define WIN32_NO_STATUS
#include <windows.h>
#include <winternl.h>
#include <string>
#include <vector>
#define REG_KEY_PATH_LENGTH 1024
typedef enum _KEY_INFORMATION_CLASS {
KeyBasicInformation,
KeyNodeInformation,
KeyFullInformation,
KeyNameInformation,
KeyCachedInformation,
KeyFlagsInformation,
KeyVirtualizationInformation,
KeyHandleTagsInformation,
KeyTrustInformation,
KeyLayerInformation,
MaxKeyInfoClass
} KEY_INFORMATION_CLASS;
typedef struct _KEY_NAME_INFORMATION {
ULONG NameLength;
WCHAR Name[1];
} KEY_NAME_INFORMATION, *PKEY_NAME_INFORMATION;
typedef NTSTATUS (NTAPI *PFN_NtQueryKey)(
__in HANDLE /* KeyHandle */,
__in KEY_INFORMATION_CLASS /* KeyInformationClass */,
__out_opt PVOID /* KeyInformation */,
__in ULONG /* Length */,
__out ULONG * /* ResultLength */
);
std::wstring RegQueryKeyPath(HKEY hKey)
{
std::wstring keyPath;
if (hKey != NULL)
{
HMODULE hinstDLL = GetModuleHandleW(L"ntdll.dll");
if (hinstDLL != NULL)
{
FARPROC pfn = GetProcAddress(hinstDLL, "NtQueryKey");
if (pfn != NULL)
{
NTSTATUS Status;
std::vector<UCHAR> Buffer(FIELD_OFFSET(KEY_NAME_INFORMATION, Name) + sizeof(WCHAR) * REG_KEY_PATH_LENGTH);
KEY_NAME_INFORMATION *pkni;
ULONG Length;
TryAgain:
Status = reinterpret_cast<PFN_NtQueryKey>(pfn)(hKey, KeyNameInformation, Buffer.data(), Buffer.size(), &Length);
switch (Status) {
case STATUS_BUFFER_TOO_SMALL:
case STATUS_BUFFER_OVERFLOW:
Buffer.resize(Length);
goto TryAgain;
case STATUS_SUCCESS:
pkni = reinterpret_cast<KEY_NAME_INFORMATION *>(Buffer.data());
keyPath.assign(pkni->Name, pkni->NameLength / sizeof(WCHAR));
default:
break;
}
}
}
}
return keyPath;
}
If you are using Visual Studio 2015 or above, ntdll.lib is included by default, so I suggest that linking to ntdll.dll statically:
#include <ntstatus.h>
#define WIN32_NO_STATUS
#include <windows.h>
#include <winternl.h>
#pragma comment(lib, "ntdll")
#include <string>
#include <vector>
#define REG_KEY_PATH_LENGTH 1024
typedef enum _KEY_INFORMATION_CLASS {
KeyBasicInformation,
KeyNodeInformation,
KeyFullInformation,
KeyNameInformation,
KeyCachedInformation,
KeyFlagsInformation,
KeyVirtualizationInformation,
KeyHandleTagsInformation,
KeyTrustInformation,
KeyLayerInformation,
MaxKeyInfoClass
} KEY_INFORMATION_CLASS;
typedef struct _KEY_NAME_INFORMATION {
ULONG NameLength;
WCHAR Name[1];
} KEY_NAME_INFORMATION, *PKEY_NAME_INFORMATION;
EXTERN_C NTSYSAPI NTSTATUS NTAPI NtQueryKey(
__in HANDLE /* KeyHandle */,
__in KEY_INFORMATION_CLASS /* KeyInformationClass */,
__out_opt PVOID /* KeyInformation */,
__in ULONG /* Length */,
__out ULONG * /* ResultLength */
);
std::wstring RegQueryKeyPath(HKEY hKey)
{
std::wstring keyPath;
NTSTATUS Status;
std::vector<UCHAR> Buffer(FIELD_OFFSET(KEY_NAME_INFORMATION, Name) + sizeof(WCHAR) * REG_KEY_PATH_LENGTH);
KEY_NAME_INFORMATION *pkni;
ULONG Length;
TryAgain:
Status = NtQueryKey(hKey, KeyNameInformation, Buffer.data(), Buffer.size(), &Length);
switch (Status) {
case STATUS_BUFFER_TOO_SMALL:
case STATUS_BUFFER_OVERFLOW:
Buffer.resize(Length);
goto TryAgain;
case STATUS_SUCCESS:
pkni = reinterpret_cast<KEY_NAME_INFORMATION *>(Buffer.data());
keyPath.assign(pkni->Name, pkni->NameLength / sizeof(WCHAR));
default:
break;
}
return keyPath;
}
Note that NtQueryKey returned STATUS_BUFFER_OVERFLOW but not STATUS_BUFFER_TOO_SMALL on Windows 10 if the supplied buffer is insufficient.
For ntsd/windbg:
!handle yourhandle 4