• dll 入口函数


    http://support.microsoft.com/kb/815065/zh-cn

    // SampleDLL.cpp // #include "stdafx.h" #define EXPORTING_DLL #include "sampleDLL.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } void HelloWorld() { MessageBox( NULL, TEXT("Hello World"), TEXT("In a DLL"), MB_OK); }
    // File: SampleDLL.h
    //
    #ifndef INDLL_H
    #define INDLL_H
    
    #ifdef EXPORTING_DLL
    extern __declspec(dllexport) void HelloWorld() ;
    #else
    extern __declspec(dllimport) void HelloWorld() ;
    #endif
    
    #endif

    下面的代码是一个“Win32 应用程序”项目的示例,该示例调用 SampleDLL DLL 中的导出 DLL 函数。

    // SampleApp.cpp 
    //
    
    #include "stdafx.h"
    #include "sampleDLL.h"
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    { 	
    	HelloWorld();
    	return 0;
    }

    注意:在加载时动态链接中,您必须链接在生成 SampleDLL 项目时创建的 SampleDLL.lib 导入库。

    在运行时动态链接中,您应使用与以下代码类似的代码来调用 SampleDLL.dll 导出 DLL 函数。

    ...
    typedef VOID (*DLLPROC) (LPTSTR);
    ...
    HINSTANCE hinstDLL;
    DLLPROC HelloWorld;
    BOOL fFreeDLL;
    
    hinstDLL = LoadLibrary("sampleDLL.dll");
    if (hinstDLL != NULL)
    {
        HelloWorld = (DLLPROC) GetProcAddress(hinstDLL, "HelloWorld");
        if (HelloWorld != NULL)
            (HelloWorld);
    
        fFreeDLL = FreeLibrary(hinstDLL);
    }
    ...
    

    当您编译和链接 SampleDLL 应用程序时,Windows 操作系统将按照以下顺序在下列位置中搜索 SampleDLL DLL:

      1. 应用程序文件夹
      2. 当前文件夹
      3. Windows 系统文件夹

        注意GetSystemDirectory 函数返回 Windows 系统文件夹的路径。
      4. Windows 文件夹

        注意GetWindowsDirectory 函数返回 Windows 文件夹的路径。

    dll文件里面,有一个入口函数dllmain

    另一个函数helloworld,供主调函数使用,并不出现在入口函数中

  • 相关阅读:
    @XmlAccessorType @XmlType 详解
    eclipse properties 文件查看和编辑插件 Properties Editor
    对象设计解耦的方法IOC和DI
    J2EE7与Servlet3.x
    关于maven
    Spring版本功能变更&Spring4.x的新特性
    2017第37周三
    Java线程池
    Java并发包
    Java内部类
  • 原文地址:https://www.cnblogs.com/minggoddess/p/3678740.html
Copyright © 2020-2023  润新知