zoukankan      html  css  js  c++  java
  • DLL初试

    环境: VC++6.0

    步骤:

    1.建立一个WIN32 DYNAMIC-LINK LIBRARY工程,编写CPP文件,文件内容例如:

    1 #include "stdafx.h"
    2 #include "windows.h"
    3 #include "dll.h" 
    4 BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
    5 {
    6     return TRUE;
    7 }
    8 extern"C"_declspec(dllexport)int sum(int a, int b) { return a+b; }


    2.记得编写头文件,在本次事例中,头文件为dll.h,内容:

     1 extern"C"_declspec(dllexport) int sum(int a,int b); 

    3.进行编译连接,本过程可能回有点(相当)曲折.编译连接OK,生成*.dll *.lib 这两个文件便可用了.

    (静态调用)测试:

    创建一个WIN32 CONSOLE APPLICATION,编写CPP代码例如:

     1 // dllTest.cpp : Defines the entry point for the console application.
     2 //
     3 
     4 #include "stdafx.h"
     5 #include <stdio.h>
     6 extern"C"_declspec(dllexport) int sum(int a,int b);
     7 #pragma comment (lib,"dll")
     8 int main(int argc, char* argv[])
     9 {
    10     printf("%d
    ",sum(2,4));
    11 
    12     return 0;
    13 }

    编译连接即可.(记得将*.LIB *.DLL复制到测试的工程目录下)

    (动态调用)测试:

     1 #include "stdafx.h"
     2 #include <Windows.h>
     3 #include <stdio.h>
     4 
     5 typedef int(*dllSum)(int,int);
     6 int main(int argc, char* argv[])
     7 {
     8     HMODULE hModule=LoadLibrary("dll.dll");
     9     dllSum sum=(dllSum)GetProcAddress(hModule,"sum");
    10 
    11     printf("%d
    ",sum(2,22));
    12     return 0;
    13 }
  • 相关阅读:
    79. Word Search
    97. Interleaving String
    74. Search a 2D Matrix
    73. Set Matrix Zeroes
    72. Edit Distance
    71. Simplify Path
    64. Minimum Path Sum
    shell编程 备份mysql数据库并发送到另一个服务器
    linux 命令执行的判断依据: ;,&&,||
    linux 数据流重定向
  • 原文地址:https://www.cnblogs.com/A--Q/p/6560635.html
Copyright © 2011-2022 走看看