I am following the MediaLiveServiceJPEG example to grab live data from a camera stream. My understanding of the example is that it uses LoadLibrary to load ServerCommandServiceClient.dll, gets the address of the CreateInstance() function, and then calls it to create an instance of ImServerCommandServiceClient *client.
I am trying to do the same except instead of using LoadLibrary, I’m linking against ToolkitFactoryProvider.lib. I then call NmServerCommandService::CreateInstance() and assign to a variable; the address in the debugger looks valid.
The code then throws an exception when I call client_->SetServerHostName(vmsServer.c_str(), vmsServer.length()), the detail being Could not parse toolkit configuration xml. If I use LoadLibrary in my code, the call to SetServerHostName succeeds.
Now, I also tried mimicking the casting that’s happening in the example code, but it throws an access violation exception on the second line:
NmServerCommandService::CreateInstanceFuncPtr_t createInstanceFuncPtr = reinterpret_cast<NmServerCommandService::CreateInstanceFuncPtr_t>(NmServerCommandService::CreateInstance());
createInstanceFuncPtr();
What am I missing here?
Hi Andrey,
You are linking to ToolkitFactoryProvider and then try to call NmServerCommandServiceClient CreateInstance. this ends up in the ToolkitFactory CreateInstance function and you get the “.. toolkit configuration xml..” error. What you are trying to do will not work as there is no ServerCommandServiceClient .lib available, so you need to load the DLL. you can also static link if possible with your project, or import dll. However the loadLibrary is just as good of an option to use in this case.
Ha, OK, that makes sense now, thanks. I see the CreateInstance function in ToolkitFactory. I will use LoadLibrary then. But just to clear it all up for myself, I have two more questions - when you say “static link”, what do you mean? As far as I understand, ToolkitFactoryProvider.lib is not a static archive. What lib would I link against?
The second question is, if I’m requesting NmServerCommandService::CreateInstance(), why does the linker find it in ToolkitFactoryProvider, NmToolkit::CreateInstance()? It’s in a different namespace. Or is there trickery going on with inheritance?
Hi Andrey,
Depending on what tools you use you can add a dll to a project as a static link so it will be compiled with the project you are making. But load library is the most dynamic way of doing it.
Why the compiler ends in another lib i cant say, it can be just a coincidence that it gets the address of the call in the Toolkit lib. the toolkit and the SCSC dll are not inheriting anything so seems that it was just “lucky” that same name call was found just in another lib.
Got it. Thanks for your help, everything is working well now!