Note: Use FORTRAN to create a dynamic link library, load the dynamic link library in the C/C ++ language environment, and call functions in the dynamic link library. This method uses the dynamic loading of dynamic linked libraries.
Development Environment:
Compaq Visual Fortran 6.5
Visual c ++ 6.0
1. Special requirements in the program:
1.1 Fortran:
Add the following declaration to the Fortran function:
! Dec $ attributes dllexport: functionname |
Indicates that this function is a DLL export function, and functionname indicates the function name.
Note: The parameter passed by the Fortran function call is the transfer address. Therefore, the parameter address must be passed when called in C/C ++.
1.2 C/C ++:
1.2.1 load Dynamic Link Library:
Hmodule loadlibrary (pctstr pszdllpathname ); Hmodule LoadLibraryEx ( Pctstr pszdllpathname, // dll path and name Handle hfile, DWORD dwflags); // loading options |
The two functions map a DLL to the address space of the process. The return value type is hmodule (equivalent to hinstance), which is the virtual memory address mapped to the DLL.
Use these two functions to dynamically load the DLL.
The hfile parameter in the LoadLibraryEx function is reserved for future expansion. It must be set to null now.
Dwflags is the Dynamic Link Library Loading option. Generally, dont_resolve_dll_refference is used to tell the system to map the DLL to the process address space.
1.2.2 uninstall the Dynamic Link Library:
Bool freelibrary (hmodule hinstdll ); |
1.2.3 search for the output library function address in the specified dynamic link library (DLL:
Farproc getprocaddress ( Hmodule, // DLL module handle Lpcstr lpprocname); // function name |
The return value is a pointer to the function. Remember to force type conversion. Otherwise, an error occurs during compilation. At the same time, it is case-insensitive in FORTRAN, so all function names must be uppercase letters during retrieval.
2. Instance
2.1 instance 1:
Notes: Fortran implements two integer addition
FORTRAN section:
Create a project. The project type is Fortran Dynamic Link Library and the project name is Testa. Select an empty DLL application and add a file test. f90. Edit the following content in the file:
Function getadd (a, B) result (r) Implicit none ! Dec $ attributes dllexport: getadd Integer: Integer: B Integer: R R = a + B Return End |
Compile and generate DLL files.
C/C ++:
Create an MFC Appwizard (exe) project named testamfc and select the dialog based option.
Edit resources:
Delete OK cancel and add a button. The button title is "add" and the ID is "idc_button_add. Set as the default button (that is, press this button when you press Enter ).
Add three editable text boxes: idc_edit_a, idc_edit_ B, and idc_edit_result.
The member variable types for the three text boxes are int, M_a, m_ B, and m_result.
Add a control function for the button Add. The function name is onbuttonadd.
Compile this function:
Void ctestamfcdlg: onbuttonadd () { This-> updatedata (); Hmodule hdll = LoadLibraryEx (text ("Testa. dll"), null, Dont_resolve_dll_references ); If (! Hdll) { MessageBox ("failed to load dynamic link library", "error", mb_ OK ); Return; } INT (_ stdcall * proc) (int *, int *); // pay attention to the pointer to the function in the declaration method, which requires _ stdcall Proc = (INT (_ stdcall *) (int *, int *) getprocaddress (hdll, "getadd "); If (! Proc) { MessageBox ("Loading Function failed", "error", mb_ OK ); Freelibrary (hdll ); Return; } M_result = proc (& M_a, & m_ B ); This-> updatedata (false ); Freelibrary (hdll ); } |
Copy the DLL file compiled by FORTRAN to the current project directory for compilation and execution.
2.2 Example 2:
Note: The Fortran dynamic link library is called to implement variable-length array addition.
FORTRAN section:
Create a project. The project type is Fortran Dynamic Link Library and the project name is test. Select an empty DLL application and add a file test. f90. Edit the following content in the file:
Function getadd (A, B, n, R) result (RR) Implicit none ! Dec $ attributes dllexport: getadd Integer: (*)! The parameter is a variable-size one-dimensional array. Integer: B (*)! The parameter is a variable-size one-dimensional array. Integer: R (*)! The parameter is a variable-size one-dimensional array. Integer: N Integer: I Integer: rr Do I = 1, n! Use cyclic computing Addition R (I) = a (I) + B (I) End do RR = N Return End |
C/C ++:
Create an MFC Appwizard (exe) project named testamfc and select the dialog based option.
Edit resources:
Delete OK cancel and add a button. The button title is "add" and the ID is "idc_button_add. Set as the default button (that is, press this button when you press Enter ).
Add nine editable text boxes with IDs: idc_edit_a1 ~ Idc_edit_a3, idc_edit_b1 ~ Idc_edit_b3, idc_edit_c1 ~ Idc_edit_c3.
Add member variables for them respectively
M_a1, m_a2, m_a3; m_b1, m_b2, m_b3; m_c1, m_c2, m_c3;
Add a control function named onbuttonadd for the button add.
Edit this function
Void ctestmfcdlg: onbuttonadd () { This-> updatedata (); Int A [3]; Int B [3]; Int C [3]; Int n = 3; /* M_a1 = 1; M_a2 = 2; M_a3 = 3; M_b1 = 3; M_b2 = 2; M_b3 = 1; */ // This-> updatedata (false ); A [0] = m_a1; A [1] = m_a2; A [2] = m_a3; B [0] = m_b1; B [1] = m_b2; B [2] = m_b3; Hmodule hdll = LoadLibraryEx (text ("test. dll"), null, Dont_resolve_dll_references ); If (! Hdll) { MessageBox ("failed to load dynamic link library", "error", mb_ OK ); Freelibrary (hdll ); Return; } // Pay attention to the pointer of the declaration method to the function, which requires _ stdcall INT (_ stdcall * proc) (int *, int *); Proc = (INT (_ stdcall *) (int *, int *) getprocaddress (hdll, "getadd "); If (! Proc) { MessageBox ("Loading Function failed", "error", mb_ OK ); Freelibrary (hdll ); Return; } Proc (a, B, & N, C ); M_c1 = C [0]; M_c2 = C [1]; M_c3 = C [2]; This-> updatedata (false ); Freelibrary (hdll ); } |
Copy the DLL file compiled by FORTRAN to the current project directory for compilation and execution.
Another problem that was not mentioned before is that the Fortran function parameters are passed as the address transfer method. Therefore, when calling the functions in the Fortran dynamic link library, the addresses are all transmitted.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
From: http://www.cnblogs.com/nervending/archive/2010/08/18/1802351.html