Vc dll basic tutorial

Source: Internet
Author: User

Use Visual sudio 6.0 to create a project. Select Win32 dynamic-Link Library as the project type. Set all other options to the default one.
Create a CPP file with the following code:

Int add (int a, int B ){
Return a + B;
}

If the project type is Win32 console application, a DEBUG directory is generated after the link is compiled, and an EXE file is in it.
Here, our project type is Win32 dynamic-Link Library. After the link is compiled, we expect to generate a DEBUG directory with a DLL file
This is exactly the case.
We can use the depends tool to open the DLL file and view the function it exports.
The depends tool is in the Tools menu. It is actually a file under D:/program files/Microsoft Visual Studio/common/tools.
We found that this dll has not exported anything
This is because we have not explained what we want to export. the function in that CPP is not exported by default. because they may only be called by the function we want to export ".
To export a function, we need to add _ declspec (dllexport) and the Code becomes:

Int _ declspec (dllexport) add (int a, int B ){
Return a + B;
}

Link again
Check the DLL file and find one? Add @ yahhh @ Z's function. It seems strange, but I finally saw something.
Now let's test this DLL.
Create a project and select Win32 console application as the type.
Create a CPP file with the following code:

# Include <iostream. h>
# Include <Windows. h>
Void main (){
Typedef int (* ADD) (int, int); // function pointer type
HINSTANCE Hint =: LoadLibrary ("DLL. dll"); // load the dll we just generated
ADD add = (ADD) GetProcAddress (Hint, "add"); // obtain the add method for dll Export
Cout <add (3, 4) <endl;
}

Loadlibrary is a function declared in windows. h.
Compilation link, no problem
An error occurred while running!
How does the program know where to find our DLL?
It searches in the following order: the directory where the current executable module is located, the current directory, the Windows System directory, and the Windows directory. The GetWindowsDirectory function retrieves the PATH of this Directory, which is listed in the PATH environment variable.
So we need to copy our dll to the Debug directory of the test project and then run it.
An error occurred!
Analysis. What we saw just now is? Add @ YAHHH @ Z function. Why?
Change the code:

# Include <iostream. h>
# Include <Windows. h>
Void main (){
Typedef int (* ADD) (int, int); // function pointer type
HINSTANCE Hint =: LoadLibrary ("DLL. dll"); // load the dll we just generated
ADD add = (ADD) GetProcAddress (Hint ,"? Add @ YAHHH @ Z "); // obtain the dll export add method.
Cout <add (3, 4) <endl;
}

Compile the link and run it!
So how can we correctly export the name of our function?
Add extern "C" to the code of the project that generates the dll, and change it:

Extern "C" int _ declspec (dllexport) add (int a, int B )...{
Return a + B;
}

After compiling the link, check the dll file and you can see that the exported function has changed to add.
The following code works properly.

# Include <iostream. h>
# Include <Windows. h>
Void main ()...{
Typedef int (* ADD) (int, int); // function pointer type
HINSTANCE Hint =: LoadLibrary ("DLL. dll"); // load the dll we just generated
ADD add = (ADD) GetProcAddress (Hint, "add"); // obtain the add method for dll Export
Cout <add (3, 4) <endl;
}

In addition to specifying the function to be exported using _ declspec (dllexport) and using extern "C" to correct the name, we can also use a. def file to achieve the above purpose.
Create a File in the dll project, select Text File as the type, and add the suffix. def to the name.
The content is as follows:
LIBRARY
EXPORTS
Add
The remaining steps are the same as before.
You can use the def file to change the name of the exported function, for example
LIBRARY
EXPORTS
Myadd = add
The exported function is called myadd instead of add.
You can also specify an serial number for the function.
For example:
LIBRARY
EXPORTS
Myadd = add @ 4
A serial number is specified for myadd.
In the test project, we can obtain our function according to the serial number:

# Include <iostream. h>
# Include <windows. h>
Void main (){
Typedef int (* Add) (INT, INT );
Hinstance =: loadlibrary ("DLL. dll ");
Add add = (ADD) getprocaddress (hinstance, makeintresource (4); // obtain the function by sequence number
Cout <add (3, 4) <Endl;
Add = (ADD) getprocaddress (hinstance, "myadd"); // The name specified in the def File
Cout <add (3, 4) <Endl;
Freelibrary (hinstance); // release the resources occupied by the loaded DLL files
}

The above section describes how to dynamically load DLL during runtime. The following section describes how to dynamically load DLL during startup.

The project that generates the DLL does not need to be changed, or the above (name is myadd, number is 4)
The test code is changed:
// Copy the DLL. Lib file to the project directory.

# Include <iostream. h>
# Pragma comment (Lib, "DLL. lib ")
Extern int myadd (INT, INT); // if this sentence is not added, only this sentence is added (or DLL. Lib is added in the Project Settings). The link is incorrect.
Void main ()
{
Cout <myadd (3, 4) <Endl;
}

This method calls the DLL. When linking, it will include the symbol to be referenced in our EXE and load all the required DLL when starting the program. (I said it was a static link)
# Pragma comment (Lib, "DLL. lib ") specifies the DLL used, where DLL. LIB can be found in debug. we also need to import the DLL. copy lib to the test project directory (not the DEBUG directory ). you can also add it in the project properties. the method is project -- settings -- link, and the DLL is added at the end of Object/libraries modules. lib
Extern int add (INT, INT); specifies that our add is an external function, rather than

At last, I want to copy the copied file to the correct place.
If the DLL file you generated is inconsistent with what I said, select build-rebuild all.
 

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.