| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1276 人关注过本帖
标题:Delphi调用C++编写的DLL
只看楼主 加入收藏
yaoweiwei082
Rank: 1
来 自:云南昆明
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-3-18
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
Delphi调用C++编写的DLL
第一步:用C++编写的调用系统时间的源码:
Time.h源码:
extern "C" _declspec(dllexport) void  main();
Time.cpp源码:
//调用系统时间
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "Time.h"

extern "C" _declspec(dllexport) void  main()
{
        struct tm *newtime;
        char am_pm[] = "AM";
        time_t long_time;

        time( &long_time );     /* Get time as long integer. */
        newtime = localtime( &long_time );
                                /* Convert to local time. */

        if( newtime->tm_hour > 12 )  /* Set up extension. */
                strcpy( am_pm, "PM" );
        if( newtime->tm_hour > 12 )  /* Convert from 24-hour */
                newtime->tm_hour -= 12;   /*   to 12-hour clock.  */
        if( newtime->tm_hour == 0 ) /*Set hour to 12 if midnight. */
                newtime->tm_hour = 12;
        printf( "%.19s %s\n", asctime( newtime ), am_pm );
}
然后编译生成了获取系统时间的Time.dll动态库文件。
第二步:在Delphi中调用这个Time.dll文件。
在Form1中,我放置了一个Button1和一个Edit1,当单击按钮时,触发事件,调用Time.dll,将系统时间显示是Edit1中,如何实现???
其中按钮事件代码如下:
procedure TForm1.Button1Click(Sender: TObject);
var
Handle:THandle;
main:Tmain;

begin
Handle:=LoadLibrary('Time.dll'); //将“Time.dll”的文件映象映射进调用进程的地址空间
if Handle<>0 then
begin
@main:=GetProcAddress(Handle,'main'); //取得DLL中函数main( )的地址
if (@main<>nil) then
begin
//edtMin.Text:=IntToStr(main(StrToInt(edtInt1.Text),
Edit1.Text:=IntToStr(main());
end
else
ShowMessage('调用函数“GetProcAddress”时出错!');
FreeLibrary(Handle); //从进程的地址空间中解除“Time.dll”文件的映射
end
end;
end.
能通过编译,但结果并没有显示出系统时间。是怎么回事?还望有人能够指点一二,谢谢。
搜索更多相关主题的帖子: Delphi 编写 DLL 
2010-03-18 19:41
yeye55
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:66
专家分:152
注 册:2007-1-19
收藏
得分:20 
void  main()

main没有返回值。

我的百度空间→http://hi.baidu.com/yeye55
2010-03-19 18:53
快速回复:Delphi调用C++编写的DLL
数据加载中...
 
   



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

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