用Delphi编写一个Svchost.exe调用的DLL模块(显示FORM出错...) ( 积分: 100 )

J

jack011

Unregistered / Unconfirmed
GUEST, unregistred user!
窗体Msg的信息:
unit Unit_Msg;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TFmMsg = class(TForm)
BitBtn1: TBitBtn;
StaticText1: TStaticText;
procedure BitBtn1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure StaticText1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
FmMsg: TFmMsg;
implementation
{$R *.dfm}
procedure TFmMsg.BitBtn1Click(Sender: TObject);
begin
close;
end;

procedure TFmMsg.FormShow(Sender: TObject);
begin
//self.Left := 700;
//self.Top := 530;
end;

在Svchost.exe中调用FORM程序:
function ShowMsgForm(): Pchar;
stdcall;
begin
try
FmMsg := TFmMsg.create(Application);
FmMsg.Show;
//finally
// FmMsg.free;
except
log('c:/clog.txt', '无法创建窗体Msg!');
exit;
end;
end;

主要程序参照以下高人的代码,但是窗体Msg无法显示出来,请问各位大虾,问题出在哪里?

用Delphi编写一个Svchost.exe调用的DLL模块

作者:west3316 提交日期:2006-9-24 11:12:00
作者: Maco
?? 这个模块的代码在网上流传的是用C写的,这里我花了一个早上用Delphi写了一个DLL,可以自己扩充各种功能.
??
代码:
??{
?? 文件名: ServiceDll.dpr
?? 概述: 替换由svchost.exe启动的某个系统服务,具体服务由全局变量 ServiceName 决定.
??
?? 经测试,生成的DLL文件运行完全正常.
?? 测试环境: Windows 2003 Server + Delphi 7.0
??
?? 代码只实现了一个框架,没有任何实际动作,仅作为学习用.如果你使用本代码
?? 进行了任何扩充和修改,希望您能将代码寄一份给我.
??
?? 日期: 2005-04-01
?? 作者: yanxizhen yanxizhen#163.com
??}
??
??library ServiceDll;
??
??uses
?? SysUtils,
?? Classes,
?? winsvc,
?? System,
?? Windows;
??
??{ 定义全局变量 }
??var
?? // 服务控制信息句柄
?? SvcStatsHandle : SERVICE_STATUS_HANDLE;
?? // 存储服务状态
?? dwCurrState : DWORD;
?? // 服务名称
?? ServiceName : PChar = 'BITS';
??
??{ 调试函数,用于输出调试文本 }
??procedure OutPutText(CH:PChar);
??var
?? FileHandle: TextFile;
?? F : Integer;
??begin
?? try
?? if not FileExists('zztestdll.txt') then
?? F := FileCreate('zztestdll.txt');
?? finally
?? if F > 0 then
 FileClose(F);
