Author: Zhou zhipeng
QQ: 39188043
You can reprint this article at will, but please indicate the source and author
If you want to quickly view the method for calling the function in the. dll file, you can start with "start.
As an example, the first is a piece of nonsense:
It took me two days to finally figure out how to call the functions in the DLL file. I am so excited that it is easy to say. First of all, I would like to thank Shi Lei and whu_yang, who I met on Baidu, thank you for your help in solving this problem.
I should declare that I have never involved programming in windows before, so I have to write an article to understand such things. Do not despise it.
In fact, the main reason for writing this article is that I think it is called. the function in the DLL file should be very simple and common, but unfortunately, I just checked such a thing, and it can be said that it was a lot of effort, it took up to two days to "Roam" on the Internet. Many people wrote similar things, but unfortunately, maybe they thought it was too simple and didn't want to be too wordy. At the end of the day, I couldn't find anything to understand. Do beginners like me want to find out something like this and learn it again ?!
You must:
As long as you understand the basic C language, you can understand this article.
This article mainly writes to programmers or engineers who seldom write programs in windows like me. Therefore, you do not need to understand windows, vc6, and basic C.
Total:
I read a lot of articles, including the documentation on msdn, and talked about a lot of DLL things. After a while, I have all connected APIs, COM, and MFC. Dizzy ~ Really dizzy, I just want to know how to call the function in the. dll file, is it really so difficult? Here, I don't want to copy and paste the concept of "implicit link" and "explicit link". I just want to explain it in the following two cases:
Start
1. Existing. lib and. H files
(1) In order to facilitate the readers to verify and learn in person, we will first build a. dll file with. lib and. h.
1. open vc6, file => New, select "Win32 dynamic-Link Library" on the projects tab, enter "dll_1" in the project name, and enter "D: /dll_1 ", and then click OK.
2. Select "a DLL that exports some symbols" and click Finish.
3. Click OK to confirm the project you created.
4. After the project is created, click F7. If you do not perform this step first, an error will be reported during compilation.
5.
Click the bottom FileView tab in the figure and double-click to open the dll_1.cpp file in the source Files folder.
6. let's take a look at these lines of code. In fact, this is a few examples automatically generated by vc6, including a variable, a function, and a class. They are the final generation. DLL files can be called.
// This is an example of an exported variable
Dll_prop API int ndll_1 = 0;
// This is an example of an exported function.
Dll_prop API int fndll_1 (void)
{
Return 42;
}
// This is the constructor of a class that has been exported.
// See dll_1.h for the class definition
Cdll_1: cdll_1 ()
{
Return;
}
We can delete these lines of examples and replace them with our own code:
Dll_history API int ftest ()
{
Return 253;
}
7. Double-click the dll_1.h file in the header files folder to view the code one by one:
// This class is exported from the dll_1.dll
Class dll_1_api cdll_1 {
Public:
Cdll_1 (void );
// Todo: add your methods here.
};
Extern dll_struct API int ndll_1;
Dll_prop API int fndll_1 (void );
These are actually the lives of the three examples mentioned above. Of course we should replace them with our code:
Dll_prop API int ftest (void );
8. Okay. Next let's directly F7 and then look at your "D:/dll_1/debug" directory. How can this problem be solved? Did you see our lovely dll_1.dll and dll_1.lib files? The dll_1.h file is in the "D:/dll_1" directory.
(2) Next, we will call the int ftest () function in the. dll file.
1. First, we will close the project we just completed. File => close workspace, and then click "Yes (y)" to save what we just did.
2. create a new console project file => New. On the projects tab, select "Win32 console application", enter exe_1 in project name, and enter D:/exe_1 in location, click OK. Then, confirm to select "An Empty Project" and click Finish. Click OK.
3. Create a New. cpp file: file => New. On the files tab, select "C ++ source file", Enter main in file, and click OK.
4. Enter the following code in the main. cpp file:
# Include "iostream. H"
# Include "dll_1.h"
Void main ()
{
Int;
A = ftest (); // This is the function in the DLL file we call.
Cout <A <Endl;
}
5. Before compiling the link, we need to do some work:
A. Copy the aforementioned dll_1.h, dll_1.dll, and dll_1.lib to the "D:/exe_1" directory.
B. Project => Settings...
At the end of "Object/library modules:" On the link tab, add "dll_1.lib". Note that spaces are separated from the preceding Lib. Click OK.
6. then compile, link, and run?
Haha ~ What does "253" mean?
Of course, the same is true for calling DLL variables and classes.
2. Only. DLL files are available.
In fact, the problem I encountered this time belongs to this type. I am so bitter to check it. Now I have finally solved it. :)
Of course, when the. dll file is provided, a description document should also be provided, which should have provided the functions and prototypes of the functions you can call. If he does not provide a function prototype, he still needs some means to obtain it. This article will not discuss how to obtain the function prototype.
Here, we will first provide the DLL file I got and Its callable function prototype:
Functions that can be called in kingvewcliend. dll (this is the DLL file name) include:
1. Int readitemno ();
Return Value:
Return the total number of projects listed in Kingview OPC
2. Int startcliend (char * node );
Entry parameters:
Node is the machine node name.
Return Value:
Returns the connection creation error code.
========================================================== =
(1) Next, we will call the int readitemno (); Function Method in the kingvewcliend. dll file:
As mentioned above (1 and 2) 2 .~ 3.) Similarly, first create the console project and CPP file, and then enter the following code:
# Include "stdio. H"
# Include "windows. H"
Void main ()
{
Int;
Hinstance dll123;
Dll123 = loadlibrary ("kingvewcliend. dll ");
If (null = dll123)
Printf ("Cant't find DLL ");
Farproc msg1;
Msg1 = getprocaddress (dll123, "readitemno ");
If (null = msg1)
Printf ("Cant't frind function ");
Else
A = msg1 ();
Printf ("% d/N", );
}
Before compiling, linking, and running, you must copy the kingvewcliend. dll file to the project folder.
(2) The next step is to call int startcliend (char * node) in the kingvewcliend. dll file );
Function Method:
Different from the int readitemno (); function above, int startcliend (char * node); function with parameters, what should I do? Study the following code and you will understand it!
# Include "stdio. H"
# Include "windows. H"
Void main ()
{
Int;
Char B = 1;
Typedef int (callback * myfun1) (char *);
Hinstance dll123;
Dll123 = loadlibrary ("kingvewcliend. dll ");
If (null = dll123)
Printf ("Cant't find DLL ");
Myfun1 msg1;
Msg1 = (myfun1) getprocaddress (dll123, "startcliend ");
If (null = msg1)
Printf ("Cant't Find function ");
Else
A = msg1 (& B );
Printf ("% d/N", );
}