win7+vs2010 making and calling DLLs

Source: Internet
Author: User

*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************


Well hum ~ to help the teacher before the module, do not have to do = =.

For nearly 10 days, the results ran for 13 minutes.

Others, second level, even the speed of the millisecond level ....

Hey....

Kneel ing,2000+ X 2000+ matrix, multiply, multiply, transpose, reverse, a series of operations,

Furthermore, the multiplication must be carried out at least 40+ times, and the inverse is 40+ times,

This is how the second level results in = =:

Or, they are not calculated by the matrix at all.

Oh, I don't think so.


Teacher, is made in C #, then I will make this function module DLL, give him, tell him parameters and return.

Words, I wouldn't do dll, to show shame ing

As a result, a variety of search and test, finally understand.


By the right, my environment is WIN7 + VS2010


1. Generating DLL files

Well, first, open VS2010 to create a new WIN32 project, name Max_2 (of course, what you want to call it):



Pop-Up dialog box, click Next

Then select the DLL and click on the Empty project and select Finish:



Then, right-click on the project name, and select Add-new item:



Then, look for the. cpp file, enter the file name, and create:



Then, fill in your code, here I will compare two number size for example:

Fill in the. cpp: (note __declspec and __cdecl are all two _)

extern "C" __declspec (dllexport) int __cdecl max (int a, int b) {Returna > B? a:b;}

--Here the extern "C" is to prevent the following may occur: Can load DLL file, but cannot find the function of the case.

extern "C" means: Tell the C + + compiler This function, C-style compilation. This mainly depends on how you call, with C-style calls or C + + style calls. (You can also see this http://blog.csdn.net/zhongjling/article/details/8088664.)

__declspec (dllexport) is the necessary thing to generate the DLL, and if not, you can replace it with a new. def file.

The--__cdecl effect is to specify that the function call rule in the VC is __cdecl. Because of the VC environment, the default call rule is __cdecl, so you can also not write. Of course, in addition to the __cdecl there are conventions called WIN32 API functions __stdcall, the performance requirements are very high in the case of __fastcall.

These details are not described in detail and can be seen in http://blog.csdn.net/kesalin/article/details/2447368

And then the size of the function, type, name, the content will not need to say it? (Focus on making DLLs)


Here, I choose to make the DLL of release,

Choose Build-and-Configure manager:



Then, in the active solution configuration, select Release:



Click Close, and then build the solution.

Then, under the generated file root directory, locate the release folder,

You can see the DLL file when you open it:



Ok,dll file generated successfully!




2. Calling the DLL

Next, it's how to downgrade the DLL in VS.

Create a new WIN32 console application (deselect Build Solution):



Then, in the pop-up window, click Next, then select Console application, empty project:



As with the build, create a new. cpp file,

Then fill in the inside:

#include <Windows.h> #include <stdio.h>typedef int (__cdecl *findmax) (int a, int b); int main () {Findmax FM Ax Hmodule hlib;hlib = LoadLibrary (TEXT ("Max_2.dll")), Fmax = (Findmax) GetProcAddress (hlib, "Max"), if (hlib = = NULL) {pri NTF ("DLL get failed \ n");} if (Fmax = = NULL) {printf ("function get failed \ n");} int A,b;while (scanf ("%d%d", &a,&b)!=eof) {if (!a &&!b) break;printf ("%d and%d, larger:%d\n", A,b,fmax (A, B ));} System ("pause"); return 0;}

Of course, put your DLL in the same directory as the SLN.

(What ' s the sln?) In short, you open the VS2010, select Open Project, and then you want to select the file. )


To build a solution, run:



Can Yo ~

There are some differences under the. cpp file:

For example, for hmodule, this can also be hinstance and HWND:

You can see http://blog.csdn.net/zhchongyao/article/details/6216659.


Of course, you can also call a DLL in a DLL:

Method (The function compares three number sizes):

#include <windows.h>typedef int (__cdecl *findmax) (int a, int b), extern "C" __declspec (dllexport) int __cdecl th _max (int A, int b, int c) {Findmax Fmax; HInstance Hlib;hlib = LoadLibrary (TEXT ("Max_2.dll")), Fmax = (Findmax) GetProcAddress (hlib, "Max"), Return Fmax (Fmax (A , b), c);}

However, to invoke this DLL, you must also place the DLL and the Max_2.dll in the same directory as the SLN.

Indispensable Yo ~


Well, for DLL making and use that's it ~

See u~




*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************

win7+vs2010 making and calling DLLs

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.