?? end;
??
?? AssignFile(FileHandle,'zztestdll.txt');
?? Append(FileHandle);
?? Writeln(FileHandle,CH);
?? Flush(FileHandle);
?? CloseFile(FileHandle);
??end;
??
??
??{ dll入口和出口处理函数 }
??procedure DLLEntryPoint(dwReason : DWord);
??begin
??
?? case dwReason of
?? DLL_PROCESS_ATTACH:
?? ;
?? DLL_PROCESS_DETACH:
?? ;
?? DLL_THREAD_ATTACH:
?? ;
?? DLL_THREAD_DETACH:
?? ;
?? end;
??end;
??
??{ 与SCM管理器通话 }
??function TellSCM(dwState : DWORD ;
dwExitCode : DWORD;
dwProgress : DWORD ): LongBool;
??var
?? srvStatus : service_status;
??begin
?? srvStatus.dwServiceType := SERVICE_WIN32_SHARE_PROCESS;
?? dwCurrState := dwState;
?? srvStatus.dwCurrentState := dwState;
?? srvStatus.dwControlsAccepted := SERVICE_ACCEPT_STOP or SERVICE_ACCEPT_PAUSE_CONTINUE or SERVICE_ACCEPT_SHUTDOWN;
?? srvStatus.dwWin32ExitCode := dwExitCode;
?? srvStatus.dwServiceSpecificExitCode := 0;
?? srvStatus.dwCheckPoint := dwProgress;
?? srvStatus.dwWaitHint := 3000;
?? Result := SetServiceStatus( SvcStatsHandle, srvStatus );
??end;
??
??{ Service 控制函数 }
??PROCEDURE servicehandler(fdwcontrol:integer);
STDCALL;
??begin
??
?? CASE fdwcontrol OF
??
?? SERVICE_CONTROL_STOP:
?? begin
?? TellSCM( SERVICE_STOP_PENDING, 0, 1 );
?? Sleep(10);
?? TellSCM( SERVICE_STOPPED, 0, 0 );
?? end;
??
?? SERVICE_CONTROL_PAUSE:
?? begin
?? TellSCM( SERVICE_PAUSE_PENDING, 0, 1 );
?? TellSCM( SERVICE_PAUSED, 0, 0 );
?? end;
??
?? SERVICE_CONTROL_CONTINUE:
?? begin
?? TellSCM( SERVICE_CONTINUE_PENDING, 0, 1 );
?? TellSCM( SERVICE_RUNNING, 0, 0 );
?? end;
??
?? SERVICE_CONTROL_INTERROGATE:
?? TellSCM( dwCurrState, 0, 0 );
?? 
?? SERVICE_CONTROL_SHUTDOWN:
?? TellSCM( SERVICE_STOPPED, 0, 0 );
??
?? end;
??
??end;
??
??
??{ service main }
??procedure ServiceMain(argc : Integer;
VAR argv : pchar );
StdCall;
??begin
?? { try
?? begin
?? if ParamStr(1) <> '' then
?? svcname := strNew(PChar(ParamStr(1)))
?? else
?? begin
?? svcname := strAlloc(10 * Sizeof(Char));
?? svcname := 'none';
?? end;
?? OutPutText(svcname);
?? end
?? finally
?? strdispose(svcname);
?? end;
?? }
??
?? // 注册控制函数
?? SvcStatsHandle := RegisterServiceCtrlHandler(ServiceName, @servicehandler);
?? IF (SvcStatsHandle = 0) then
?? begin
?? OutPutText('Error in RegisterServiceCtrlHandler');
?? exit;
?? END
?? else
?? begin
?? FreeConsole();
?? end;
??
?? // 启动服务
?? TellSCM( SERVICE_START_PENDING, 0, 1 );
?? TellSCM( SERVICE_RUNNING, 0, 0 );
?? OutPutText('Service is Running');
??
?? // 这里可以执行我们真正要作的代码
?? while ((dwCurrState <> SERVICE_STOP_PENDING) and (dwCurrState <> SERVICE_STOPPED))do
?? begin
        if not FmMsg.Showing then
        ShowMsgForm;
    //显示FORM,有问题,如何解决呢????????????????谢谢!
??    sleep(1000);
?? end;
??
?? OutPutText('Service Exit');
?? 
??end;
??
??
??// 导出函数列表
??exports
?? ServiceMain;
??
??{ dll入口点 }
??begin
?? DllProc := @DLLEntryPoint;
??end.
 
TRzMarqueeStatus
這個是可以的.
 
广告做成gif,flash.等等
你的客户端只要是个播放器就成了。
有时候换条路子解决问题效果也不错。
 
是不是可以用时间控件来做呢!
 
unit ScrollText;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
StdCtrls,LKJTypes;
type
TScrollThread = class(TThread)
protected
procedure Draw;
procedure Execute;
override;
public
FDelay:cardinal;
FOnDraw:TNotifyEvent;
end;

