| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1315 人关注过本帖
标题:dll调用dll问题2
只看楼主 加入收藏
眼底星空
Rank: 4
等 级:业余侠客
威 望:3
帖 子:85
专家分:289
注 册:2006-9-2
收藏
得分:0 
试试include一下windows.h
方便的话把你代码贴出来看看吧

无为而为 && 每天进步一小点...
2012-09-17 12:46
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
程序代码:
//dll_1.h
#ifdef __cplusplus
#define EXPORT extern "C" __declspec(dllexport)
#else
#define EXPORT __declspec(dllexport)
#endif//__cplusplus

EXPORT int in_1(void);
EXPORT int out_1(void);

//
程序代码:
//dll_1.c
#include <Windows.h>
#include "dll_1.h"

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

EXPORT int in_1()
{
    return 1;
}

EXPORT int out_1()
{
    return 2;
}
//
程序代码:
//dll_2.h
#ifdef __cplusplus
#define EXPORT extern "C" __declspec(dllexport)
#else
#define EXPORT __declspec(dllexport)
#endif//__cplusplus

EXPORT int in_2(void);
EXPORT int out_2(void);
//
程序代码:
//dll_2.c
#include <Windows.h>
#include "dll_2.h"

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

EXPORT int in_2()
{
    int i = 0;
    HMODULE h = NULL;
    int (*fun)(void);
    SetDllDirectory(TEXT("E:\\Project\\DLL1"));
    h = LoadLibrary(TEXT("dll_1"));
    if (h)
    {
        fun = (int (__cdecl *)(void)) GetProcAddress(h, "in_1");
        i = 2*fun();
        FreeLibrary(h);
    }

    return i;
}

EXPORT int out_2()
{
    int i = 0;
    HMODULE h = NULL;
    int (*fun)(void);
    SetDllDirectory(TEXT("E:\\Project\\DLL1\\dll_1"));
    h = LoadLibrary(TEXT("dll_1"));
    if (h)
    {
        fun = (int (__cdecl *)(void)) GetProcAddress(h, "out_1");
        i = 2*fun();
        FreeLibrary(h);
    }

    return i;
}
//
程序代码:
//dll_3.h
#ifdef __cplusplus
#define EXPORT extern "C" __declspec(dllexport)
#else
#define EXPORT __declspec(dllexport)
#endif//__cplusplus

EXPORT int in_3(void);
EXPORT int out_3(void);
//
程序代码:
//dll_3.c
#include <Windows.h>
#include "dll_3.h"

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

EXPORT int in_3()
{
    int i = 0;
    HMODULE h = NULL;
    int (*fun)(void);
    SetDllDirectory(TEXT("E:\\Project\\DLL1\\dll_1"));
    h = LoadLibrary(TEXT("dll_1"));
    if (h)
    {
        fun = (int (__cdecl *)(void)) GetProcAddress(h, "in_1");
        i = 3*fun();
        FreeLibrary(h);
    }

    return i;
}

EXPORT int out_3()
{
    int i = 0;
    HMODULE h = NULL;
    int (*fun)(void);
    SetDllDirectory(TEXT("E:\\Project\\DLL1"));
    h = LoadLibrary(TEXT("dll_1"));
    if (h)
    {
        fun = (int (__cdecl *)(void)) GetProcAddress(h, "out_1");
        i = 3*fun();
        FreeLibrary(h);
    }

    return i;
}
//
程序代码:
//DLL_APP.c
#include <Windows.h>
#include <strsafe.h>

int WINAPI WinMain(HINSTANCE hInstace,
    HINSTANCE PrevhInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{
    HMODULE hDll_1 = NULL, hDll_2 = NULL, hDll_3 = NULL;
    int (*fun)(void);
    TCHAR buff[128] = {0};

    hDll_1 = LoadLibrary(TEXT("E:\\Project\\DLL1\\dll_1"));
    hDll_2 = LoadLibrary(TEXT("E:\\Project\\DLL2\\dll_2"));
    hDll_3 = LoadLibrary(TEXT("E:\\Project\\DLL3\\dll_3"));

    if (!hDll_1 || !hDll_2 || !hDll_3)
    {
        MessageBox(NULL, TEXT("error..."), TEXT("msg"), 0);
        return -1;
    }

    fun = (int (*)(void))GetProcAddress(hDll_1, "in_1");
    StringCchPrintf(buff, _countof(buff), TEXT("int in_1(void) = %d\n"), fun());
    MessageBox(NULL, buff, TEXT("msg"), 0);
    fun = (int (*)(void))GetProcAddress(hDll_1, "out_1");
    StringCchPrintf(buff, _countof(buff), TEXT("int out_1(void) = %d\n\n"), fun());
    MessageBox(NULL, buff, TEXT("msg"), 0);

    fun = (int (*)(void))GetProcAddress(hDll_2, "in_2");
    StringCchPrintf(buff, _countof(buff), TEXT("int in_2(void) = %d\n"), fun());
    MessageBox(NULL, buff, TEXT("msg"), 0);
    fun = (int (*)(void))GetProcAddress(hDll_2, "out_2");
    StringCchPrintf(buff, _countof(buff), TEXT("int out_2(void) = %d\n\n"), fun());
    MessageBox(NULL, buff, TEXT("msg"), 0);

    fun = (int (*)(void))GetProcAddress(hDll_3, "in_3");
    StringCchPrintf(buff, _countof(buff), TEXT("int in_3(void) = %d\n"), fun());
    MessageBox(NULL, buff, TEXT("msg"), 0);
    fun = (int (*)(void))GetProcAddress(hDll_3, "out_3");
    StringCchPrintf(buff, _countof(buff), TEXT("int out_3(void) = %d\n\n"), fun());
    MessageBox(NULL, buff, TEXT("msg"), 0);

    FreeLibrary(hDll_1);
    FreeLibrary(hDll_2);
    FreeLibrary(hDll_3);

    return 0;
}












2012-09-17 15:40
yalewang
Rank: 2
等 级:论坛游民
帖 子:125
专家分:35
注 册:2010-4-15
收藏
得分:0 
版主帮一下啊
2012-09-18 10:59
KBTyanhuo
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-9-27
收藏
得分:0 
你可以搜一下 __declspec(dllexport) 和 __declspec(dllimport) 的区别和使用

在 dll 中调用其他 dll 中的函数和变量以及类,需要用后者修饰
2012-09-27 11:12
快速回复:dll调用dll问题2
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.057928 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved