一、C封装dll
首先,使用vs2008创建windows32控制台程序,选择dll复选框,确定。
然后在stdafx.h头文件中声明导出函数,举例如下
__declspec(dllexport) int add(int a ,int b);
如果创建的是c++工程则需要在函数声明前加上extern 接着在你创建的c文件中实现上述函数体,如下
__declspec(dllexport) int add(int a ,int b)
{
return a+b;
}
如果仅仅使用一个c文件实现dll,举例如下
#include __declspec(dllexport) int add(int a ,int b); C” “ BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } __declspec(dllexport) int add(int a ,int b) { return a+b; } 二、C调用dll 把之前生成的dll文件拷贝入调用工程的debug文件夹下。 #include #include int main() { typedef int (*ADD)(int ,int);//函数指针类型 HINSTANCE Hint = LoadLibrary(\"adddll.dll\");//加载我们刚才生成的dll ADD add = (ADD)GetProcAddress(Hint,\"add\");//取得dll导出的add方法 printf(\"%d\",add(3,4)); return 0; } 三、C调用lib 把上述工程生成的lib文件拷入建立的工程下,然后在代码中加入如下语句 #pragma comment(lib,\"adddllcpp.lib\") extern int add(int ,int);//没有加这句而只加上面这句(或在工程设置里加上DLL.lib)会链接错误 注意:工程属性字符集必须改成“使用多字节字符集”。 四、dlltool工具封装dll 可以使用Dev C++安装之后bin目录下的G++等可执行文件。 13 dlltool dlltool is used to create the files needed to create dynamic link libraries (DLLs) on systems which understand PE format image files such as Windows. A DLL contains an export table which contains information that the runtime loader needs to resolve references from a referencing program. dlltool被用来创建那些文件,它们在那些理解PE格式的镜像文件的系统上,例如Winsows系统,上被需要创建成动态链接库(DLLs)。一个DLL包含一个导出表,表中包含运行时加载器需要从一个参考的参考程序信息。 The export table is generated by this program by reading in a .def file or scanning the .a and .o files which will be in the DLL. A .o file can contain information in special .drectve sections with export information. 导出表通过读取封装在DLL中的一个.def文件或者浏览.a和.o文件而生成。一个.o文件能够包含一些特殊导出信息中的.drectve部分。 Note: dlltool is not always built as part of the binary utilities, since it is only useful for those targets which support DLLs. 注意:dlltool不总是被构建作为二进制通用库的一部分,因为它对那些支持DLLs的目标有用。 dlltool [-d|--input-def def-file-name] [-b|--base-file base-file-name] [-e|--output-exp exports-file-name] [-z|--output-def def-file-name] [-l|--output-lib library-file-name] [--export-all-symbols] [--no-export-all-symbols] [--exclude-symbols list] [--no-default-excludes] [-S|--as path-to-assembler] [-f|--as-flags options] [-D|--dllname name] [-m|--machine machine] [-a|--add-indirect] [-U|--add-underscore] [-k|--kill-at] [-A|--add-stdcall-alias] [-p|--ext-prefix-alias prefix] [-x|--no-idata4] [-c|--no-idata5] [-i|--interwork] [-n|--nodelete] [-t|--temp-prefix prefix] [-v|--verbose] [-h|--help] [-V|--version] [object-file ...] dlltool reads its inputs, which can come from the -d and -b options as well as object files specified on the command line. It then processes these inputs and if the -e option has been specified it creates a exports file. If the -l option has been specified it creates a library file and if the -z option has been specified it creates a def file. Any or all of the -e, -l and -z options can be present in one invocation of dlltool. dlltool读取它的输入,这些输入来自于-d和-b选项就像目标文件在命令行上呗指定一样。然后它通过处理这些输入并且如果-e选项呗指定它会创建一个导出文件。如果-l选项被指定,它会创建一个库文件并且如果-z选项被指定,它会创建一个def文件。任何或全部-e,-l和-z选项,能够出现在dlltool的输入。 When creating a DLL, along with the source for the DLL, it is necessary to have three other files. dlltool can help with the creation of these files. 创建一个DLL时,伴随DLL的源,有三个其他文件是必要的。dlltool能够帮助这些 文件创建。 The first file is a .def file which specifies which functions are exported from the DLL, which functions the DLL imports, and so on. This is a text file and can be created by hand, or dlltool can be used to create it using the -z option. In this case dlltool will scan the object files specified on its command line looking for those functions which have been specially marked as being exported and put entries for them in the .def file it creates. 第一个文件时.def文件,这个文件指定了需要从DLL中导出的函数以及DLL引用的函数等等。它是一个文本文件能够直接创建,或者dlltool可以使用-z选项创建它。在这种情况dlltool将会扫描在命令行中指定的目标文件寻找那些已经被特殊标记为导出的函数并且把它创建的.def文件中的入口给他们。 In order to mark a function as being exported from a DLL, it needs to have an -export: 为了表姐从DLL中导出的函数,它需要有一个-export: asm (\".section .drectve\"); asm (\".ascii \\\"-export:my_func\\\"\"); int my_func (void) { ... } The second file needed for DLL creation is an exports file. This file is linked with the object files that make up the body of the DLL and it handles the interface between the DLL and the outside world. This is a binary file and it can be created by giving the -e option to dlltool when it is creating or reading in a .def file. 第二个在创建DLL时需要的文件是一个导出文件。这个文件和组成DLL主体的目标文件链接并且它也掌握着DLL和外部世界的接口。它是一个二进制文件并且能够在dlltool创建或读取一个.def文件时添加-e选项创建。 The third file needed for DLL creation is the library file that programs will link with in order to access the functions in the DLL. This file can be created by giving the -l option to dlltool when it is creating or reading in a .def file. 第三个文件是库文件,为了访问DLL中的函数程序将要使用这个库文件。这个文件能够在dlltool创建或读取一个.def文件时添加-l选项创建。 dlltool builds the library file by hand, but it builds the exports file by creating temporary files containing assembler statements and then assembling these. The -S command line option can be used to specify the path to the assembler that dlltool will use, and the -f option can be used to pass specific flags to that assembler. The -n can be used to prevent dlltool from deleting these temporary assembler files when it is done, and if -n is specified twice then this will prevent dlltool from deleting the temporary object files it used to build the library. dlltool手动构建库文件,但是它通过创建包含汇编语句然后汇编这些临时文件构建导出文件。-S命令选项能被用来指定dlltool将要使用的汇编器的路径,同时-f选项能用来传递指定标志给汇编器。-n选项能够防止dlltool删除这临时汇编文件,并且如果-n选项指定两次的haunted,将会防止dlltool删除用来创建库的临时目标文件。 Here is an example of creating a DLL from a source file dll.c and also creating a program (from an object file called program.o) that uses that DLL: 下面是一个从源文件dll.c创建dll的一个例子并且创建了调用dll的一个程序。 gcc -c dll.c dlltool -e exports.o -l dll.lib dll.o gcc dll.o exports.o -o dll.dll gcc program.o dll.lib -o program The command line options have the following meanings: 命令行选项有如下含义: -d filename --input-def filename Specifies the name of a .def file to be read in and processed. 指定读取和处理的.def文件名 -b filename --base-file filename Specifies the name of a base file to be read in and processed. The contents of this file will be added to the relocation section in the exports file generated by dlltool. 指定读取和处理的基本文件名。文件内容添加到dlltool生成的导出文件的指定位置部分。 -e filename --output-exp filename Specifies the name of the export file to be created by dlltool. 指定dlltool创建的导出文件名。 -z filename --output-def filename Specifies the name of the .def file to be created by dlltool. 指定dlltool创建的饿.def文件名。 -l filename --output-lib filename Specifies the name of the library file to be created by dlltool. 指定dlltool创建的库文件名。 --export-all-symbols Treat all global and weak defined symbols found in the input object files as symbols to be exported. There is a small list of symbols which are not exported by default; see the --no-default-excludes option. You may add to the list of symbols to not export by using the --exclude-symbols option. --no-export-all-symbols Only export symbols explicitly listed in an input .def file or in .drectve sections in the input object files. This is the default behaviour. The .drectve sections are created by dllexport attributes in the source code. --exclude-symbols list Do not export the symbols in list. This is a list of symbol names separated by comma or colon characters. The symbol names should not contain a leading underscore. This is only meaningful when --export-all-symbols is used. --no-default-excludes When --export-all-symbols is used, it will by default avoid exporting certain special symbols. The current list of symbols to avoid exporting is DllMain@12, DllEntryPoint@0, impure_ptr. You may use the --no-default-excludes option to go ahead and export these special symbols. This is only meaningful when --export-all-symbols is used. -S path --as path Specifies the path, including the filename, of the assembler to be used to create the exports file. -f options --as-flags options Specifies any specific command line options to be passed to the assembler when building the exports file. This option will work even if the -S option is not used. This option only takes one argument, and if it occurs more than once on the command line, then later occurrences will override earlier occurrences. So if it is necessary to pass multiple options to the assembler they should be enclosed in double quotes. -D name --dll-name name Specifies the name to be stored in the .def file as the name of the DLL when the -e option is used. If this option is not present, then the filename given to the -e option will be used as the name of the DLL. -m machine -machine machine Specifies the type of machine for which the library file should be built. dlltool has a built in default type, depending upon how it was created, but this option can be used to override that. This is normally only useful when creating DLLs for an ARM processor, when the contents of the DLL are actually encode using Thumb instructions. -a --add-indirect Specifies that when dlltool is creating the exports file it should add a section which allows the exported functions to be referenced without using the import library. Whatever the hell that means! -U --add-underscore Specifies that when dlltool is creating the exports file it should prepend an underscore to the names of the exported functions. -k --kill-at Specifies that when dlltool is creating the exports file it should not append the string @ -A --add-stdcall-alias Specifies that when dlltool is creating the exports file it should add aliases for stdcall symbols without @ -p --ext-prefix-alias prefix Causes dlltool to create external aliases for all DLL imports with the specified prefix. The aliases are created for both external and import symbols with no leading underscore. -x --no-idata4 Specifies that when dlltool is creating the exports and library files it should omit the .idata4 section. This is for compatibility with certain operating systems. -c --no-idata5 Specifies that when dlltool is creating the exports and library files it should omit the .idata5 section. This is for compatibility with certain operating systems. -i --interwork Specifies that dlltool should mark the objects in the library file and exports file that it produces as supporting interworking between ARM and Thumb code. -n --nodelete Makes dlltool preserve the temporary assembler files it used to create the exports file. If this option is repeated then dlltool will also preserve the temporary object files it uses to create the library file. -t prefix --temp-prefix prefix Makes dlltool use prefix when constructing the names of temporary assembler and object files. By default, the temp file prefix is generated from the pid. -v --verbose Make dlltool describe what it is doing. -h --help Displays a list of command line options and then exits. -V --version Displays dlltool's version number and then exits. • def file format: The format of the dlltool .def file 13.1 The format of the dlltool .def file A .def file contains any number of the following commands: NAME name [ , base ] The result is going to be named name.exe. LIBRARY name [ , base ] The result is going to be named name.dll. EXPORTS ( ( ( name1 [ = name2 ] ) | ( name1 = module-name . external-name ) ) [ integer ] [ NONAME ] [ CONSTANT ] [ DATA ] [ PRIVATE ] ) * Declares name1 as an exported symbol from the DLL, with optional ordinal number integer, or declares name1 as an alias (forward) of the function external-name in the DLL module-name. IMPORTS ( ( internal-name = module-name . integer ) | [ internal-name = ] module-name . external-name ) ) * Declares that external-name or the exported function whose ordinal number is integer is to be imported from the file module-name. If internal-name is specified then this is the name that the imported function will be referred to in the body of the DLL. DESCRIPTION string Puts string into the output .exp file in the .rdata section. STACKSIZE number-reserve [, number-commit ] HEAPSIZE number-reserve [, number-commit ] Generates --stack or --heap number-reserve,number-commit in the output .drectve section. The linker will see this and act upon it. CODE attr + DATA attr + SECTIONS ( section-name attr + ) * Generates --attr section-name attr in the output .drectve section, where attr is one of READ, WRITE, EXECUTE or SHARED. The linker will see this and act upon it. 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- 69lv.com 版权所有 湘ICP备2023021910号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务