开发者

How Can I call a C++ Win32 DLL from another C++ win32 Console Application

What is my main concern is , I am able to write a C++ dll using VC++ . Now the dll is present in the Debug folder.

How can I use my DLL in other C++ Console Application. How to add reference or link the DLL to the application.

Another point, While creating a DLL , The VC++ wizard gives me thre op开发者_如何学编程tion:

  1. An Empty DLL project
  2. A Simple DLL project
  3. A DLL that Exports some Symbol

Now as per article from CP I have used the 3rd option. (Unable to follow as the dll was used by an MFC app, Some how little advanced at this point of time)

Do I need to always choose the third option? What are the the other two options mean?


Not completely sure what you questions are but:

It doesn't really matter which option you use it is just a matter of what the wizard does for you; if you use the third option then the wizard creates a bit in your header file that looks like this:

#ifdef TEST_EXPORTS
#define TEST_API __declspec(dllexport)
#else
#define TEST_API __declspec(dllimport)
#endif

The way this works is that in the DLL project TEST_EXPORTS is defined in the compiler options so TEST_API evaluates to dllexport thus telling the linker to export these symbols. If you include this header in another project it defines TEST_API as dllimport which tells the compiler to link to it at run-time in the DLL. This #define method of exporting symbols is widely used and easy to read.

In order to call a function/class inside the DLL you need to export the symbols one of two ways: a) using the __declspec(dllexport) [this seems the more convenient option in almost all cases ]OR b) using a .DEF file in your project

Also wanted to mention that you need to include either the DLL project in your solution for the .exe file OR the .lib generated by the DLL compile.


You can use "A DLL that Exports some Symbol" to learn how Dll project should be built. Once you understand this, use "A Simple DLL project". You can prefer to start always with "A DLL that Exports some Symbol", and change the code generated by Wizard, replacing sample exported class/function/symbol with your own code.

To reference .Dll project from a client project, add .lib file to the client project linker dependencies: Project - Properties - Linker - Input - Additional Dependencies. To ensure that .lib file can be found by linker, add directory where .lib file is placed, to the linker directories list. There are two places this can be done: locally in the client project (Project - Properties - Linker - General - Additional library directories) and globally, for all VC++ projects (Tools - Options - VC++ Directories - Libraries).

The last thing is to ensure that DLL can be loaded by client .exe at runtime. Dll must be in the current directory, executable directory, Windows, system directory, or available through PATH variable.


The DLL can be imported by specifying it as a dependency in project settings of the Console application within Visual Studio as described by Alex Farber. You have to make sure that the application is able to find the DLL by placing the DLL at any location specified the PATH variable. You can also load the DLL programmatically within you application using LoadLibrary method (see documentation here http://msdn.microsoft.com/en-us/library/ms684175(VS.85).aspx) and call a method exported within the DLL using function GetProcAddress ( refer http://msdn.microsoft.com/en-us/library/ms683212(VS.85).aspx ).


Go through the following steps to set up two projects. http://msdn.microsoft.com/en-us/library/ms235636.aspx

In those instructions, the new project is added to the same solution that contains the DLL. This might not be the case in most of the situations. If you have two separate projects which you want to handle, slightly alter the above procedure as follows.

  1. In the DLL project, make sure you have configured to create a DLL file. To do that, click on the project in the solutions explorer and go to properties. Under Configuration Properties >> General, change the Configuration Type to "Dynamic Library (.dll)". Now, compile the dynamic link library by choosing Build, Build Solution on the menu bar.

  2. Create the other project and the files as mentioned in the link. To use the dll in the app, you must reference it. To do this, add the lib file created from the dynamic library. The lib file is created in the same folder as the dll. If Visual studio was used in the debug mode, it will be in Project>>Folder>>Debug. If Release mode was used, Project Folder>>Release. To use the lib file in the app, Go to Project >> Properties >> Linker >> Input >> Additional Dependencies and add the name of the lib file to the list ("AFR24x7.lib" in my case).

  3. To ensure that .lib file can be found by linker, add directory where .lib file is placed, to the linker directories list. There are two places this can be done: locally in the client project (Project>>Properties>>Linker>>General>>Additional library directories) and globally, for all VC++ projects (Tools>>Options>>VC++ Directories>>Libraries).

  4. Add the include file as mentioned in the link.

  5. Copy the created DLL file and paste it in the app's release and debug folders (If you are using both of them).

  6. Complete the rest of the steps other than setting the dependencies given in the link.

Hope this will help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