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

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

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.

Edited 6 Years Ago by Ancient Dragon: n/a

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

#include <stdio.h>
#include<iostream.h>
#include<MTCSApi.h>



main()
{

 
    /* get handle to dll */ 
   HINSTANCE hGetProcIDDLL = LoadLibrary("D:\Program Files\Microsoft Visual Studio\MyProjects\sensor\Debug\MTCSApi.dll"); 

   /* get pointer to the function in the dll*/ 
   FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"MTCSInitSystem"); 

   /* 
      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)(char, int, int); 

   pICFUNC MTCSInitSystem; 
   MTCSInitSystem = pICFUNC(lpfnGetProcessID); 

   /* The actual call to the function contained in the dll */ 
   int intMyReturnVal = MTCSInitSystem("0", 0x152a, 0x8220); 

   /* Release the Dll */ 
   FreeLibrary(hGetProcIDDLL); 

   /* The return val from the dll */ 
    cout<<MyReturnVal; 
}

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.

Edited 6 Years Ago by Nick Evan: Added code-tags

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.

Edited 6 Years Ago by Ancient Dragon: n/a

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

#include<iostream.h>
#include<MTCSApi.h>

main()
{
/* The actual call to the function contained in the dll */ 
int ReturnVal = MTCSInitSystem("0", 0x152a, 0x8220); 
cout<<"The value"<<ReturnVal;
}

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

Edited 6 Years Ago by zaraf: n/a

Without seeing the contents of MTCSApi.h I have no clue what the problem is, other than the error message you posted

// Symbol USB_DLL_EXPORTS compiled. This symbol should not be defined in another location.
// diese DLL verwendet. 
//to export for C++ & C
#define USB_DLL_API __declspec(dllimport)
#ifndef _USB_API_H_
#define _USB_API_H_

#define USB 0
#define COM 1

#ifdef OBJECT
	#define USB_DLL_API
#else
#ifdef _USRDLL
	#ifdef USB_DLL_EXPORTS
	#define USB_DLL_API   __declspec(dllexport) __stdcall 
	#else
	#define USB_DLL_API  __declspec(dllimport) __stdcall 
	#endif
#endif
#endif

#ifdef EXE
	#ifdef USB_DLL_EXPORTS
	#define USB_DLL_API   __declspec(dllexport) __stdcall 
	#else
	#define USB_DLL_API  __declspec(dllimport) __stdcall 
	#endif
#endif 

#ifdef INDEX
#include "MTCSApi_idx.h"
#endif


#ifndef OBJECT
#if defined(__cplusplus)
extern "C" { 
#endif 
#endif 


 void USB_DLL_API   MTCSDllGetVersion(char* cBuf);
 int USB_DLL_API	MTCSInitSystem( char cTyp, int iVendorID, int iProductID ); 
 int USB_DLL_API    MTCSReadVersion( char* cBuf);
 int USB_DLL_API    MTCSReadMemory( unsigned char* cBuf, int iNum);
 int USB_DLL_API    MTCSWriteMemory( unsigned char* cBuf, int iNum);
 int USB_DLL_API    MTCSGetADCBL( unsigned short* usBuf, int iCounts);
 int USB_DLL_API    MTCSGetADCAVR( unsigned short* usBuf, int iCounts);
 int USB_DLL_API    MTCSGetADCBuf( unsigned short* usBuf);
 int USB_DLL_API    MTCSGetADC(  unsigned short* usBuf);
 int USB_DLL_API    MTCSSetSwitch(  int iCounts);
 int USB_DLL_API    MTCSSetEPoti(  int iCounts);
 int USB_DLL_API    MTCSGetRGB(  unsigned short* usBuf);
 int USB_DLL_API    MTCSStartRGB(  unsigned short* usBuf, int iTime, int iCycles);
 int USB_DLL_API    MTCSStopRGB(  unsigned short* usBuf);
 int USB_DLL_API   	MTCSCloseDevice(void);
 int USB_DLL_API    MTCSSetUpdateMode(void);


void USB_DLL_API MTCSGetSerienNummer( unsigned char idx, char *buffer);
int USB_DLL_API  MTCSGetADCAVR2( int iIndex, unsigned short* usBuf, int iCounts);
int USB_DLL_API  MTCSGetADCSummen( int iIndex, unsigned long* ulBuf, int iCounts);
int USB_DLL_API  MTCSSetParameter( int iIndex, int iVerst, int iTol);
int USB_DLL_API	 MTCSSearchAmplification( int iIndex, unsigned short* usBuf, int iLimit);
int USB_DLL_API  MTCSReadMemFromAdr( int  iIndex, unsigned char* cBuf, int iAdr, int iNum);
int USB_DLL_API  MTCSWriteMemToAdr(int iIndex, unsigned char* cBuf, int iAdr, int iNum);
int USB_DLL_API  MTCSSetShift( int iIndex, int iShift);
   
#ifndef OBJECT
#ifdef __cplusplus
 } 
#endif 
#endif 

/**************************************************************************************/

#endif	/*	_USB_API_H_ */

/**************************************************************************************/

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

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.

#include<iostream>
#include "MTCSApi.h"
using namespace std;

int main()
{
/* The actual call to the function contained in the dll */ 
int ReturnVal = MTCSInitSystem('0', 0x152a, 0x8220); 
cout<<"The value"<<ReturnVal;
}

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.

int CallMyDLL(void) 
{ 
   int x;
	/* get handle to dll */ 
   HINSTANCE hGetProcIDDLL = LoadLibrary("D:\\Sensor8\\Debug\\MTCSApi.dll"); 

   if (hGetProcIDDLL){
	cout<<"Success DLL";
   /* get pointer to the function in the dll*/ 
   FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"MTCSSetParameter"); 

   if (lpfnGetProcessID){

	   cout<<"Success function";
   /* 
      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); 

   pICFUNC MTCSSetParameter; 
   MTCSSetParameter = pICFUNC(lpfnGetProcessID); 

   /* The actual call to the function contained in the dll */ 
   x = MTCSSetParameter(0, 10, 2);
   }

   }
   /* Release the Dll */ 
   FreeLibrary(hGetProcIDDLL); 

   /* The return val from the dll */ 
    return x; 
}

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

This article has been dead for over six months. Start a new discussion instead.