如何使程序只能由其它程序调用?(100分)

  • 主题发起人 主题发起人 Town
  • 开始时间 开始时间
T

Town

Unregistered / Unconfirmed
GUEST, unregistred user!
现需要做2个程序,主程序a.exe和附属程序b.exe,<br>怎样使程序b.exe只能由a.exe调用,而不能独立运行?即使a.exe已经运行<br>&nbsp; ,双击b.exe也必须不能运行.<br><br>&nbsp;<br>&nbsp;
 
在b中判断a是否在运行,不就可以了吗?
 
你可以为b.exe 作一个不公开的参数。
 
把b的扩展名改为其他的名字(如*.dat),用户就不会直接启动他了.
 
用Winexec 或CreateProcess运行b.exe时,可在lpszCmdLine和lpCommandLine参数中作手脚。<br>BOOL CreateProcess(<br>&nbsp; &nbsp; LPCTSTR &nbsp;lpApplicationName,<br>&nbsp; &nbsp; LPTSTR &nbsp;lpCommandLine, <br>&nbsp; &nbsp; LPSECURITY_ATTRIBUTES &nbsp;lpProcessAttributes,<br>&nbsp; &nbsp; LPSECURITY_ATTRIBUTES &nbsp;lpThreadAttributes,<br>&nbsp; &nbsp; BOOL &nbsp;bInheritHandles, <br>&nbsp; &nbsp; DWORD &nbsp;dwCreationFlags,<br>&nbsp; &nbsp; LPVOID &nbsp;lpEnvironment, <br>&nbsp; &nbsp; LPCTSTR &nbsp;lpCurrentDirectory,<br>&nbsp; &nbsp; LPSTARTUPINFO &nbsp;lpStartupInfo, <br>&nbsp; &nbsp; LPPROCESS_INFORMATION &nbsp;lpProcessInformation <br>&nbsp; &nbsp;); <br>UINT WinExec(<br>&nbsp; &nbsp; LPCSTR &nbsp;lpszCmdLine,<br>&nbsp; &nbsp; UINT &nbsp;fuCmdShow <br>&nbsp; &nbsp;); <br>
 
以上方法都是不太好,用我的吧!<br>修改程序a的Project Source:<br>program a;<br><br>uses<br>&nbsp; Forms,<br>&nbsp; Windows,<br>&nbsp; main in 'main.pas' {Form1};<br><br>const<br>&nbsp; evstr = 'I must be run first';<br><br>{$R *.RES}<br>begin<br>&nbsp; Application.Initialize;<br>&nbsp; SetEnvironmentVariable('EnvirVar',evstr);<br>&nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; Application.Run;<br>end.<br><br>修改程序b的Project Source:<br>program sub;<br><br>uses<br>&nbsp; Forms,<br>&nbsp; Windows,<br>&nbsp; SysUtils,<br>&nbsp; Dialogs,<br>&nbsp; submain in 'submain.pas' {Form1};<br>const<br>&nbsp; evstr = 'I must be run first';<br><br>{$R *.RES}<br>var<br>&nbsp; str :array [0..127] of char;<br>begin<br>&nbsp; GetEnvironmentVariable('EnvirVar',@str,128);<br>&nbsp; if strpas(str)&lt;&gt;evstr then<br>&nbsp; begin<br>&nbsp; &nbsp; ShowMessage('this program could only be run under program A !');<br>&nbsp; &nbsp; Application.Terminate;<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br>&nbsp; Application.Initialize;<br>&nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; Application.Run;<br>end.<br>
 