TScrollText = class(TWinControl)
private
FMemo:TStaticText;
FTimerTag:Integer;
FActive: boolean;
FDelay: cardinal;
FPixel: Integer;
FCurrPos:Integer;
FSelectable: boolean;
FDirection: TDirection;
FScrollSaved:Integer;
Fstrings: TstringList;
FDeja:Cardinal;
FScroll:TScrollThread;
FFont: TFont;
FStartY:Integer;
FDown:Boolean;
procedure SetItems(const Value: TstringList);
procedure OnScroll(Sender: TObject);
procedure SetActive(const Value: boolean);
procedure SetDelay(const Value: cardinal);
procedure SetPixel(const Value: integer);
procedure SetDirection(const Value: TDirection);
procedure CalculeMemo(Sender: TObject);
function GetAlignMent: TAlignment;
procedure Setalignment(const Value: TAlignment);
function GeTColor: TColor;
procedure SeTColor(const Value: TColor);
procedure FontChanged(Sender: TObject);
function GetFont: TFont;
procedure SetFont(const Value: TFont);
procedure MouseD(Sender: TObject;Button: TMouseButton;
Shift: TShiftState;
X,Y: integer);
procedure MouseM(Sender: TObject;Shift: TShiftState;
X, Y: integer);
procedure MouseU(Sender: TObject;Button: TMouseButton;
Shift: TShiftState;
X,Y: integer);
protected
procedure WMSize(var Message: TWMSize);
message WM_SIZE;
public
constructor Create(AOwner: TComponent);override;
destructor Destroy;override;
published
property TextAlignment:TAlignment read GetAlignMent write Setalignment;
property Items:TstringList read Fstrings write SetItems;
property Active:boolean read FActive write SetActive default false;
property Delay:cardinal read FDelay write SetDelay default 50;
property ScrollPixels:Integer read FPixel write SetPixel default 1;
property ScrollDirection:TDirection read FDirection write SetDirection default diBottomToTop;
property BackgroundColor:TColor read GeTColor write SeTColor;
property Font:TFont read GetFont write SetFont;
procedure Pause;
procedure Unpause;
procedure Reset;
property Align;
property ShowHint;
property ParentShowHint;
end;

procedure Register;
implementation
resourcestring
RC_TestText = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
procedure Register;
begin
RegisterComponents('LKJ', [TScrollText]);
end;
{*******************************************************}
procedure TScrollThread.Draw;
begin
if Assigned(FOnDraw) then
FOnDraw(nil)
end;
{*******************************************************}
procedure TScrollThread.Execute;
begin
while not(Terminated)do
begin
SynChronize(Draw);
Sleep(FDelay);
end;
end;
{*******************************************************}

