visual studio set up dll type project console program

  • 2020-06-07 04:55:24
  • OfStack

1. vs engineering type related knowledge

When using vs building engineering, if in the "template" is "Win32", "Win32 console application", or "Win32 project", after the project has been created, in the "properties - C/C + + - the preprocessor definition" of a macro definition: "WIN32", therefore, can be in code by checking whether there is a macro definition "WIN32" to the code make windows and linux control;

When creating an win32 project, selecting either "Win32 console Application" or "Win32 Project" will eventually jump to the "Application Settings" wizard, which requires the user to select "Application type". There are four types: windows Application, console application, DLL(D), and static library (S). It's just that initially if you select "Win32 Console Application" here vs will default to "Console Application" in this wizard, and if you select "Win32 Project" here vs will default to "Console Application" in this wizard. When we want to encapsulate the dll interface, select "Dll(D)" here

When the vs project is created, if the application type selected is "DLL(D)", the macro definition "WIN32" and "project name (uppercase) _EXPORTS" will be defined in "Properties --C/C++-- Preprocessor definition" after the project is created.

2. Create knowledge about dll type projects

In a project of type Dll, the macro in the dll header file is defined as follows:


1.//  The following  ifdef  Blocks are created to enable from  DLL  Export simpler  
2.//  Standard method for macros. this  DLL  All files are defined on the command line  ONEDLL_EXPORTS 
3.//  Symbol compiled. In the use of this  DLL  the  
1.//  This symbol should not be defined on any other item. That way, any other project that contains this file in the source file will  

The // ONEDLL_API function is treated as imported from DLL, and this DLL treats // symbols defined with this macro as exported. ___ 54en ONEDLL_EXPORTS#define extern "C", 62en (dllexport) else# ONEDLL_ES68en "C" ES70en (dllimport)
#endif

___ (dllexport) indicates that the function is an DLL output function, which can be called by other applications


extern  " C "  __declspec(dllexport) int add(int a, int b);

extern "C" is declared to be compiled by C. Since the C++ compiler causes this change in its function name at compile time, which makes the function uncallable in other applications, the C compiler does not change its function name after compilation. In this way, if a program compiled with C calls a function in dll, the function may not be found.


__declspec(dllexport) __declspec(dllimport)1 It is usually in the form of macros 

This translates, in the DLL code itself, to succdeclspec (dllexport) and in programmes using DLL, to ___, 97en (dllimport), with each flag identifying the current function to be exported and the current function to be imported.

Define 1 interface function

1.ONEDLL_API int fnOneDll(void);

3. dll call method

There are two ways to invoke DLL: dynamic and static (display dynamic links and implicit dynamic links)

(1) Dynamic invocation (display dynamic link) :


 typedef int(*lpAddFun)(int, int); // Macros define function pointer types 
 lpAddFun add;// A function pointer 
 HINSTANCE hDll=LoadLibrary( " path " );
 add=(lpAddFun)GetProcAddress(hDll, "add");// To obtain dll In the add A function pointer 
 FreeLibrary(hDll);

A function, pointer, or class returned from an dll call is Pointers, that is, the address of a function, variable, or class. One thing to note here is that you can't simply assign a function name.

(2) Static invocation (implicit dynamic link) :

Copy the generated.dll and.lib files into the project calling dll, using the command


 #pragma comment(lib,"dllTest.lib") , to achieve a static call, that is, put the dll It also compiles at compile time exe File, and then call it in the project using the following code: 
 #pragma comment(lib,"dllTest.lib")//.lib The file is just about its correspondence DLL Relocation information for functions in the file 
 extern "C" __declspec(dllimport) add(int x,int y);
 int main(int argc, char* argv[])
 {
  int result = add(2,3);
  printf("%d",result);
  return 0;
 }

As can be seen from the above code, the smooth progress of static invocation mode needs to complete two actions:

(1) Tells the compiler the path and file name of the.lib file corresponding to DLL, #pragma comment(lib," dllTest.lib "). When the programmer creates an DLL file, the connector automatically generates a corresponding.ES136en file for it, which contains the symbolic name and sequence number of the DLL export function (without the actual code). In the application, the.lib file is compiled as an alternative to DLL. Another way to do this explicitly is to set the directory in vc and includefiles

(2) Declare the import function, and each extern "C"/ES146en (dllimport) add(int x,int y) ___. Static invocation no longer requires system API to load and unload DLL and get the address of the exported function in DLL. This is because when the programmer builds the generated application by static linking, the function symbols called in the application that match the exported symbols in the.lib file go into the generated EXE file, and the file names of the corresponding DLL files contained in the.lib file are also stored in the EXE file by the compiler. When the DLL file needs to be loaded while the application is running, Windows will discover and load DLL based on this information, and then dynamically link the DLL function with the symbolic name. In this way, EXE will be able to call the output function of DLL directly from the function name, just like any other function 1 within the calling program.

conclusion


Related articles: