943,148 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 612
  • C++ RSS
Feb 2nd, 2010
0

accessing values from sensor API,dll etc

Expand Post »
Dear All,

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.

regards,
Faraz
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zaraf is offline Offline
6 posts
since Dec 2009
Feb 2nd, 2010
0
Re: accessing values from sensor API,dll etc
I though you said the company already gave you the DLL???
Quote ...
the company has provided me with a
header file and lib file and ofcourse the dll file
So why are you trying to recompile it? Just use the DLL that they gave you. Write your program, include their header file, and link with their library. Put their DLL in the same directory as the program you wrote and you have it all done. Nothing different about that then there is using DLLs provided by the compiler.
Last edited by Ancient Dragon; Feb 2nd, 2010 at 12:52 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2281
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,938 posts
since Aug 2005
Feb 2nd, 2010
0

i am trying my best

I though you said the company already gave you the DLL???

So why are you trying to recompile it? Just use the DLL that they gave you. Write your program, include their header file, and link with their library. Put their DLL in the same directory as the program you wrote and you have it all done. Nothing different about that then there is using DLLs provided by the compiler.
yes dragon you are right, they have given me the dll file and the following is the code i wrote after copying some from the net

C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include<iostream.h>
  3. #include<MTCSApi.h>
  4.  
  5.  
  6.  
  7. main()
  8. {
  9.  
  10.  
  11. /* get handle to dll */
  12. HINSTANCE hGetProcIDDLL = LoadLibrary("D:\Program Files\Microsoft Visual Studio\MyProjects\sensor\Debug\MTCSApi.dll");
  13.  
  14. /* get pointer to the function in the dll*/
  15. FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"MTCSInitSystem");
  16.  
  17. /*
  18.   Define the Function in the DLL for reuse. This is just prototyping the dll's function.
  19.   A mock of it. Use "stdcall" for maximum compatibility.
  20.   */
  21. typedef int (__stdcall * pICFUNC)(char, int, int);
  22.  
  23. pICFUNC MTCSInitSystem;
  24. MTCSInitSystem = pICFUNC(lpfnGetProcessID);
  25.  
  26. /* The actual call to the function contained in the dll */
  27. int intMyReturnVal = MTCSInitSystem("0", 0x152a, 0x8220);
  28.  
  29. /* Release the Dll */
  30. FreeLibrary(hGetProcIDDLL);
  31.  
  32. /* The return val from the dll */
  33. cout<<MyReturnVal;
  34. }

could you please give a look at the code, and I dint get how to link with "their library" part.. sorry I know I am a newbie in dll.
Last edited by Nick Evan; Feb 2nd, 2010 at 1:40 pm. Reason: Added code-tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zaraf is offline Offline
6 posts
since Dec 2009
Feb 2nd, 2010
0
Re: accessing values from sensor API,dll etc
If you are using Microsoft VC++ 2008 Express (or some other edition of that IDE) then you have a couple choices. If you have used 3d party libraries in your programs before, then this will be no different than that.

1) use the pragma #pragma message(lib , "somefile.lib") near the top of your program, but after all the include files.

2) Add the name of the library to Project --> Properties --> Configuration Properties --> Linker --> Input..

