返回信息流// dlltest.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <string>
#include <fstream>
#include <io.h>
#include <fcntl.h>
#define LICENSEFILE "D:\\wzf\\wzf\\license.txt"
using namespace std;
typedef int (*lpdllFun)(const char*); //宏定义函数指针类型
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{HINSTANCE hdDll;//DLL句柄
lpdllFun DLLFun;//函数指针
hdDll = LoadLibrary("D:\\wzf\\wzf\\callmd5\\Debug\\callmd5.dll");
DLLFun=(lpdllFun)GetProcAddress(hdDll, "Decode");
if (DLLFun != NULL)
{
if(DLLFun(LICENSEFILE) == 0)
{
cout<<"ok";
break;}
else
{
cout<<"error";
FreeLibrary(hdDll);
break;
}
}
}
break;
case DLL_THREAD_ATTACH:
{}
break;
case DLL_THREAD_DETACH:
{}
break;
case DLL_PROCESS_DETACH:
{}
break;
default: break;
}
return TRUE;
}
extern "C" _declspec(dllexport) int _stdcall add(int x,int y)
{
return (x+y);
}
//main.cpp
#include <sys/stat.h>
#include <io.h>
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <string>
#include <fstream>
#include <io.h>
#include <fcntl.h>
using namespace std;
typedef int(WINAPI *lpAddFun)(int, int); //宏定义函数指针类型
int main(int argc, char *argv[])
{
HINSTANCE aDll;//DLL句柄
lpAddFun addFun; //函数指针
aDll = LoadLibrary("D:\\wzf\\wzf\\dlltest\\Debug\\dlltest.dll");
if (aDll != NULL)
{
addFun = (lpAddFun)GetProcAddress(aDll, "add");
if (addFun != NULL)
{
int result = addFun(2, 3);
cout<<result;
}
FreeLibrary(aDll);
}
return 0;
}
貌似DLL并没有被调用
调用方式有问题?
谢谢
这是一条镜像帖。来源:北邮人论坛 / soft-design / #31784同步于 2008/12/16
该镜像源已超过 30 天没有更新,可能在源站已被删除。
SoftDesign机器人发帖
【求助】谢谢, DLL操作
nonothing
2008/12/16镜像同步37 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
你在main.cpp里至少要声明一下add函数啊。。。
声明它是一个导入函数。。否则编译main.cpp的时候,怎么会知道add是个什么玩意
lib.h里不是声明了么
【 在 rebirthatsix 的大作中提到: 】
: 你在main.cpp里至少要声明一下add函数啊。。。
: 声明它是一个导入函数。。否则编译main.cpp的时候,怎么会知道add是个什么玩意
【 在 flyingmiao 的大作中提到: 】
: lib.h里不是声明了么
是呀,我想做个DLL连接 ,怎么搞都用不了,完全搞不清出哪里有问题
【 在 flyingmiao 的大作中提到: 】
: lib.h里不是声明了么
...你看她的头文件包含
#include "..\lib.h"
这个能是lib工程里的头文件么。。
另外,你所提到的所有问题我没觉得和动态链接库有什么必然联系,感觉完全是个静态库。。
如果你上面的工程是个dll project,连个导出函数都没有,何来的.lib生成?
相当混乱啊。。先搞清楚自己到底是要写个动态链接库还是静态库吧。。
【 在 rebirthatsix 的大作中提到: 】
: ...你看她的头文件包含
: #include "..\lib.h"
: 这个能是lib工程里的头文件么。。
#include "..\lib.h" 是示例一下,用的是那个文件的存放路径
编译没有错误
【 在 rebirthatsix 的大作中提到: 】
: 另外,你所提到的所有问题我没觉得和动态链接库有什么必然联系,感觉完全是个静态库。。
: 如果你上面的工程是个dll project,连个导出函数都没有,何来的.lib生成?
: 相当混乱啊。。先搞清楚自己到底是要写个动态链接库还是静态库吧。。
而且我确实生成了一个LIB但是用不了
【 在 nonothing 的大作中提到: 】
: #include "..\lib.h" 是示例一下,用的是那个文件的存放路径
你确定你是要写动态链接库?