第一:在动态链接库中如何声明消息处理句柄方法
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls, ComCtrls;
  const WM_COMMNOTIFY=WM_APP+1;//通信消息
procedure WMCOMMNOTIFY(var message:TMessage); message  WM_COMMNOTIFY;export;
我想在dll中声明WMCOMMNOTIFY这一消息处理函数时就不行,错误提示message不能声明.
但如果我不是在dll中声明,而是放到主程序中声明的话就没有问题.第二:如何将该消息处理过程的结果传递给主程序(主程序并不知道该消息处理过程什么时候会传数据过来).
我的消息处理过程如下:
procedure WMCOMMNOTIFY(var message: TMessage);
var commstate :comstat;
    dwnumberofbytesread:dword;
    errorflag:dword;
    inputbuffer:array[0..1024] of char;
begin
 if not clearcommerror(pubhnewcommfile,errorflag,@commstate) then
 begin
  messagebox(0,'clearcommerror!','notice',mb_ok);
  purgecomm(pubhnewcommfile,purge_rxabort or purge_rxclear);
  exit;
 end ;
 if (commstate.cbInQue >0) then
 begin
  fillchar(inputbuffer,commstate.cbInQue,#0);
  if (not readfile(pubhnewcommfile,inputbuffer,commstate.cbInQue,
                   dwnumberofbytesread,@pubread_os)) then
  begin
   errorflag:=getlasterror();
   if (errorflag<>0) and (errorflag<>error_io_pending) then
   begin
    messagebox(0,'readfile error','notice',mb_ok);
    pubreceive:=false;
    closehandle(pubread_os.hEvent);
    closehandle(pubPost_Event);
    closehandle(pubhnewcommfile);
    exit;
   end
   else
   begin
    waitforsingleobject(pubhnewcommfile,infinite);
    getoverlappedresult(pubhnewcommfile,pubread_os,dwnumberofbytesread,false);
   end ;
  end;//end if (not readfile (.....
  if dwnumberofbytesread>0 then
  begin
   pubread_os.Offset :=pubread_os.Offset+dwnumberofbytesread;
   pubreceivedata:=pubread_os.Offset ;
   addtoreadstring(inputbuffer,dwnumberofbytesread);
  end;//end if dwnumberofbytesread>0 then
 end;
 setevent(pubPost_Event);
end;procedure AddToReadstring(str:pchar;len:dword);
begin
 str[len]:=#0;
 //str 即为我想传递给主程序的.那么主程序中应该如何来接收该str呢?
end;望根据本例举例说明,不甚感激.
不会的up也有分.EMAIL:[email protected]

解决方案 »

  1.   

    在dll里不行吧,应用程序的窗口函数根本无法去处理
    学习
    顶一下
      

  2.   

    你可以把application.handle 传给你的dll 而后再用sendmessage() 当然你要自已定义一个消息的。最后在你的application中处理你的这个消息不就可以了吗?
      

  3.   

    我会考虑交给Application处理
    不过我对消息机制也不是太通透,希望有这方面的高手能就Delphi的消息发送体系谈谈。
    一般而言,在Delphi中分以下几级:
    MainWndProc
    WndProc
    Dispatch
    MessageHandler
    Method
    Event Handler
    但至今没弄明白这几级分别对应编程时采用的什么方法?以及在什么时候该采用什么方法?就象楼主这种问题一般用什么方法?有几种可选方法?
    如果有高手肯指点,我愿赞助100分!
      

  4.   

    现在的问题的:
    第一:在动态链接库中能否使用消息,该如何使用(我想在DLL声明一个消息处理函数都不行).
    第二:如何将DLL中消息处理函数得出的数据(暂定为字符串吧)传递给主程序.
    我试着在主程序中也定义了一消息用来接收DLL中传来的数据.但不知如何才能接收.
    望指点.
    EMAIL:  [email protected]
      

  5.   

    要在dll和主程序间传递字符串要加sharemem单元
      

  6.   

    我以前用过主程序从DLL中取回数据的问题,有2种方法:
    1:把你定义的消息定义为系统级消息,或者同时在主窗口和DLL中定义该消息。
    2:DLL回调。
    但我个人觉的DLL回调最好用方便。这种方法的使用在《DELPHI5 开发大全》有详细的说明。当然,如果你不动过程类型转换和回调函数,那就不 能用这方法了。