3) If you are using LoadLibrary() then you don't have to do either of the above two opeions.
Last edited by Ancient Dragon; Feb 2nd, 2010 at 1:24 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2281
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,938 posts
since Aug 2005
Feb 4th, 2010
0
Re: accessing values from sensor API,dll etc
Dear dragon,

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
  1. #include<iostream.h>
  2. #include<MTCSApi.h>
  3.  
  4. main()
  5. {
  6. /* The actual call to the function contained in the dll */
  7. int ReturnVal = MTCSInitSystem("0", 0x152a, 0x8220);
  8. cout<<"The value"<<ReturnVal;
  9. }



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.
regards
Last edited by zaraf; Feb 4th, 2010 at 1:33 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zaraf is offline Offline
6 posts
since Dec 2009
Feb 5th, 2010
0
Re: accessing values from sensor API,dll etc
Without seeing the contents of MTCSApi.h I have no clue what the problem is, other than the error message you posted.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2281
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,938 posts
since Aug 2005
Feb 5th, 2010
0
Re: accessing values from sensor API,dll etc
[QUOTE=Ancient Dragon;1121605]Without seeing the contents of MTCSApi.h I have no clue what the problem is, other than the error message you posted

  1.  
  2. // Symbol USB_DLL_EXPORTS compiled. This symbol should not be defined in another location.
  3. // diese DLL verwendet.
  4. //to export for C++ & C
  5. #define USB_DLL_API __declspec(dllimport)
  6. #ifndef _USB_API_H_
  7. #define _USB_API_H_
  8.  
  9. #define USB 0
  10. #define COM 1
  11.  
  12. #ifdef OBJECT
  13. #define USB_DLL_API
  14. #else
  15. #ifdef _USRDLL
  16. #ifdef USB_DLL_EXPORTS
  17. #define USB_DLL_API __declspec(dllexport) __stdcall
  18. #else
  19. #define USB_DLL_API __declspec(dllimport) __stdcall
  20. #endif
  21. #endif
  22. #endif
  23.  
  24. #ifdef EXE
  25. #ifdef USB_DLL_EXPORTS
  26. #define USB_DLL_API __declspec(dllexport) __stdcall
  27. #else
  28. #define USB_DLL_API __declspec(dllimport) __stdcall
  29. #endif
  30. #endif
  31.  
  32. #ifdef INDEX
  33. #include "MTCSApi_idx.h"
  34. #endif
  35.  
  36.  
  37. #ifndef OBJECT
  38. #if defined(__cplusplus)
  39. extern "C" {
  40. #endif
  41. #endif
  42.  
  43.  
  44. void USB_DLL_API MTCSDllGetVersion(char* cBuf);
  45. int USB_DLL_API MTCSInitSystem( char cTyp, int iVendorID, int iProductID );
  46. int USB_DLL_API MTCSReadVersion( char* cBuf);
  47. int USB_DLL_API MTCSReadMemory( unsigned char* cBuf, int iNum);
  48. int USB_DLL_API MTCSWriteMemory( unsigned char* cBuf, int iNum);
  49. int USB_DLL_API MTCSGetADCBL( unsigned short* usBuf, int iCounts);
  50. int USB_DLL_API MTCSGetADCAVR( unsigned short* usBuf, int iCounts);
  51. int USB_DLL_API MTCSGetADCBuf( unsigned short* usBuf);
  52. int USB_DLL_API MTCSGetADC( unsigned short* usBuf);
  53. int USB_DLL_API MTCSSetSwitch( int iCounts);
  54. int USB_DLL_API MTCSSetEPoti( int iCounts);
  55. int USB_DLL_API MTCSGetRGB( unsigned short* usBuf);
  56. int USB_DLL_API MTCSStartRGB( unsigned short* usBuf, int iTime, int iCycles);
  57. int USB_DLL_API MTCSStopRGB( unsigned short* usBuf);
  58. int USB_DLL_API MTCSCloseDevice(void);
  59. int USB_DLL_API MTCSSetUpdateMode(void);
  60.  
  61.  
  62. void USB_DLL_API MTCSGetSerienNummer( unsigned char idx, char *buffer);
  63. int USB_DLL_API MTCSGetADCAVR2( int iIndex, unsigned short* usBuf, int iCounts);
  64. int USB_DLL_API MTCSGetADCSummen( int iIndex, unsigned long* ulBuf, int iCounts);
  65. int USB_DLL_API MTCSSetParameter( int iIndex, int iVerst, int iTol);
  66. int USB_DLL_API MTCSSearchAmplification( int iIndex, unsigned short* usBuf, int iLimit);
  67. int USB_DLL_API MTCSReadMemFromAdr( int iIndex, unsigned char* cBuf, int iAdr, int iNum);
  68. int USB_DLL_API MTCSWriteMemToAdr(int iIndex, unsigned char* cBuf, int iAdr, int iNum);
  69. int USB_DLL_API MTCSSetShift( int iIndex, int iShift);
  70.  
  71. #ifndef OBJECT
  72. #ifdef __cplusplus
  73. }
  74. #endif
  75. #endif
  76.  
  77. /**************************************************************************************/
  78.  
  79. #endif /* _USB_API_H_ */
  80.  
  81. /**************************************************************************************/

dear Dragon,

the above is the header file, i have resolved the earlier errors, they were due to the first line #define USB_DLL_API __declspec(dllimport) not being included in it.
now after this I compiled, but now getting linking errors such as following. i have linked to the headers and .lib files perfectly. do you have a clue..


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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zaraf is offline Offline
6 posts
since Dec 2009
Feb 5th, 2010
0
Re: accessing values from sensor API,dll etc
I compiled it with VC++ 2008 Express and only got an error about the first parameter to MTCSInitSystem().

Note that iostream.h is not supported any more (deprecated) by modern compilers. Only ancient compilers such as Turbo C use that header file.

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include "MTCSApi.h"
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. /* The actual call to the function contained in the dll */
  8. int ReturnVal = MTCSInitSystem('0', 0x152a, 0x8220);
  9. cout<<"The value"<<ReturnVal;
  10. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2281
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,938 posts
since Aug 2005
Feb 7th, 2010
0
Re: accessing values from sensor API,dll etc
Dear dragon,

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.
  1. int CallMyDLL(void)
  2. {
  3. int x;
  4. /* get handle to dll */
  5. HINSTANCE hGetProcIDDLL = LoadLibrary("D:\\Sensor8\\Debug\\MTCSApi.dll");
  6.  
  7. if (hGetProcIDDLL){
  8. cout<<"Success DLL";
  9. /* get pointer to the function in the dll*/
  10. FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"MTCSSetParameter");
  11.  
  12. if (lpfnGetProcessID){
  13.  
  14. cout<<"Success function";
  15. /*
  16.   Define the Function in the DLL for reuse. This is just prototyping the dll's function.
  17.   A mock of it. Use "stdcall" for maximum compatibility.
  18.   */
  19. typedef int (__stdcall * pICFUNC)(int, int, int);
  20.  
  21. pICFUNC MTCSSetParameter;
  22. MTCSSetParameter = pICFUNC(lpfnGetProcessID);
  23.  
  24. /* The actual call to the function contained in the dll */
  25. x = MTCSSetParameter(0, 10, 2);
  26. }
  27.  
  28. }
  29. /* Release the Dll */
  30. FreeLibrary(hGetProcIDDLL);
  31.  
  32. /* The return val from the dll */
  33. return x;
  34. }
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.

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zaraf is offline Offline
6 posts
since Dec 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: error C2447: '{' : missing
Next Thread in C++ Forum Timeline: Need Help with Recursive Function that Deals with Arrays of Strings





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC