Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a DLL - newbie

Status
Not open for further replies.

psandekian

Programmer
Oct 12, 2000
48
0
0
US
Hi,

I'm new to VC++ and I need to create a DLL using only ANSI C and the functions I have are in a .h file. Can anyone point me in the right direction?

Thanks.
Patty
 
Sure,

File/New/projects/win32 Dynamic Link Library

I'm assuming you don't need mfc support
 
Ok. So I created a new project, added my .h files and I created a main .c file like this
Code:
   #include <windows.h>
   #include <stdio.h>
   #include <math.h>
   #ifdef WIN32
      #include <conio.h> // for _kbhit
      #include <mmsystem.h> // for timeGetTime
   #endif
   #ifdef WIN16
      #include <io.h> // for _wyield
   #endif

	
/* NI-DAQ related includes ------------------------------------ */

   #include &quot;nidaq.h&quot;      /* for NI-DAQ function prototypes       */
   #include &quot;nidaqcns.h&quot;   /* for NI-DAQ constants                 */
   #include &quot;nidaqerr.h&quot;   /* for NI-DAQ error codes               */
When I build, I get no errors and it generates a dll in the debug directory. My question now is since I'm a VB person, I tried to check it in VB. When I try to call one of functions, it says:

Can't find DLL entry point AI_Mux_Config in e:\SCXI1126\nidll\nidll\Debug\nidll.dll

AI_Mux_Config is one of the functions. Will this library work for C programmers as is or will it give them the same error message? I do not know C enough to do the same quick check.

Thanks.
Patty
 
Did you declare the function in visual basic correctly?
private declare function AI_Mux_Config etc... ?
 
I believe I did. Here's the code from my module:
Code:
Declare Function AI_Mux_Config Lib &quot;e:\SCXI1126\nidll\nidll\Debug\nidll.dll&quot; (ByVal slot As Integer, ByVal numMuxBrds As Integer)

Thx.
Patty
 
So I tried to create something in VC++ to see if my dll was working and it doesn't seem to. Here's what I wrote:
Code:
typedef long (CALLBACK* AIMUXCONFIG)(short,short);
	
	HINSTANCE hDLL;               // Handle to DLL
	AIMUXCONFIG AI_Mux_Config;    // Function pointer

	short sSlot;
	short sNumMuxBrds;
	
	long lStatus;
	long lErrorCode;

		sSlot = 1;
	sNumMuxBrds = 2;
	lErrorCode = 3;

	hDLL = LoadLibrary(&quot;nidll.dll&quot;);

	AI_Mux_Config = (AIMUXCONFIG)GetProcAddress(hDLL,&quot;AI_Mux_Config&quot;);
   
	if (!AI_Mux_Config)
   {
      // handle the error
      FreeLibrary(hDLL);  
	  lErrorCode=1;
      
   }
   else
   {
      // call the function
      	lStatus = AI_Mux_Config(sSlot,sNumMuxBrds);
		lErrorCode=lStatus;
   }
The hDLL variable comes back with 0x00000000. I'm assuming that it did not find the dll or something is wrong with it. I knew it couldn't be that simple!

Anyway, I checked with a working dll by just inserting it's name in the hDLL line of code and it comes back with 0x21000000.

Is there something else I need to do to my DLL to make it work? Does it need a main function? Do I need to add the DEF file? I've tried this but I can't get it to compile because of errors.

Thanks.
Patty
 
I think what you might be missing is to declare your functions as being exported.

You need to do something like the following (which I borrow from what I was told in a VC++ forum):

* Add this code before the class declaration (in the header file):

#ifdef IN_DLL
#define WHICHWAY __declspec (dllexport)
#else
#define WHICHWAY __declspec (dllimport)
#endif

* Then you would add this variable to your class as follows:

class WHICHWAY ClassNameHere
{
};

* Now inside your source file (.cpp) you put the following at the top of file:

#define IN_DLL

He was showing me how to export a class, but I must say I never tried it like that... Instead I export functions using the same method.

You might need to adapt the code above, since I don`t know if it is 100% C (could be some C++ there, aside from the &quot;class&quot;). Anyway, it`s a good staring point.

Vincent
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top