查看Win32 下动态链接库 (DLL) 机制笔记的源代码
←
Win32 下动态链接库 (DLL) 机制笔记
跳转到:
导航
,
搜索
因为以下原因,你没有权限编辑本页:
您刚才请求的操作只有这个用户组中的用户才能使用:
用户
您可以查看并复制此页面的源代码:
== DLL 导出简单 C 函数 == simple.cc: <pre> #include <stdio.h> #ifdef __cplusplus // If used by C++ code, extern "C" { // we need to export the C interface #endif __declspec(dllexport) int myPuts(char *lpszMsg) { printf("%s\n", lpszMsg); return 1; } #ifdef __cplusplus } #endif </pre> 编译生成 DLL: cl /LD /Fesimple.dll simple.cc 选项 /LD 告诉编译器创建 DLL 完了会生成 simple.dll simple.lib <br><br> === 加载时动态链接测试 === <pre> #include <windows.h> int myPuts(char *str); // a function from a DLL int main(VOID) { int Ret = 1; Ret = myPuts("Using the DLL function, HaHaHaHa, Tutututu\n"); return Ret; } </pre> 以上保存为 load.time.link.c 编译之: cl /Feload.time.link.exe load.time.link.c simple.lib simple.lib 为 simple.dll 对应的 import library,链接时为链接器 link.exe 所需要 <br><br> === 运行时动态加载测试 === <pre> #include <stdio.h> #include <windows.h> typedef int (*MYFUNC)(char *); VOID main(VOID) { HINSTANCE hinstLib; MYFUNC ProcAdd; BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; //用 LoadLibrary() 加载 simple.dll hinstLib = LoadLibrary(TEXT("simple")); // If the handle is valid, try to get the function address. if (hinstLib != NULL) { //用 GetProcAddress() 获取 DLL 中函数地址 ProcAdd = (MYPROC) GetProcAddress(hinstLib, TEXT("myPuts")); // If the function address is valid, call the function. if (NULL != ProcAdd) { fRunTimeLinkSuccess = TRUE; (ProcAdd) (TEXT("Message via DLL function, HaHaHa, TUTUTUTUTU\n")); } // Free the DLL module. fFreeResult = FreeLibrary(hinstLib); } if (! fRunTimeLinkSuccess) printf("Load DLL failed\n"); } </pre> 存为 run.time.link.c,如下命令编译之: cl /Ferun.time.link.exe run.time.link.c <br><br>
返回到
Win32 下动态链接库 (DLL) 机制笔记
。
个人工具
18.223.106.199
该IP地址的讨论
登录
名字空间
页面
讨论
变换
查看
阅读
查看源代码
查看历史
操作
搜索
导航
首页
社区专页
新闻动态
最近更改
随机页面
帮助
工具箱
链入页面
相关更改
特殊页面