怎么在一个vc的 win32 console application 里引入一个dll?

解决方案 »

  1.   

    我要调用一个dll。不知道怎么写代码
    vb  里不是 reference 可以加入吗?我想vc里是不是也有类似的动作?
      

  2.   

    Using Run-Time Dynamic Linking
    You can use the same DLL in both load-time and run-time dynamic linking. The following source code produces the same output as the load-time example in the previous section. The program uses the LoadLibrary function to get a handle to MYPUTS.DLL. If LoadLibrary succeeds, the program uses the returned handle in the GetProcAddress function to get the address of the DLL's myPuts function. After calling the DLL function, the program calls the FreeLibrary function to unload the DLL. The following example illustrates an important difference between run-time and load-time dynamic linking. If the MYPUTS.DLL file is not available, the application using load-time dynamic linking simply terminates. The run-time dynamic linking example, however, can respond to the error. // File:  RUNTIME.C
    // A simple program that uses LoadLibrary and 
    // GetProcAddress to access myPuts from MYPUTS.DLL. 
     
    #include <stdio.h> 
    #include <windows.h> 
     
    typedef VOID (*MYPROC)(LPTSTR); 
     
    VOID main(VOID) 

        HINSTANCE hinstLib; 
        MYPROC ProcAdd; 
        BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 
     
        // Get a handle to the DLL module.
     
        hinstLib = LoadLibrary("myputs"); 
     
        // 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 (fRunTimeLinkSuccess = (ProcAdd != NULL)) 
                (ProcAdd) ("message via DLL function\n"); 
     
            // Free the DLL module.
     
            fFreeResult = FreeLibrary(hinstLib); 
        } 
     
        // If unable to call the DLL function, use an alternative.
     
        if (! fRunTimeLinkSuccess) 
            printf("message via alternative method\n"); 

    Because the program uses run-time dynamic linking, you should not link with the import library when creating the program module. 
      

  3.   

    有静态和动态两种,静态的需要lib而动态的只需要一个dll就可以了(但是要知道函数的定义一般是头文件)动态相对复杂一点!但是掌握了也不是很难
    //第一步  定义函数指针就是你要调用的函数,参数必须一致,不然就内存泄露
    typedef HANDLE (PASCAL * OPEN)(char *,int);
    //第二步  定义本地函数
    //一般前两步都在头文件里面声明
    OPEN   open;
    //第三步 加载dll
    HMODULE m_hLib;
    m_hLib  ::LoadLibrary"E:\\yewufuwu\\TERMINALDLL\\Debug\\TerminalDLL.dll");
    //输入相应的路径
    if(m_hLib == NULL )
    {
       MessageBox("动态连接库没有找到!!!","LoadLibrary",MB_OK);
      return ;
    }
    //第四步
    else
    {
       MessageBox("动态连接库成功打开...","LoadLibrary",MB_OK);
       open=(OPEN)GetProcAddress(m_hLib, "OpenDevice");
    }//第五步
    long  handle =open("EF",1);动态库下的函数如下
    //*************打开终端*****************************************
    HANDLE __declspec(dllexport)  WINAPI
    OpenDevice(char * szType,int nType)
    { hReader=theTerminal.OpenReader(szType,0);
    if(hReader==0)
    return 0;
    DWORD ThreadID=0;
        return hReader;
    }
      

  4.   


    以前的帖子好多http://community.csdn.net/Expert/topic/3192/3192962.xml?temp=.6919367http://community.csdn.net/Expert/topic/2649/2649772.xml?temp=.1770441
      

  5.   

    为什么我在vb里的reference里加入这个要用的dll,报错 “can not reference to the specified dll”
      

  6.   

    我没有  .h 的头文件,只有一个dll 阿
      

  7.   

    我照各位的方法能成功 loadlibrary 但是执行到 GetProcAddress 方法时总是返回null
    method的名字我是输入的dll的帮助手册里写的method列表里的名字。怎么回事啊?
      

  8.   

    GetProcAddress(m_hLib, "method");
    GetProcAddress(m_hLib, " method ");
    是不一样的!你注意method前边或者后边是否有空格