{*******************************************************}
constructor TScrollText.Create(AOwner: TComponent);
begin
inherited;
self.width:=200;
self.height:=150;
FActive:=false;
FDelay:=50;
FPixel:=1;
FCurrPos:=0;
FSelectable:=true;
FDirection:=diBottomToTop;
Fstrings:=TstringList.Create;
FMemo:=TStaticText.Create(self);
FMemo.parent:=self;
FMemo.Width:=self.width;
FMemo.height:=self.height;
FMemo.Borderstyle:=sbsNone;
FMemo.TabStop:=false;
FMemo.Enabled:=FSelectable;
FMemo.AutoSize:=false;
FMemo_OnMouseDown:=MouseD;
FMemo_OnMouseMove:=MouseM;
FMemo_OnMouseUp:=MouseU;
FFont:=TFont.Create;
FFont:=FMemo.Font;
FFont.OnChange:=FontChanged;
FTimertag:=0;
FDown:=False;
FDeja:=Application.HintPause;
FScroll:=TScrollThread.Create(true);
FScroll.FreeOnTerminate:=false;
FScroll.FDelay:=FDelay;
FScroll.FOnDraw:=OnScroll;
end;
{*******************************************************}
function TScrollText.GetFont: TFont;
begin
result:=FMemo.Font;
end;
{*******************************************************}
procedure TScrollText.SetFont(const Value: TFont);
var
FAl:TAlignment;
begin
FMemo.Font.Assign(Value);
CalculeMemo(self);
Fal:=FMemo.Alignment;
if FMemo.Alignment=taCenter then
FMemo.Alignment:=taLeftJustify
else
FMemo.Alignment:=taCenter;
FMemo.Alignment:=FAl;
end;
{*******************************************************}
procedure TScrollText.MouseD(Sender: TObject;Button: TMouseButton;
Shift: TShiftState;
X, Y: integer);
var
p:TPoint;
begin
p.x:=x;p.y:=y;
p:=FMemo.ClientToScreen(p);
if FDirection in [diTopToBottom,diBottomToTop] then
FStartY:=p.y
else
FStartY:=p.x;
FScroll.FOnDraw:=nil;
FDown:=true;
end;
{*******************************************************}
procedure TScrollText.MouseM(Sender: TObject;Shift: TShiftState;
X, Y: integer);
var
NewY:Integer;
p:TPoint;
begin
if FDown then
begin
//if NewY>0, going up, NewY<0, goingdo
wn
p.x:=x;p.y:=y;
p:=FMemo.ClientToScreen(p);
y:=p.y;
x:=p.x;
if FDirection in [diTopToBottom,diBottomToTop] then
begin
NewY:=FStartY-Y;
FStartY:=Y;
FCurrPos:=FCurrPos-NewY;
if FCurrPos<-FMemo.Height then
FCurrPos:=self.height
else
if FCurrPos>self.height then
FCurrPos:=-FMemo.Height;
FMemo.Top:=FCurrPos;
end
else
begin
NewY:=FStartY-x;
FStartY:=x;
FCurrPos:=FCurrPos-NewY;
if FCurrPos<-FMemo.Width then
FCurrPos:=self.width
else
if FCurrPos>self.Width then
FCurrPos:=-FMemo.Width;
FMemo.Left:=FCurrPos;
end;
end;
end;
{*******************************************************}
procedure TScrollText.MouseU(Sender: TObject;Button: TMouseButton;
Shift: TShiftState;
X, Y: integer);
begin
FScroll.FOnDraw := OnScroll;
FDown := False;
end;
{*******************************************************}
destructor TScrollText.Destroy;
begin
FScroll.Terminate;
while not(FScroll.Terminated)do
Application.ProcessMessages;
FScroll.Free;
Application.HintPause := FDeja;
Fstrings.Free;
FMemo.Free;
inherited;
end;
{*******************************************************}
procedure TScrollText.OnScroll(Sender: TObject);
var
t:Integer;
begin
//tag=1 pause
if FTImertag=1 then
begin
if FscrollSaved<=0 then
begin
SetActive(False);
FTimertag:=0;
exit;
end
else
begin
t:=FScrollSaved;
dec(FScrollSaved);
end;
end
else
if FTimertag=2 then
begin
if FScrollSaved>=FPixel then
begin
FTimertag:=0;
t:=FPixel;
end
else
begin
t:=FScrollSaved;
inc(FSCrollSaved);
end;
end
else
t:=FPixel;
//tag=2 unpause
//FDirection
case FDirection of
diTopToBottom :
begin
if FCurrPos>self.height then
FCurrPos:=-FMemo.height
else
FCurrPos:=FCurrPos+t;
FMemo.Top:=FCurrPos;
end;
diLeftToRight :
begin
if -FCurrPos>FMemo.width then
FCurrPos:=self.width
else
FCurrpos:=FCurrPos-t;
Fmemo.left:=FCurrPos;
end;
diRightToLeft :
begin
if FCurrPos>self.width then
FCurrPos:=-self.width
else
FCurrpos:=FCurrPos+t;
Fmemo.left:=FCurrPos;
end;
diBottomToTop :
begin
if -FCurrPos>FMemo.height then
FCurrPos:=self.height
else
FCurrPos:=FCurrPos-t;
FMemo.Top:=FCurrPos;
end;
end;
end;
{*******************************************************}
procedure TScrollText.Pause;
begin
if FActive then
begin
FScrollSaved:=FPixel;
FTimertag:=1;
end;
end;
{*******************************************************}
procedure TScrollText.SetActive(const Value: boolean);
begin
SetItems(Fstrings);
FActive:=Value;
if Value then
FScroll.Resume
else
FScroll.Suspend;
end;
{*******************************************************}
procedure TScrollText.SetDelay(const Value: cardinal);
begin
if Value>Fdeja then
Application.hintpause:=Fdeja
else
if Value>10 then
Application.HintPause:=Value-10
else
Application.hintpause:=abs(Value-1);
FDelay:=Value;
FScroll.FDelay:=Value;
end;
{*******************************************************}
procedure TScrollText.SetDirection(const Value: TDirection);
begin
FDirection := Value;
FMemo.left:=0;
FMemo.top:=0;
case FDirection of
diTopToBottom : FCurrpos:=self.height;
diLeftToRight : FCurrpos:=-self.width;
diRightToLeft : FCurrpos:=self.width;
diBottomToTop : FCurrpos:=-Fmemo.height;
end;
end;
{*******************************************************}
procedure TScrollText.CalculeMemo(Sender: TObject);
var
i,j:Integer;
ts:TstringList;
begin
//calculate the size of the memo (vertically)
with TCanvas.Createdo
begin
Handle:=GetDc(0);
Font.Assign(FMemo.Font);
j:=0;
ts:=TstringList.Create;
ts.Text:=FMemo.Caption;
for i:=0 to ts.Count-1do
try
if ts<>'' then
j:=j+TextHeight(ts)
else
j:=j+TextHeight(RC_TestText);
except
end;
if j<=0 then
j:=self.Height;
FMemo.Height:=j;
ReleaseDc(0,handle);
ts.free;
free;
end;
if FMemo.height<self.height then
FMemo.height:=self.height;
case FDirection of
diTopToBottom : FCurrpos:=self.height;
diLeftToRight : FCurrpos:=-self.width;
diRightToLeft : FCurrpos:=self.width;
diBottomToTop : FCurrpos:=-Fmemo.height;
end;
end;
{*******************************************************}
procedure TScrollText.SetItems(const Value: TstringList);
begin
Fstrings.Text:=Value.Text;
FMemo.caption:=Value.text;
CalculeMemo(self);
end;
{*******************************************************}
function TScrollText.GeTColor: TColor;
begin
result:=FMemo.Color;
end;
{*******************************************************}
procedure TScrollText.SeTColor(const Value: TColor);
begin
FMemo.Color:=Value;
self.color:=Value;
invalidate;
end;
{*******************************************************}
procedure TScrollText.FontChanged(Sender: TObject);
var
FAl:TAlignment;
begin
CalculeMemo(self);
Fal:=FMemo.Alignment;
if FMemo.Alignment=taCenter then
FMemo.Alignment:=taLeftJustify
else
FMemo.Alignment:=taCenter;
FMemo.Alignment:=FAl;
end;
{*******************************************************}
procedure TScrollText.SetPixel(const Value: integer);
begin
FPixel := Value;
end;
{*******************************************************}
procedure TScrollText.Reset;
begin
case FDirection of
diTopToBottom : FCurrpos:=self.height;
diLeftToRight : FCurrpos:=-self.width;
diRightToLeft : FCurrpos:=self.width;
diBottomToTop : FCurrpos:=-Fmemo.height;
end;
end;
{*******************************************************}
procedure TScrollText.Unpause;
begin
if not(Factive)then
begin
FScrollSaved:=0;
FTimertag:=2;
SetActive(true);
end;
end;
{*******************************************************}
procedure TScrollText.WMSize(var Message: TWMSize);
begin
FMemo.Width:=self.width;
if FMemo.Height<self.height then
FMemo.height:=self.height;
end;
{*******************************************************}
function TScrollText.GetAlignMent: TAlignment;
begin
result:=FMemo.Alignment;
end;
{*******************************************************}
procedure TScrollText.Setalignment(const Value: TAlignment);
begin
FMemo.Alignment:=Value;
end;
{*******************************************************}
end.
 
用时间控件可以做出这种效果
 
to AHLKJ:
LKJTypes这个单元在哪里找哦?
有没有现成的包啊,这些代码我编译有问题啊?
EMAIL:jack011@126.com
 
多人接受答案了。
 
顶部