compiling DLLs/calling external DLLs in VS Engineering

Source: Internet
Author: User

The question yesterday asked the great God, record it.

1. Compiling DLLs

Create a new project, Win32 the console application, select the DLL here. This will automatically generate DLL files in the debug directory after compilation.

In this way, the following directories are generated:

The 2nd and 3 in the header file are automatically generated, and 1 or 3 of the source files are automatically generated.

We just need to change:

Write in DllWithClass.h:

extern " C " _declspec (dllexport)// export float functionmultiply (floatfloat y);

Here the first line is for the DLL export, the second line is the declaration of the function function that you define

Write in DllWithClass.cpp:

extern " C " _declspec (dllexport) float Functionmultiply (floatfloat  y) {    return x*y;}

The first line corresponds to the. h inside, take care not to score number. Then the function body is defined.

Compiling this will generate a DLL file that implements a simple multiplication.

2. Calling the DLL

Build a Win32 console application, here to build the. exe

There are three things you need to do to call a DLL:

1> Code section, which only needs to change the UseDllWithClass.cpp file

Add the following code:

  #include  "   int     _tmain (int  argc, _tchar* argv[]) {  float  Functionresult = functionmultiply ( 1.2f , 5.0f     ); printf (  %f   "     return  0    

Where a reference to the header file of the. dll file is called, a relative path is used. Here's a little tip: because. Vcxproj the contents of this file represent the directory where the. exe file resides

.. \ means to return to the previous directory, so the order of this lookup is: Return to the previous directory from the. exe and open the \dllwithclass directory to find DllWithClass.h

For convenience, you can also directly place the. dll file in the directory where the. exe is located, so that you can directly include "DllWithClass.h"

2> Open Project Properties-linker-General

You can also use relative paths in the additional libraries directory where the. lib file is located:

If the. lib file is placed directly under the. exe file directory, it can be used without setting up (so the most convenient way is to put the. H/.lib/.dll all in the directory of the. exe file)

3> Open Project Properties-linker-Input

Add the. lib file name to the additional dependencies

By completing these settings, you can directly invoke the functions encapsulated in the. dll file.

compiling DLLs/calling external DLLs in VS Engineering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.