zoukankan      html  css  js  c++  java
  • Using Run-Time Dynamic Linking(使用运行时动态链接库)

    // A simple program that uses LoadLibrary and // GetProcAddress to access myPuts from Myputs.dll. 
    #include <windows.h> 
    #include <stdio.h> 
     
    typedef int (__cdecl *MYPROC)(LPWSTR); 
     
    int main( void ) 
    { 
        HINSTANCE hinstLib; 
        MYPROC ProcAdd; 
        BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 
     
        // Get a handle to the DLL module.
     
        hinstLib = LoadLibrary(TEXT("MyPuts.dll")); 
     
        // If the handle is valid, try to get the function address.
     
        if (hinstLib != NULL) 
        { 
            ProcAdd = (MYPROC) GetProcAddress(hinstLib, "myPuts"); 
     
            // If the function address is valid, call the function.
     
            if (NULL != ProcAdd) 
            {
                fRunTimeLinkSuccess = TRUE;
                (ProcAdd) (L"Message sent to the DLL function
    "); 
            }
            // Free the DLL module.
     
            fFreeResult = FreeLibrary(hinstLib); 
        } 
    
        // If unable to call the DLL function, use an alternative.
        if (! fRunTimeLinkSuccess) 
            printf("Message printed from executable
    "); 
    
        return 0;
    
    }
     
     

     
    int (__cdecl *MYPROC)(LPWSTR);  

    int 表示函数的返回值为 int     若是 TLSConnectToLsServer 函数,则应为 TLS_HANDLE
    (__cdecl *MYPROC) 说明这是一个函数指针,调用方式为 __cdecl (还有_stdcall
    LPWSTR 说明此函数有一个参数,类型为 LPWSTR

    (函数指针语法就是要求这样定义的,不是为什么要用,而是必须要用)
     


    使用LoadLibrary把dll文件load进内存,使用GetProcAddress得到函数地址就可以使用了



    #include <stdio.h>
    #include <windows.h>
    #include <string>
    #include <iostream> 
    
    using namespace std;
    void main(void)
    {
            typedef int (__stdcall   *PFUNCTION)( int k,int i);
            //声明参数类型,以后会用到PFUNCTION
            HMODULE hLib = ::LoadLibrary("xx.dll");
            if ( NULL == hLib )     
            {
                    perror("装载DLL文件错误:");
            }
            else
            {
    
                    PFUNCTION myAddFunction=myAddFunction=(PFUNCTION)::GetProcAddress(hLib,"ConnectEx");//ConnectEx是动态库中的方法
                    if ( NULL == myAddFunction)     
                    {
                            perror("装载的DLL文件中无对应的函数:");
                    }
                    else
                    {
                            int k=myAddFunction(1,2);
                            cout <<k <<endl;
                    }
                    ::FreeLibrary(hLib);
            }
    }
  • 相关阅读:
    工具函数(代码块的大小,代码块起始地址,提升进程权限)
    在共享DLL中使用MFC 和在静态库中使用MFC的区别
    虚拟机检测绕过总结--不定时更新
    OSGI原形(.NET)
    iOS开发技术分享(1)— iOS本地数据存储
    将JSON映射为实体对象(iOS篇)
    灵活的路由(上)
    github开源项目
    EF里查看/修改实体的当前值、原始值和数据库值以及重写SaveChanges方法记录实体状态
    实体能否处于非法状态
  • 原文地址:https://www.cnblogs.com/LeoGodfrey/p/3643897.html
Copyright © 2011-2022 走看看