首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

遇到个简单又非常变态 神经病 的有关问题, 有经验的帮忙看下

2013-04-21 
遇到个简单又非常变态 神经病 的问题, 有经验的帮忙看下。unit XJHinterfaceusesWindows, Messages, SysUt

遇到个简单又非常变态 神经病 的问题, 有经验的帮忙看下。


unit XJH;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IniFiles, Functions, StdCtrls;

type
  TForm1 = class(TForm)
    lbl2: TLabel;
    lbl1: TLabel;
    edt_chdPath: TEdit;
    btn1: TButton;
    btn2: TButton;
    dlgOpen1: TOpenDialog;
    procedure btn2Click(Sender: TObject);
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
begin
  if(dlgOpen1.Execute) then
  begin
    edt_chdPath.Text := dlgOpen1.FileName;
  end;
end;

procedure TForm1.btn2Click(Sender: TObject);
var
  comStr: string;
  exeName: string;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  Tid: THandle;
begin
  if (not FileExists(edt_chdPath.Text)) then // 文件不存在 直接跳出
    Exit;

  exeName := edt_chdPath.Text;

  comStr := ' /Window /Pack';
  FillChar(ProcessInfo, Sizeof(TProcessInformation), 0);
  FillChar(StartupInfo, Sizeof(TStartupInfo), 0);
  StartupInfo.cb := Sizeof(TStartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := SW_SHOW;
  If CreateProcess(PWideChar(exeName), PWideChar(comStr), nil, nil, False,
    NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then
  begin

  end;
end;

end.


非常简单的一段代码,是点击按钮 用 OpenDialog 打开一个文件,返回一个 文件名 给 Edit.Text
然后用 CreateProcess 启动这个exe , 然后远程注入个Dll到这个进程。
一切正常,后来我用ini记录下 Edit.Text 文件名 下次就不需要再打开文件了
到这里思路都很清晰吧?   
上面代码我没贴出记录到ini的  以免显得太乱。
问题就是 我加载的时候读出ini的路径   显示到 Edit.Text
然后点击按钮, exe启动了 但是有问题 乱报错。
调试的时候 又是启动失败, 但不调试又启动成功 但乱报错。
重点是  我用 OpenDialog 浏览一次  又能正常启动   不用OpenDialog 就报错

CreateProcess('直接写路径', '直接写命令行', nil, nil, False,
    NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo)

连这样都报错, 非要OpenDialog 浏览一次才不报错  问题是我OpenDialog那又没写什么重要代码
这是什么个鸟情况?   有没人遇到过  我快疯了 经验 OpenDialog CreateProcess
[解决办法]
其实你自己断点单步走一下程序就应该很快就能找出根结
[解决办法]
exeName := ini.ReadString...


CHDIR(ExtractFilePath(exeName)); //加上这句
If CreateProcess(PWideChar(exeName), PWideChar(comStr), nil, nil, False,    NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then
begin
end;

ini保存,由于未打开OpenDialog,当前路径还是在原路径,

热点排行