function MouseHookProc(iCode:LongInt;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
  begin
       Result:=0;       if iCode<0 then Result:=CallNextHookEx(NextHook,iCode,wParam,lParam);
       if wParam=WM_MOUSEMOVE then begin
          pShare^.MouseInfoStruct:=PMOUSEHOOKSTRUCT(lParam)^;
          GetWindowText(pShare^.MouseInfoStruct.hwnd, pShare^.WindowText,1024);
          SendMessage(pShare^.Data[1],pShare^.Data[2],wParam,lParam); //单步执行到这里出错
          //错误信息为:Project project1.exeraised exception class exteranl 'sigsegv'
          //请前辈们指点,谢谢!
       end;
  end; pShare的数据类型定义在:unit MemoryShare;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils,Windows;
const MappingFileName='MouseHookMappingFile';
type
    TMemoryShare=record
       {Sender:DWORD;
       MessageID:DWORD;}
       Data:array[1..2] of DWORD;
       MouseInfoStruct:TMOUSEHOOKSTRUCT;
       WindowText:array[0..1024] of char;
    end;
    PMemoryShare =^TMemoryShare;
implementation
end.             

解决方案 »

  1.   

    下面是完整工程
    MouseHook.Dll工程unit MouseHookDll;{$mode delphi}{$H+}interfaceuses
      Classes, SysUtils,Windows,Messages,MemoryShare;
      var
         pShare:PMemoryShare;
         NextHook:LRESULT;
         FileMappingHandle:HANDLE;
      function StartHook(sender:HWND;MessageID:DWORD):BOOL;stdcall;
      function StopHook():BOOL;stdcall;
      function MouseHookProc(iCode:LongInt;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
      procedure CreateOfOpenMaping();
    implementation
      function MouseHookProc(iCode:LongInt;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
      begin
           Result:=0;       if iCode<0 then Result:=CallNextHookEx(NextHook,iCode,wParam,lParam);
           if wParam=WM_MOUSEMOVE then begin
              pShare^.MouseInfoStruct:=PMOUSEHOOKSTRUCT(lParam)^;
              GetWindowText(pShare^.MouseInfoStruct.hwnd, pShare^.WindowText,1024);
              SendMessage(pShare^.Data[1],pShare^.Data[2],wParam,lParam);
           end;
      end;
      {挂钩,参数说明sender为调用本Dll的窗口句柄,MessageID为自定义消息}
      function StartHook(sender:HWND;MessageID:DWORD):BOOL;stdcall;begin
          CreateOfOpenMaping();
          Result:=False;
          pShare^.Data[1]:=sender;
          pShare^.Data[2]:=MessageID;
          NextHook:=SetWindowsHookEx(WH_MOUSE,MouseHookProc,GetModuleHandle(nil),0);
          Result:=NextHook<>0;
      end;
      {脱钩}
      function StopHook():BOOL;stdcall;begin
          if NextHook<>0 then begin
             UnHookWindowsHookEx(NextHook);
             NextHook:=0;
          end;
          Result:=NextHook=0;
      end;
      procedure CreateOfOpenMaping();begin
        NextHook:=0;
        FileMappingHandle:=OpenFileMapping(FILE_MAP_WRITE,False,MappingFileName);
        if FileMappingHandle=0 then begin
           FileMappingHandle:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TMemoryShare),MappingFileName);
        end else begin
           Exception.Create('不能创建共享内存');
        end;
        pShare:=MapViewOFFile(FileMappingHandle,FILE_MAP_READ or FILE_MAP_WRITE,0,0,0);
        if pShare=nil then begin
           CloseHandle(FileMappingHandle);
           Exception.Create('不能映射共享内存');
        end;
      end;{DLL初始化时候执行的动作}
    initialization
        {NextHook:=0;
        FileMappingHandle:=OpenFileMapping(FILE_MAP_WRITE,False,MappingFileName);
        if FileMappingHandle=0 then begin
           FileMappingHandle:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TMemoryShare),MappingFileName);
        end else begin
           Exception.Create('不能创建共享内存');
        end;
        pShare:=MapViewOFFile(FileMappingHandle,FILE_MAP_READ or FILE_MAP_WRITE,0,0,0);
        if pShare=nil then begin
           CloseHandle(FileMappingHandle);
           Exception.Create('不能映射共享内存');
        end;}
    {DLL释放时候执行的动作}
    finalization
        UnMapViewOfFile(pShare);
        CloseHandle(FileMappingHandle);
    end.
    unit MemoryShare;{$mode objfpc}{$H+}interfaceuses
      Classes, SysUtils,Windows;
    const MappingFileName='MouseHookMappingFile';
    type
        TMemoryShare=record
           {Sender:DWORD;
           MessageID:DWORD;}
           Data:array[1..2] of DWORD;
           MouseInfoStruct:TMOUSEHOOKSTRUCT;
           WindowText:array[0..1024] of char;
        end;
        PMemoryShare =^TMemoryShare;
    implementationend.主程序:
    [code=Delphi(Pascal)]
    unit MouseHookMain;{$mode delphi}{$H+}interfaceuses
      Classes,Windows,Messages, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
      StdCtrls,MemoryShare;type  { TForm1 }  TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Edit1: TEdit;
        Label1: TLabel;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { private declarations }
      public
        { public declarations }
        procedure WndProc(var TheMessage : TMessage); override;
      end; var
      Form1: TForm1;
      pShare:PMemoryShare;
      FileMappingHandle:HANDLE;
      const MessageID=WM_USER+100;
    implementation
        function StartHook(sender:HWND;MessageID:DWORD):BOOL;stdcall;external 'MouseHook.DLL';
        function StopHook():BOOL;stdcall;external  'MouseHook.DLL';    procedure TForm1.Button1Click(Sender: TObject);begin
            StartHook(Form1.Handle,MessageID);
        end;    procedure TForm1.Button2Click(Sender: TObject);begin
            StopHook();
        end;    procedure TForm1.FormCreate(Sender: TObject);begin
            pShare:=nil;
        end;    procedure TForm1.WndProc(var TheMessage : TMessage);begin
            FileMappingHandle:=OpenFileMapping(FILE_MAP_WRITE,False,MappingFileName);
            if FileMappingHandle=0 then begin
               Exception.Create('不能打开共享内存');
            end;
            pShare:=MapViewOfFile(FileMappingHandle,FILE_MAP_READ OR FILE_MAP_WRITE,0,SizeOf(TMemoryShare),0);
            if pShare=nil then begin
               Exception.Create('不能映射共享内存');
            end;
            if TheMessage.Msg=MessageID then begin
                Edit1.Text:=string(pShare^.WindowText);
            end else begin
                inherited;
            end;
        end;initialization
      {$I MouseHookMain.lrs}end.
    [/code]
      

  2.   

    SIGSEGV --- Segment Fault. The possible cases of your encountering this error are: 
    1.buffer overflow --- usually caused by a pointer reference out of range. 
    2.stack overflow --- please keep in mind that the default stack size is 8192K. 
    3.illegal file access --- file operations are forbidden on our judge system.SIGSEGV(Segment fault)意味着指针所对应的地址是无效地址,没有物理内存对应该地址。
      

  3.   

    问题就在这一句SendMessage(pShare^.Data[1],pShare^.Data[2],wParam,lParam);
    实在想不透了,请前辈们帮帮忙吧。
    我修改成这样样子又没有问题,可以确定是pShare^.Data[1]的问题,可是我又找不到是什么原因。
    SendMessage(0,pShare^.Data[2],wParam,lParam);
      

  4.   

    pShare:PMemoryShare;是指针吧?你在
    procedure TForm1.FormCreate(Sender: TObject);begin
       pShare:=nil;
    end;
    没有分配内存,导致指针非法。#include <iostream.h>
    typedef struct test 
    {
    int a;
    int b;
    }*PTEST,TEST;void main()
    {
    //   PTEST p = new TEST;
    PTEST p;
       p->a = 2;
       p->b = 3;
       cout<<p->a<<"==="<<p->b<<endl;
    //   delete p;
    }你的和如上C++代码类似,不new不行
      

  5.   

    本质的问题我找出来了,是这一句Edit1.Text:=string(pShare^.WindowText);
    想不明白为什么出错了。
      

  6.   

    pShare没分配内存吧?指针合法?
      

  7.   

    pShare是指向一个共享内存的,其它的值都可以读得到。