Calling functions from sensors dll c++
Lately during one of my projects i have encountered a problem and
stuck on that point. I have an optical sensor that i have attached to
my pc and it detects colors in terms of RGB values. The sensor is from
a famous company and has been supplied with its own software where I
could see the data. Now to access those RGB values on visual C (so i
could further manipulate the data) the company has provided me with a
header file and lib file and ofcourse the dll file. In the
documentation which is poorly written they have also described some
API functions that have been defined in the header file and should be
called in order to access the values from sensor. For example, int
USB_DLL_API MTCSGetADCAVR2( int iIndex, unsigned short* usBuf, int
iCounts); is one of the functions that I have to use.
Now since I am new to dll programming and want to use these functions
so I could access the values from sensor, I was wondering if someone
could give me a help in order to trigger off my work. I mean I read
some articles on how to make a dll but they are rather confusing and I
think if the company has already given me the files with functions it
should be rather easier to use.
I would deeply appreciate your help.
well it looks like you have everything you need. In fact I believe all you now need to do is to make sure that the header files you got delivered are in the paths where your compiler looks for them (try -I option or configure in your compiler settings/options) and that your DLL is in the DLL-paths (again you might need to configure the compiler/linker options and or the commandline option -L) . Then you should be good to go to run the exe from your Dev environment. If you happy with your development and deploy the exe then you have to put the DLLs in a global path (%PATH% variable on windows).
That should be it. Hope that helps.
Life is a short warm moment -
Death is the long cold rest.
Hardware interfaces are so often so very bad. A lot of them are written in C, too, making it difficult to work into a modern program without wrapping the stuff into an object. And, there is some sort of need to provide an example program that does not make any sense at all, you never get a 1-2-3 this is all you need to get the basics sort of example, its always an A to Z full program that shows off the widgets and extras while hiding the basics!
Anyway, here is how to handle the dll side of it:
add the headers to your project, and # include them.
add the .LIB file to your project
build, and it should all work, try calling the first function or 2 (usually, you have to init the hardware and or tell it your liscense or some sort of keycode to get started).
If you get that far, its simply figureing out what order to call the functions in and which functions you need to call.... Which always turns out to be just a few functions (like 10 or less out of 100 or more provided..).
Dear both of you,
Atleast I have started the way you said and have written a code. What I have done is that in Project settings->preprocesser->include directories, I have given the path of where my header file is, in my case I have set to D:\Program Files\Microsoft Visual Studio\MyProjects\sensor\MTCSApi.h because this is where I have placed my header file (could be wrong). Similarly for linker I have given the path putting .lib file in debug folder where my exe also exists.
I have written a small code
/* The actual call to the function contained in the dll */
int ReturnVal = MTCSInitSystem("0", 0x152a, 0x8220);
just to see, as you said that if sensor was being initialized or not. i made this cpp file like normal win32 console application, and have included the header as shown. Now at compilation I am getting the following errors:
d:\program files\microsoft visual studio\vc98\include\mtcsapi.h(43) : error C2146: syntax error : missing ';' before identifier 'MTCSDllGetVersion'
d:\program files\microsoft visual studio\vc98\include\mtcsapi.h(43) : error C2182: 'USB_DLL_API' : illegal use of type 'void'
d:\program files\microsoft visual studio\vc98\include\mtcsapi.h(43) : fatal error C1004: unexpected end of file found
and when I try to debug it takes me to the header file of the sensor. I was wondering if one of you could suggest that where the problem lies.
this isnt it, but, use:
using namespace std;
int main ()
Those change what you have, which works and probably compiles, from a 1980's style program to a modern c++ look.
Your issue is one of 2 things. Either you failed to include some of the header files,
you need a MFC program to use their library (many libraries make use of MFC stuff, when they should not, usually its something unnecessary like CSTRING or HANDLE etc.)
What it is telling you is that the custom type or function 'MTCSDllGetVersion' cannot be found, so its header or defination is not "seen" by the compiler. Most likely then, your library has multiple headers that must be included.
I was able to fix those errors, it appeared that one of the define statement in the header was missing, now i dont have any compilation error but loads of linking error that I am unable to resolve. I have correctly linked to the header and the lib but still get this. do you have any idea...
sen2.obj : error LNK2001: unresolved external symbol __imp__MTCSGetADCAVR2
libcid.lib(iostrini.obj) : error LNK2001: unresolved external symbol "void * __cdecl operator new(unsigned int,int,char const *,int)" (??2@YAPAXIHPBDH@Z)
libcid.lib(_ios.obj) : error LNK2001: unresolved external symbol "void * __cdecl operator new(unsigned int,int,char const *,int)" (??2@YAPAXIHPBDH@Z)
libcid.lib(streamb.obj) : error LNK2001: unresolved external symbol "void * __cdecl operator new(unsigned int,int,char const *,int)" (??2@YAPAXIHPBDH@Z)
Debug/Sensor2.exe : fatal error LNK1120: 2 unresolved externals
hmm the operator new one is familiar but I am not 100% sure...
It can be caused by using managed code, is that on by accident? I always turn it off, it seems to break the legal C++ language while adding odd, java or fake-java capability (C#).
If that is not it, its still very likely a project setting -- either you told it to use MFC and forgot the MFC headers, or something like that.
Also, try fooling with the default libs setting.
It sometimes appears to be caused by linking the compiler's libraries in the wrong order, which should not be possible but it is.
You may find this link useful:
the above errors have gone, now I have following errors:
sen3.obj : error LNK2001: unresolved external symbol __imp__MTCSInitSystem
Debug/Sensor3.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Looks like one of the libraries or headers or something is not seen, you just have to figure out where that entity is defined and get it into the project.
I persistently got the same errors using implicit DLL linking even when I changed the compiler settings so what I did was used explicit function call. The following is the code and I am not using any lib or header provided by the vendor.
/* get handle to dll */
HINSTANCE hGetProcIDDLL = LoadLibrary("D:\\Sensor8\\Debug\\MTCSApi.dll");
/* get pointer to the function in the dll*/
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"MTCSSetParameter");
Define the Function in the DLL for reuse. This is just prototyping the dll's function.
A mock of it. Use "stdcall" for maximum compatibility.
typedef int (__stdcall * pICFUNC)(int, int, int);
MTCSSetParameter = pICFUNC(lpfnGetProcessID);
/* The actual call to the function contained in the dll */
x = MTCSSetParameter(0, 10, 2);
/* Release the Dll */
/* The return val from the dll */
I am calling this function from main. I dont have any sort of errors, but the point is that I am unable to get pointer to the function. I have made a cout check on that by cout<<"Success function";. The DLL gets loaded as I could enter Dll success. Do you think now there is a problem with the DLL file since now its not able to access the functions either.
I am not sure from here. I just use library functions explicitly and avoid function pointers, callbacks, etc. I would say the DLL is just fine, though, if you can call it explicitly.
I cannot even remember the last time I felt like I needed a function pointer, I know you can do something similar with pointer to member in objects and I have yet to *need* to do that either. The callbacks are sometimes unavoidable though, in some libraries.
Its ugly, but if you cannot figure this out with a google, you can always make a function that calls the dll function explicitly and take a pointer to your wrapper function as a workaround, if you absolutely MUST have a function pointer for some reason. Try a google on "c++ dll function pointer" to see if there is some sort of special thing to do in the case of a dll (it would not be the first time a dll needed extra work for no obvious reason).
AFAIK libsig++ is a good library to help with member function pointers in c++. GTKmm uses it for the callbacks for button presses etc...
Also... often a function pointer can be avoided with a little OO magic...
Logic now can override the "callback" methods
public: // virtual voids make up the "callback" methods
virtual void testCallback(Hardware* hardware);
virtual void disconnectedCallback(Hardware* hardware)
Now, when you instantiate the class which can trigger callbacks (Hardware*), just pass into it the class pointer that derives from HardwareAdapter like...
class Logic : public HardwareAdapter //Uses Hardware Adapter as base class
void testCallback(Hardware* hardware);
void disconnectedCallback(Hardware* hardware)
Now Hardware (a wrapper class etc...) can now callback to the Logic class
hardware = new Hardware(this); // this = a Logic class
Make sure that if threads are used for polling and stuff, they are locked etc...
Last edited by kpedersen; 02-08-2010 at 02:01 PM.
Last Post: 09-08-2009, 05:09 PM
By Carson Saunders in forum .NET
Last Post: 06-20-2002, 01:12 PM
By Bruce Casson in forum VB Classic
Last Post: 03-21-2002, 12:51 PM
By Big John in forum VB Classic
Last Post: 02-22-2002, 04:25 PM
By PV in forum VB Classic
Last Post: 05-26-2001, 05:09 PM
Tags for this Thread
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL