[Delphi] DLL too Application Communication

liunx

Guest
Ok, this is my current project:<br /><br />I am working on a desktop manager using the WINAPI: CreateDesktop() and its counterparts.<br /><br />The step I am at is fully closing a desktop, by the only means is to close all programs currently running in it.<br /><br />To do this, I needed to find out what processes were running under what desktop.<br /><br />I found out that using GetStartUpInfo API, I could determine this. The only problem I found that it is for a local process, not remote. So I decided to code a dll that I would inject and have it relay the information to the main application.<br /><br />This is the code for the main application:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><br /><br />var<br />  Form1 : TForm1;<br />  Importing : String;<br />  WM_DLLMESSAGE : DWord;<br /><br />...<br />...<br /><br />type<br />  TWMDLLData = record<br />    Msg: Cardinal;<br />    Handle: HWND;<br />    Info: Longint;<br />    Result: Longint;<br />  end;<br /><br />...<br />...<br /><br />procedure TForm1.ViewRunning1Click(Sender: TObject);<br />var<br />  pID : Array[0..1023] Of DWORD;<br />  Current : DWORD;<br />  I : Integer;<br />  ProcCount : Integer;<br />  hProcess : THandle;<br />  hMod : HMODULE;<br />  ModuleName : Array[0..300] Of Char;<br />  Debug : TStartupInfo;<br />  _DesktopName : PChar;<br />  DesktopName : String;<br />begin<br />  EnumProcesses(@pID,SizeOf(pID),Current);<br />  ProcCount := Current DIV SizeOf(DWORD);<br />  GetStartupInfo(Debug);<br />  _DesktopName := Debug.lpDesktop;<br />  DesktopName := _DeskTopName;<br />  For I := 0 To ProcCount - 1 Do<br />    begin<br />    hProcess := OpenProcess(Process_all_access,False,pID);<br />    If hProcess > 0 Then<br />      begin<br />      EnumProcessModules(hProcess,@hMod,SizeOf(hMod),Current);<br />      GetModuleFileNameEx(hProcess,hMod,ModuleName,SizeOf(ModuleName));<br />      Importing := '';<br />      If InjectLibrary(hProcess,PChar(ExtractFilePath(ParamStr(0)) + 'DesktopAPI.dll')) Then<br />        begin<br />        While Importing = '' Do<br />          begin<br />          Application.ProcessMessages;<br />        end;<br />        UninjectLibrary(hProcess,PChar(ExtractFilePath(ParamStr(0)) + 'DesktopAPI.dll'));<br />        If Importing = DesktopName Then<br />          begin<br />          Form4.ValueListEditor1.InsertRow(ExtractFileName(ModuleName),IntToStr(pID),True);<br />        end;<br />      end;<br />    end;<br />    CloseHandle(hProcess);<br />  end;<br />  Form4.Show;<br />end;<br /><br />procedure TForm1.WMDLLData(var Msg: TWMDLLData);<br />begin<br />  Importing := String(Msg.Info);<br />end;<br /><br />procedure TForm1.DefaultHandler(var Message);<br />var<br />  ee: TWMDLLData;<br />begin<br />  with TMessage(Message) do<br />  begin<br />    if (Msg = WM_DLLMESSAGE) then<br />    begin<br />      ee.Msg    := Msg;<br />      ee.Handle := wParam;<br />      ee.Info   := lParam;<br />      if ee.Handle <> Handle then<br />        WMDLLData(ee);<br />    end<br />    else<br />      inherited DefaultHandler(Message);<br />  end;<br />end;<br /><br />Initialization<br /> WM_DLLMESSAGE := RegisterWindowMessage('Sending Data'); <br /><!--c2--></div><!--ec2--><br /><br />The DLL source is as follows:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><br />library DesktopAPI;<br /><br />uses<br />  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br />  Dialogs, StdCtrls, ExtCtrls;<br /><br />{$R *.res}<br />var<br />  WM_DLLMESSAGE : DWORD;<br /><br />type<br />  TWMDLLData = record<br />    Msg: Cardinal;<br />    Handle: HWND;<br />    Info: Longint;<br />    Result: Longint;<br />  end;<br /><br />procedure EntryPoint(Reason: LongWord);<br />var<br />  Debug :  TStartupInfo;<br />  Data : TCopyDataStruct;<br />  pDesktop : PChar;<br />  Tmp2 : String;<br />begin<br />  If Reason = 1 Then<br />    begin<br />    GetStartUpInfo(Debug);<br />    pDesktop := Debug.lpDesktop;<br />    Data.dwData := 0;<br />    Data.cbData := 1 + Length(pDesktop);<br />    Data.lpData := pDesktop;<br />    Tmp2 := pDesktop;<br />    SendMessage(HWND_Broadcast,WM_DLLMESSAGE,0,Integer(Tmp2));<br />  end;<br />end;<br /><br />begin<br />  WM_DLLMESSAGE := RegisterWindowMessage('Sending Data');<br />  DLLProc := @EntryPoint;<br />  EntryPoint(1);<br />end.<br /><!--c2--></div><!--ec2--><br /><br />The problem is that whenever I run the main procedure and start the process, the computer does a hard reboot.<br /><br />I am not really sure where to go from this point, as trying anything new gets me another crash. So I thought eye's other than my own might catch something I did wrong.
</div>
 
Top