怎样调用一个Dll中函数,只知道函数和参数描述。

shaken 2005-01-09 10:13:46
给个例程,没有例程不给分。
...全文
405 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
AntonlioX 2005-05-21
  • 打赏
  • 举报
回复
up
shaken 2005-01-13
  • 打赏
  • 举报
回复
也不行,我改过了,都是同样的错误。
slience 2005-01-12
  • 打赏
  • 举报
回复
你现在是在VC下调用出错吗?可以把下面这条语句
typedef int (CALLBACK * FtpType)(LPSTR,LPINT,LPSTR)
改成
typedef int (WINAPI * FtpType)(LPSTR,LPINT,LPSTR)
试试!

可能是调用约定出错!
shaken 2005-01-11
  • 打赏
  • 举报
回复
不是warning,而是程序运行的时候提示写内存错误。
shaken 2005-01-11
  • 打赏
  • 举报
回复
不是的,我的dll函数在delphi下调用成功。
oyljerry 2005-01-11
  • 打赏
  • 举报
回复
是不是这个函数执行的问题
oyljerry 2005-01-11
  • 打赏
  • 举报
回复
哦,没看清楼主的定义,不用改成指针
oyljerry 2005-01-11
  • 打赏
  • 举报
回复
FtpType* FtpTypePtr = NULL;
这里用指针类型
shaken 2005-01-10
  • 打赏
  • 举报
回复
这个程序老是提示错误,但是编译通过,不知道错再哪里?
shaken 2005-01-10
  • 打赏
  • 举报
回复
// art.c
// A simple program that uses LoadLibrary and
// GetProcAddress to access FindArtist function from art.dll.
#include <stdio.h>
#include <windows.h>

//Define the function prototype
//CALLBACK
typedef int (CALLBACK * FtpType)(LPSTR,LPINT,LPSTR);

void main(void)
{
BOOL freeResult, runTimeLinkSuccess = FALSE;
HINSTANCE dllHandle = NULL;
FtpType FtpTypePtr = NULL;

//Load the dll and keep the handle to it
dllHandle = LoadLibrary("ftpserv.dll");

// If the handle is valid, try to get the function address.
if (NULL != dllHandle)
{
//Get pointer to our function using GetProcAddress:
FtpTypePtr = (FtpType)GetProcAddress(dllHandle,
"FtpFile");

// If the function address is valid, call the function.
if (runTimeLinkSuccess = (NULL != FtpTypePtr))
{
LPINT iRetCode =NULL;
LPSTR cRetMsg =NULL;
LPSTR myArtist = "A.txt";

int retVal = FtpTypePtr(myArtist,iRetCode,cRetMsg);
}

//Free the library:
freeResult = FreeLibrary(dllHandle);
}

//If unable to call the DLL function, use an alternative.
if(!runTimeLinkSuccess)
printf("message via alternative method\n");
}
shaken 2005-01-10
  • 打赏
  • 举报
回复
就是GetProcAddress() -> * MyFun ,对吧。
slience 2005-01-10
  • 打赏
  • 举报
回复
因为LoadLibray()将DLL加载到当前的应用程序中并且返回当前DLL文件的句柄,而GetProcAddress()函数通过该句柄获取导入到应用程序中的函数指针,定义一个指向相同函数的指针就可以在程序中自由的使用该函数。
aoosang 2005-01-10
  • 打赏
  • 举报
回复
typedef int (WINAPI * MyFun)(HWND,LPCTSTR,LPCTSTR,UINT);

定义了一个函数指针,当加载动态库以后hHandle=LoadLibrary("user32.dll");,
通过Myfun获取指定函数的地址,上面的例子是用myfun获取MessageBoxA这个函数的地址
shaken 2005-01-10
  • 打赏
  • 举报
回复
为什么都要先定义函数原型?
typedef ……
oyljerry 2005-01-10
  • 打赏
  • 举报
回复
什么错误?
能编译通过,那就是warning了 ^_^
kugou123 2005-01-09
  • 打赏
  • 举报
回复
void CLoadLibraryTestDlg::OnLoad()
{
typedef int (WINAPI * MyFun)(HWND,LPCTSTR,LPCTSTR,UINT);
MyFun fun=NULL;
HINSTANCE hHandle;
hHandle=LoadLibrary("user32.dll");
fun=(MyFun)::GetProcAddress(hHandle,"MessageBoxA");
if(fun!=NULL)
{
fun(NULL,"hahahahahahahaha","success :)",MB_OK);
}
}
kugou123 2005-01-09
  • 打赏
  • 举报
回复
使用显式加载
先LoadLibrary
再GetProcAddress取函数地址
EnochShen 2005-01-09
  • 打赏
  • 举报
回复
上面的代码少了一行
typedef IcmpSendEcho* lpIcmpSendEcho;
Yanbin_Q 2005-01-09
  • 打赏
  • 举报
回复
用LoadLibrary方法就不须要使用编译动态库生成的dll.lib文件了

但是必须保证输出方法前一定要有

extern "C"

或者自己定义有def文件指定输出函数

否则调用就不会成功的
Yanbin_Q 2005-01-09
  • 打赏
  • 举报
回复
还有更好控制的方式就是LoadLibrary方法,不过就是要包含windows.h头文件,
还是用上面那个输出方法,调用代码如下:


#include <WINDOWS.H>

int main(int argc, char* argv[])
{
HINSTANCE gLibSample=NULL;

typedef int(FNDLL)(void);
FNDLL *execFnDll;
gLibSample=LoadLibrary("dll.dll");

//返回DLL中ShowMe()函数的地址
execFnDll=(FNDLL*) GetProcAddress(gLibSample,"fnDll");

printf("fnDll return %d\n",execFnDll());
FreeLibrary(gLibSample);
return 0;
}
加载更多回复(2)

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