Thread: Creating and using a Dll in Dev-C++

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    Question Creating and using a Dll in Dev-C++?

    Hi all,

    This might be a very easy question and a commen topic and I know there is a lot of info on it but I could not find a single tutorial or artical for creating and using a dll in Dev-C++, I am using the latest version 5.0 beta 7 (4.9.7.0). I know there is a project template type dll to select and most of the code is ready but I can't understand it and there is nothing written in the help documents. And the worst part is there is nothing on how to use the compiled dll . The reason why I choose Dev-C++ for my dll is becouse I love it and it is so easy to use, except for this part maybe , and I have already written the program that the dll is for in Dev-C++. I heard on the site www.functionx.com/visualc/applications/staticdll.htm from FunctionX that dlls compiled in Visual C++ can't simply be "plugged" in programs written in Dev-C++ and Borland C++, is that true? And how do I use and create a dll in Dev-C++?
    Last edited by Aidman; 01-17-2003 at 03:13 PM.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  2. #2
    Registered User alex6852's Avatar
    Join Date
    Sep 2001
    Posts
    43
    I don't know anything about your specific compiler but I think I can help you with standart mothood:
    First the dll. Call it system.dll and write this:
    Code:
    #include <windows.h>
    #include <iostream.h>
    #include <stdio.h>
    
    extern "C" __declspec(dllexport) void Hello();
    extern "C" __declspec(dllexport) void NumberList(int);
    
    char name[70];
    
    extern "C" __declspec(dllexport) void Hello()
    {
      cout << "\nHello from system.dll" << endl;
      cout << endl << endl;
    }
    
    extern "C" __declspec(dllexport) void NumberList(int a)
    {
    	GetModuleFileName(NULL,(LPTSTR)name,70);
    	cout << "Called from: " << name << endl << endl;
    
    	int i;
    	for (i=a; i<a+10; i++)
    	{
    		cout << i << "  ";
    	}
    
    	cout << endl << endl;
    }
    This dll will exprot two functions.

    Now the main console app:
    Code:
      #include <windows.h>
      #include <iostream.h>
      #include <stdio.h>
      #include <conio.h>
    
      typedef void (*pfunc1)();
      typedef void (*pfunc2)(int);
    
      pfunc1 One;
      pfunc2 Two; 
    
    int main()
    	{	
    HINSTANCE hLib = LoadLibrary("system.dll");
    
      if(hLib==NULL)
    	{
    	cout << "Error! Can't open dll!";
    	return 1;
    	}
    
    	char dllpath[70];
    
    	GetModuleFileName((HMODULE)hLib,(LPTSTR)dllpath,70);
    
    	cout << "Dll loaded:" << dllpath << endl;
    
    	One = (pfunc1)GetProcAddress((HMODULE)hLib, "Hello");
    	Two = (pfunc2)GetProcAddress((HMODULE)hLib, "NumberList");
    
    	if((One==NULL) || (Two==NULL)) 
    	{
    	cout << "Critical error! Can't load functions !" << endl;
    	FreeLibrary((HMODULE)hLib);
    	return 1;
    	}
    
    	One();
    	Two(50);
    
    	FreeLibrary((HMODULE)hLib);
    	getch();
    
    	return 0;
          }
    It should work on any compiler. Ask wich line you don't understand.
    C++ rulez!!!

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    Thanks for the help but it seems like the new Dev-C++ doesn't include the iostream header and according to the Compiler there must be a DllMain procedure for the Dll and a WinMain for the App, becouse I need other Windows API functions.
    Last edited by Aidman; 01-18-2003 at 08:09 AM.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

Popular pages Recent additions subscribe to a feed