哎呀,差点忘了,在程序中加入对程序b(在我这儿是sub)的调用,以下是上面那个例<br>子的继续:<br>function WinExecProcess(FileName:String; Visibility : integer):boolean;<br>var<br>&nbsp; zAppName:array[0..512] of char;<br>&nbsp; zCurDir:array[0..255] of char;<br>&nbsp; WorkDir:String;<br>&nbsp; StartupInfo:TStartupInfo;<br>&nbsp; ProcessInfo:TProcessInformation;<br>begin<br>&nbsp; StrPCopy(zAppName,FileName);<br>&nbsp; GetDir(0,WorkDir);<br>&nbsp; StrPCopy(zCurDir,WorkDir);<br>&nbsp; FillChar(StartupInfo,Sizeof(StartupInfo),#0);<br>&nbsp; StartupInfo.cb := Sizeof(StartupInfo);<br>&nbsp; StartupInfo.dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; StartupInfo.wShowWindow := Visibility;<br>&nbsp; if not CreateProcess(nil,<br>&nbsp; &nbsp; zAppName, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ pointer to command line string }<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to process security attributes }<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to thread security attributes }<br>&nbsp; &nbsp; false, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { handle inheritance flag }<br>&nbsp; &nbsp; CREATE_NEW_CONSOLE or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ creation flags }<br>&nbsp; &nbsp; NORMAL_PRIORITY_CLASS,<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 继承当前程序的环境!}<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to current directory name }<br>&nbsp; &nbsp; StartupInfo, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to STARTUPINFO }<br>&nbsp; &nbsp; ProcessInfo) then<br>&nbsp; &nbsp; Result := false { pointer to PROCESS_INF }<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; Result := true;<br>&nbsp; &nbsp; end;<br>}<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; cmdline :string;<br>begin<br>&nbsp; cmdline := ExtractFilePath(paramstr(0))+'sub.exe';<br>&nbsp; if not WinExecProcess(cmdline,1) then<br>&nbsp; &nbsp; showmessage('could not call program sub!');<br>end;<br>
 
同意柳五公子 的方法,补充一点:可以用FileMapping函数,可能比用环境变量<br>干净。<br>
 
1.datoncg的方法不能保证b.exe不被独立运行.<br>2.menxin的方法和俺想的一样,但是我不知道如何在Delphi中使用<br>&nbsp; 命令行参数. 俺再加50分请menxin老兄说说怎样在Delphi中使用命令<br>&nbsp; 行参数,不知可否?<br>3.redforks的方法很有趣,看来俺的思维太受定势的影响.<br>4.amo的说法俺没懂,是否也是要做一个带参数的程序?<br>5.柳五公子的方法简明、好用,完全达到了俺的目的,呵呵.<br>6.tqz所言之filemapping函数,俺遍寻未得。请问如何使用?<br>
 
由a产生一临时文件,b运行时判断此特定的临时文件是否存在即可.
 
town:<br>&nbsp; &nbsp; tqz大虾的意思是建立一段共享内存(你可以参考API),a在共享内存内做个标<br>记,b启动时检查这个标记。
 
你可以看看Word5.0的Graphics,它使用的是OLE接口,可以保证不能单独运行,如果单独运行会有提示并退出,仅供参考 :-)
 
还有一种方式就是把B.EXE做成动态链接库DLL(名字无所谓啦 :-))。
 
最简单而可靠的方法:<br><br>假定程序A的主Form的类名是TFormA, 标题(Caption)是'FormA',<br>则在程序b的项目文件中(.dpr文件, Project|View Source)可以这样写:<br><br>uses<br>&nbsp; Windows, //添加这一行<br>&nbsp; Forms,<br>&nbsp; ....;<br>...<br>begin<br>&nbsp; Application.Initialize;<br>&nbsp; if FindWindow('TFormA', 'FormA') = 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; ShowMessage('程序b不能单独运行!');<br>&nbsp; &nbsp; Application.Terminate;<br>&nbsp; end;<br>&nbsp; Application.CreateForm(TFormB, FormB);<br>&nbsp; Application.Run;<br>end.<br>
 
Sorry, 题目意思理解错了.<br>上面的"最简单而可靠的方法:" 不行.不能够防止程序A运行后又运行b.
 
看来命令行参数最可靠了.<br>修改b的dpr:<br>&nbsp; .....<br>begin<br>&nbsp; if (paramcount = 1) and (paramstr(1) = 'somespecialstring') then<br>&nbsp; begin<br>&nbsp; &nbsp; Application.Initialize;<br>&nbsp; &nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; &nbsp; Application.Run;<br>&nbsp; end;<br>end.
 
我认为你只要对你的程序作如下手脚就可以达到目的<br>对b.exe project中加入<br>&nbsp; Application.Initialize;<br>&nbsp; Application.CreateForm(TForm1, Form1);<br>......<br>&nbsp; if ParamStr(1)='hideparam' then //若第一个参数为hideparam时执行<br>&nbsp; &nbsp; Application.Run;<br><br>对a.exe 调用b时用 &nbsp;<br>&nbsp; &nbsp; WinExec(Pchar('b hideparam'),0); &nbsp;带参数调用b.exe<br>
 
很满意 :)<br>柳五公子得此题之100分,<br>Another_eYes,menxin,redforks分享另外的100分,<br>请到&lt;a href="DispQ.asp?LID=119624"&gt;此处&lt;/a&gt;领取
 
接受答案了.
 
后退
顶部