C++/WinRT: MCVC doesn't linking event callback - linker

I'm trying to use Desktop Capture API in c++ project.
Here is initialisation of frame pool:
ScreenGrabWinCaptureApi::ScreenGrabWinCaptureApi() {
//
// <neccessary stuff for d3d device and capture item creation>
//
framePool = winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool::CreateFreeThreaded(d3dDevice2,
winrt::Windows::Graphics::DirectX::DirectXPixelFormat::B8G8R8A8UIntNormalized,
2, item.Size());
framePool.FrameArrived(&ScreenGrabWinCaptureApi::OnFrameArrived);
captureSession = framePool.CreateCaptureSession(item);
captureSession.StartCapture();
}
and here is ScreenGrabWinCaptureApi::OnFrameArrived definition:
void
ScreenGrabWinCaptureApi::OnFrameArrived(const winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool &sender,
const winrt::Windows::Foundation::IInspectable &) {
// <some buisness logic>
}
I'm trying to build this, and code seems OK to compiler, but linkage fails on framePool.FrameArrived(&ScreenGrabWinCaptureApi::OnFrameArrived); call with
error LNK2019: unresolved external symbol "public: __cdecl winrt::Windows::Foundation::TypedEventHandler<struct winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool,struct winrt::Windows::Foundation::IInspectable>::TypedEventHandler<struct winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool,struct winrt::Windows::Foundation::IInspectable><void (__cdecl ScreenGrabWinCaptureApi::)(struct winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool const &,struct winrt::Windows::Foundation::IInspectable const &)>(void (__cdecl ScreenGrabWinCaptureApi::)(struct winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool const &,struct winrt::Windows::Foundation::IInspectable const &))" (??$?0P8ScreenGrabWinCaptureApi##EAAXAEBUDirect3D11CaptureFramePool#Capture#Graphics#Windows#winrt##AEBUIInspectable#Foundation#45##Z#?$TypedEventHandler#UDirect3D11CaptureFramePool#Capture#Graphics#Windows#winrt##UIInspectable#Foundation#45##Foundation#Windows#winrt##QEAA#P8ScreenGrabWinCaptureApi##EAAXAEBUDirect3D11CaptureFramePool#Capture#Graphics#23#AEBUIInspectable#123##Z#Z) referenced in function "public: __cdecl ScreenGrabWinCaptureApi::ScreenGrabWinCaptureApi(void)" (??0ScreenGrabWinCaptureApi##QEAA#XZ)
I've tried all the way of reinterpret/static casts, introducing variable with method reference, replacing method with clojure, but nothing works. Anybody knows what is reason and how to make this running?

As Simon Mourier said in the comments, I've forgotten to include header to TypedEventHandler. My code works after insertion of the corresponding include:
#include "winrt/Windows.Foundation.h"

Related

VS linker can't find PsGetProcessWow64Process

I am trying to compile one driver in VS, but it shows -
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol __imp__PsGetProcessWow64Process#4 referenced in function _GetProcessModule#8 Garhal C:\Users\Raitis\source\repos\GarHal_CSGO\Garhal\memory.obj 1
Quickly enough, I found a place, where this PsGetProcessWow64Process is being used https://prnt.sc/uffavf
But it is defined, and it's even an official ntos.h function. Just pressing F12 on it finds it easily - https://prnt.sc/uffbjg
Screenshot from error list - https://prnt.sc/uffc6t
The problem is that this in undocumented functions and the linker is having a hard time finding where is the function to put a pointer to it in the generated binary, so first include ntifs.h before ntddk.h like this
#include <ntifs.h>
#include <ntddk.h>
it must be before ntddk or you will get weird errors
then add this line :
NTKERNELAPI PVOID PsGetProcessWow64Process(__in PEPROCESS Process);
which is the signature of the function and now you are ready to call that function
another way to get it is using function pointers and resolving it at run time
#include <ntddk.h>
typedef struct _PEB32 {
UCHAR InheritedAddressSpace;
UCHAR ReadImageFileExecOptions;
UCHAR BeingDebugged;
UCHAR Spare;
ULONG Mutant;
ULONG ImageBaseAddress;
ULONG/*PPEB_LDR_DATA32*/ Ldr;
} PEB32, *PPEB32;
typedef PPEB32 (NTAPI * pfn_PsGetProcessWow64Process) (PEPROCESS Process);
pfn_PsGetProcessWow64Process PsGetProcessWow64Process = NULL;
RtlInitUnicodeString (&usFunctionName, L"PsGetProcessWow64Process");
PsGetProcessWow64Process = (pfn_PsGetProcessWow64Process) (SIZE_T)MmGetSystemRoutineAddress (&usFunctionName);
pPEB32 = PsGetProcessWow64Process (pEProcess);

Link to a DLL in Pelles C

I have a 218KB .dll and a 596KB .so file, both with identical names. I want to link to the .dll to avoid the "unresolved external symbol" error that the linker returns, but I can't find a way to link to the DLL file.
According to this Pelles C forum topic, I need to use the .def file to create a .lib... but I don't have a .def file. This forum topic shows how to use polink to create a .lib from the command line, so I ran polink /? to get some more options. I noticed a /MAKEDEF option, but running this with both the .dll and the .so gives a "No library file specified" fatal error.
I have been trying to do this for three hours, and am out of ideas. I have got to the point where my web searches turn up my own help-requests. There must be a way to do this... How can I link to a .dll?
With information found in the header #include and your details, here is a way to replace the missing function by calling them dynamically from your software.
1- the following prototype is in #include :
typedef float (* XPLMFlightLoop_f)(float inElapsedSinceLastCall, float inElapsedTimeSinceLastFlightLoop, int inCounter, void * inRefcon);
2- some const that you can fill as needed:
const char *sDllPathName = "<Your XPLM_API DLL>.dll";
const char *sXPLMRegisterFlightLoopCallbackName = "XPLMRegisterFlightLoopCallback";
In order to confirm the sXPLMRegisterFlightLoopCallbackName, you can
use the freeware Dependency Walker and check name and format of
the exported functions.
3- declare the prototype of the external function:
Be aware to the calling convention __cdecl or __stdcall
In the current case, the keyword XPLM_API is defined in the XPLMDefs.h as follow:
#define XPLM_API __declspec(dllexport) // meaning __cdecl calling convention
typedef void (__cdecl *XPLMRegisterFlightLoopCallback_PROC)(XPLMFlightLoop_f, float, void *);
4- clone the function to call it in your software:
#include <windows.h>
void XPLMRegisterFlightLoopCallback(XPLMFlightLoop_f inFlightLoop, float inInterval, void * inRefcon)
{
HINSTANCE hInstDLL;
XPLMRegisterFlightLoopCallback_PROC pMyDynamicProc = NULL;
// Load your DLL in memory
hInstDLL = LoadLibrary(sDllPathName);
if (hInstDLL!=NULL)
{
// Search for the XPLM Function
pMyDynamicProc = (XPLMRegisterFlightLoopCallback_PROC) GetProcAddress(hInstDLL, sXPLMRegisterFlightLoopCallbackName);
if (pMyDynamicProc != NULL)
{
// Call the XPLM Function with the orignal parameter
(pMyDynamicProc)(inFlightLoop,inInterval,inRefcon);
return;
}
}
// Do something when DLL is missing or function not found
}
5- just add your described call:
...
XPLMRegisterFlightLoopCallback(callbackfunction, 0, NULL);
...

Video Stabilization - using estimateGlobalMotionLeastSquares opencv 2.4.7

I am a newbie about Video Stabilization field. Now I am researching about it.
I'm coding a small video stabilization demo. But I am stuck in some problems
I use the function "estimateGlobalMotionLeastSquares" in OpenCV to estimate global motion
But it doesn't work
Here is my code:
CvPoint2D32f p0, p1;
vector<Point2f,allocator<Point2f>> ax, by;
ax.push_back(Point2f(2,2));
by.push_back(Point2f(3,2));
Mat t = estimateGlobalMotionLeastSquares(ax,by,AFFINE,0);
For example: I create 2 variables p0,p1 as parameter for the function "
estimateGlobalMotionLeastSquares" and I want to estimate global motion "t".
But when I complied, the error is as:
1>VS_OpenCVDlg.obj : error LNK2001: unresolved external symbol "class cv::Mat __cdecl cv::videostab::estimateGlobalMotionLeastSquares(class std::vector,class std::allocator > > const &,class std::vector,class std::allocator > > const &,int,float *)" (?estimateGlobalMotionLeastSquares#videostab#cv##YA?AVMat#2#ABV?$vector#V?$Point_#M#cv##V?$allocator#V?$Point_#M#cv###std###std##0HPAM#Z)
1>F:\Research\Workspace\VS_OpenCV\Debug\VS_OpenCV.exe : fatal error LNK1120: 1 unresolved externals
Please help me to fix this!!!
Can you give me some examples about that function it?
Try to include the proper file :
#include "opencv2/videostab/videostab.hpp"
And change your code to :
CvPoint2D32f p0, p1;
vector<Point2f,allocator<Point2f>> ax, by;
ax.push_back(Point2f(2,2));
ax.push_back(Point2f(2,3));
ax.push_back(Point2f(2,4));
by.push_back(Point2f(3,2));
by.push_back(Point2f(3,3));
by.push_back(Point2f(3,4));
Mat t = videostab::estimateGlobalMotionLeastSquares(ax,by,3,0);

dlopen issue(OSX)

I have a main application which dynamically loads a dylib, from inside that dylib I would like to call exported functions from my main program. I'm using dlopen(NULL,flag) to retrieve my main applications handle and dlsym(handle, symbol) to get the function.
dlopen gives no error but when I try to dlsym my function I get the following error:
dlerror dlsym(RTLD_NEXT, CallMe): symbol not found
The symbol is exported corrected confirmed by nm
I'm not sure why RTLD_NEXT is there? is this the result of dlopen(NULL,flag)?
How can I solve this problem or achieve my goal?
Or are there other ways to call the main application (preferably not by passing on function pointers to the dylib)?
Thanks in advance!
Added:
Export:
extern "C" {
void CallMe(char* test);
}
__attribute__((visibility("default")))
void CallMe(char* test)
{
NSLog(#"CallMe with: %s",test);
}
Result of nm
...
0000000000001922 T _CallMe
..
Code in dylib:
void * m_Handle;
typedef void CallMe(char* test);
CallMe* m_Function;
m_Handle = dlopen(NULL,RTLD_LAZY); //Also tried RTLD_NOW|RTLD_GLOBAL
if(!m_Handle)
return EC_ERROR;
m_Function = (CallMe*)dlsym(m_Handle, "CallMe");
if(!m_Function)
return EC_ERROR;
m_Function("Hallo");
I think a better approach might be to establish a proprietary protocol with your dynamic library where you initialise it by passing it a struct of function pointers. The dynamic library needs to simply provide some sort of init(const struct *myfuncs), or some such, function and this makes it simpler to implement the dynamic library.
This would also make the implementation more portable.

extern variables in static library, using Objective-C

I've built a static library, to be linked in my iPhone apps. This library uses some global variables and functions, like in C. My problem is, when using for example:
extern
void do_stuff (const int a)
{
return a*a;
}
extern const int a_variable;
extern const int an_array[DEFINED_VALUE];
When I use this function, or access these variables, anywhere in my code, the compiler tells me
"_do_stuff" referenced from:
-[Object testMethod] in tests.o
"_a_variable" referenced from:
-[Object testMethod] in tests.o
"_an_array" referenced from:
-[Object testMethod] in tests.o
Symbol(s) not found
Collect2: Id returned 1 exit status
Has anyone ever faced this problem before? I know I'm doing something stupid, I'm missing some key Objective-C or C concept, but I can't really see what. So I was hoping someone could help me. Thanks in advance.
These are linker errors, telling you that the referenced entities can't be found. Probably this means that you haven't added your library to the project.
As an aside, you probably should distinguish between the place where you declare these things, where they should indeed be declared as extern, and the place where you define them, where they shouldn't be. That is, you might have a header file that includes:
extern void do_stuff (const int a);
extern const int a_variable;
extern const int an_array[];
And then an implementation file that has something like:
void do_stuff (const int a)
{
return a*a;
}
const int a_variable = 42;
const int an_array[DEFINED_VALUE] = { 1, 2, 3, 4 };
As another aside, calling something a_variable when it's actually a const is a bit misleading!
#walkytalky Well I ran nm on the .a filtered with grep to see if those symbols were exported.
host-006:Release-iphonesimulator <username>$ nm -g libCardLib.a | grep CP_
nm: no name list
U _CP_BACK
U _CP_FILE_EXTENSION_SUFFIX
U _CP_FILE_PATH
U _CP_SUIT_PREFIX
U _CP_VALUE_PREFIX
00002020 D _CP_BACK
00002018 D _CP_FILE_EXTENSION_SUFFIX
0000201c D _CP_FILE_PATH
00002024 D _CP_FRONT
00002108 D _CP_SUIT_PREFIX
0000210c D _CP_VALUE_PREFIX
nm: no name list
nm: no name list
nm: no name list
So it seems that for each symbol there's an undefined copy?

Resources