VC6.0 how to create and invoke a dynamic-link library Instance _c language

Source: Internet
Author: User
Younger brother in the company's duties, in the previous blog has been brief introduction. This blog mainly introduces the application of technology and not the principle of scrutiny. Because the company project worry, the result is important, and I am not focused on research, so, basically understand the principle, direct work, get the best demo.

As for the work of the company, we have to skip today, and of course it is not one or two words that can be clearly expressed. There will be a corresponding summary of work, please look forward to ...

Now, cut the crap, straight to the subject. Create a dynamic link library in--vc6.0.
As the client and backstage intermediary, in order to better adjust the relationship between the two parties, I wisely chose the WebService and dynamic link library. In with customer C + + make dynamic link library way, and with background Java, use WebService to communicate.
Now that you have a dynamic link library, you're sure to think of a static link library. Oh, this is relative. What's the difference between the two? That's for What chooses the dynamic link library?
Static link library: Is the Lib file is also packaged into the EXE file.
Dynamic Link library: The Lib file is not packaged into an EXE file, if used, it is loaded directly unload the corresponding DLL file.
Also, static-link libraries and dynamic-link libraries are not allowed in static-link libraries, and dynamic-link libraries are allowed to contain static-link libraries and dynamic-link libraries. Because the little brother encapsulates a DLL that invokes a client, it is possible to include another own dynamic-link library.
In view of this, the younger brother chose the dynamic link library.
There is also a point, dynamic link library, is divided into three kinds of situations. A Non-MFC DLL (that is, the console DLL), the second is a regular MFC DLL (which is divided into static DL and shared DLLs), and the third is the extended MFC DLL. and MFC's DLLs can be called by MFC programs or console programs.
Because the little brother encapsulates a DLL that needs to be called by an MFC program, you choose to use the MFC general DLL. and use the console program to do the testing.
First, select the MFC AppWizard (DLL) in the new project
Then in the header. h file, declare a function that is called by the outside world
Copy Code code as follows:

extern "C" _declspec (dllexport) char* Queryfunctionbyfid (char* funcid);

After the declaration, in the CPP file, implement this function.
Copy Code code as follows:

char* Queryfunctionbyfid (char* funcid)
{
..............................
Implementation of the specific operation}

compilation, Build generation. OK, generate the corresponding DLL under Debug, so the dynamic link Coussen. The program can then use the DLL file directly.
And then what. Copy the DLL file to the appropriate test DLL program (the program that invokes the DLL). The use of the following methods:
I test the DLL program, is the use of the console program, so simple and convenient, of course, the use of MFC programs can also.
For example: We create a new console program, and then in the main method, write as follows:
Copy Code code as follows:

Declaring function pointers Specify the number of function arguments, parameter types, and return value types
typedef char* (* Queryfunctionbyfid) (char*);
Load dynamic link library, return handle of DLL file
HINSTANCE Hdll=null;
Load the dynamic link library with the DLL name consistent with the name of the DLL you just generated.
Hdll=loadlibrary ("IProcessInstIn.dll");
Queryfunctionbyfid Saveprocess=null;
Load the corresponding function in the dynamic link library
Saveprocess= (Queryfunctionbyfid) GetProcAddress (hDLL, "Queryfunctionbyfid");
The real call to the corresponding function
cout<< "The result is:" <<saveprocess ("DD");
Releasing resources after the call is complete
FreeLibrary (hDLL);

The function of each sentence, the note has been clearly expressed. If you do not understand, you can contact me and communicate with each other.
In fact, a dynamic-link library, like the one we have in VS, is just a reference to the appropriate program and can be used directly by the DLL. And this is where we manually load, connect, and release the DLL. Using the dynamic link library in this way, we can easily use the function we want to call anywhere in our program.
As long as we adhere to the "load, loadlibrary--getprocaddress--freelibrary" principle can be. Very convenient to use. It frees the coupling between the two and can be easily loaded and released.
the procedure for calling a static link library is as follows
First: The Lib file and DLL file two files all placed under the client, call dynamic link library, only need DLL file.
Second: A new header file is required on the client, similar to the header file of the exported function in the DLL.
Third: In the client call DLL file (CPP file), add pre-instructions, that is, the Lib file explicitly loaded in.
such as: #pragma comment (lib, "IProcessInstIn.dll")
Four: Under the button event, call the appropriate method directly.
such as:cout<< "The result is:" <<queryfunctionbyfid (corresponding parameter);
In short, call the static link library, relative to, relatively simple, as long as the corresponding files loaded in, directly call the method can be. But the call dynamic link library is relatively flexible, when to use when to load, do not use the direct uninstall can.
Everything has advantages and disadvantages, we are to make full use of their advantages, in different situations and different needs under different means of use.
Just now when we were building the DLL, we used the DLL under the debug version, and now that we have the option of the debug version, we would think, according to our thinking, that there is another version. In the next blog, we'll look at the difference between the dynamic link library under the Debug version and the dynamic link library under another release version, and when and where to choose the version.

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.