dll问题

我刚学dll文件的使用,自己写了个程序,dll文件的定义如下:
//dll.h
#ifndef   _DLL_H_
#define   _DLL_H_

#include   <windows.h>

#if   BUILDING_DLL
#   define   DLLIMPORT   __declspec   (dllexport)
#else   /*   Not   BUILDING_DLL   */
#   define   DLLIMPORT   __declspec   (dllimport)
#endif   /*   Not   BUILDING_DLL   */

DLLIMPORT   void   CALLBACK   DllFoo(void);

#endif   /*   _DLL_H_   */

//dllmain.cpp
/*   Replace   "dll.h "   with   the   name   of   your   header   */
#include   "dll.h "
#include   <windows.h>
#include   <iostream>

int   WINAPI   DllMain(HINSTANCE   hInstance,   DWORD   fdwReason,   PVOID   pvReserved)
{
        return   TRUE;
}

DLLIMPORT   void   CALLBACK   DllFoo(void)
{
        std::cout < < "这是dll里的函数 ";  
}
编译后得到mydll.dll   和   libmydll.a(我用的编译器是devc++,这里的*.a相当于*.lib).
在google里找了以后我用#include   "dll.h "并在编译器工程设置里加入链接libmydll.a后使用DllFoo成功,但是用另一种方法,也就是不#include   "dll.h ",并且不用链接文件,在程序中使用LoadLibrary()却出问题,程序如下:
//main.cpp
#include   <windows.h>
#include   <iostream>


using   namespace   std;

typedef   void   (*DllFoo)(void);

int   main(int   argc,   char   *argv[])
{
        DllFoo   _DllFoo;
        HINSTANCE   hInstLibrary   =   LoadLibrary( "mydll.dll ");
        if   (hInstLibrary   ==   NULL)
        {
                  cout < < "载入dll失败 " < <endl;
                  FreeLibrary(hInstLibrary);  
                  system( "pause ");
                  return   1;
        }
        _DllFoo   =   (DllFoo)GetProcAddress(hInstLibrary, "DllFoo ");
        if(_DllFoo   ==   NULL)//运行后程序进入这里,显示“转换失败”后结束
        {
                  cout < < "转换失败 " < <endl;
                  FreeLibrary(hInstLibrary);
                  system( "pause ");
                  return   1;  
        }
        //以下程序没有运行
        DllFoo();
        cout < <endl;
        FreeLibrary(hInstLibrary);
        system( "PAUSE ");
        return   EXIT_SUCCESS;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值