一个可执行程序有没有可能不通过其他文件就可以记住上次输入的内容 怎么可能?就好像让没脑子的人记事情一样。无外乎三种办法:1)注册表2)程序本身3)ini文件 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以写到自身的exe中,可是有必要弄得那么复杂吗? 看过一些介绍,可以的。首先要有两个程序,main和config,运行config,配置你的CONNECTSTRING,写入main(在main中用预留给CONNECTSTRING空间,或者在指定地方先放一些空格填充)。在main运行的时候再读出这个CONNECTSTRING就可以了,不过实现好像比较复杂。 to ALL: 多谢大家的关心!to qiubolecn、cszhz: 我的程序是已经编译好EXE文件,有没有可能在程序中预留一些空间,由其他程序写入?具体怎么做,能不能给点详细的说明,或是应用的例子,我好入手 如你讲的,还是在尾部添加的好!但为什么非要只用一个文件呢!创建一个ini其实很有效吗?也不必这么大费周折! 如果需要加入的数据太大,EXE文件的空间不够装呢 但是我在程序中如何读呢?var t:textfile; s:string;begin assignfile(t,'project2.exe'); reset(t); while not eof(t) do begin readln(t,s); showmessage(s); end; closefile(t);end;好象读不出我写进去的内容喔! 因为我做的程序是中间层的应用服务器,调用者只有调用这个应用服务器的权限,所以不能借助其他的资源,例如INI文件 用INI文件装配置,在EXE文件中用变量读取这些设置不就成了吗?这不就很简单。 INI,注册表很简单,如果加到自身文件后就麻烦 多谢大家!经过实验,发现写在EXE文件的最后是可行的,但是如何读出来就是问题了,我写的代码如下,但得不到预期的结果,希望大家能出点主义:procedure TForm1.Button1Click(Sender: TObject);var t:TFileStream; s:string; c:array [0..65535] of char; f:textfile;begin assignfile(f,'project2.exe'); append(f); writeln(f,'abcdefg=asb'); closefile(f); try t:=tFileStream.Create('project2.exe',fmOpenRead); showmessage(‘filesize:'+inttostr(t.size)); t.Read(c,t.size+1); s:=c; showmessage(inttostr(pos('abcdefg=asb',s))); finally t.free; end;end; 用BlockWrite,BlockRead等读写t.Read(c,t.size+1);有问题,最好添加部分长度是固定的,这样整个文件长度减去固定长度就是你添加数据的开始处 既然只有运行权限,肯定写运行程序本身也是不可以的。答案很绝:不可能在本机实现!除了一个例外:借助外力:在程序里面固定设置一个Config Server(也可以通过广播获取,UDP/NetBIOS etc.),连接上就可以通过远程通信读写配置了。 在大富翁里找到的答案,可我在WIN2000运行总出错,给大家试试,有结果不要忘了告诉我一声program Hello;{$APPTYPE CONSOLE}uses Windows, SysUtils;var // szSrc, // szDest : array[0..MAX_PATH] of char; // szCmdLine : string; hFile : THandle; hProcess : THandle; SI : TStartupInfo; PI : TProcessInformation; nTimes : DWord;beginif ParamCount = 0 then begin GetModuleFileName(0, szSrc, MAX_PATH); GetTempPath(MAX_PATH, szDest); GetTempFileName(szDest, 'Tmp', 0, szDest); CopyFile(szSrc, szDest, FALSE); // 将当前可执行文件复制一个副本 hFile := CreateFile(szDest, // pointer to name of the file 0, // access (read-write) mode FILE_SHARE_READ, // share mode nil, // pointer to security attributes OPEN_EXISTING, // how to create FILE_FLAG_DELETE_ON_CLOSE, // file attributes 0); // handle to file with attributes to copy hProcess := OpenProcess(SYNCHRONIZE, // access flag TRUE, // handle inheritance flag GetCurrentProcessId); // process identifier szCmdLine := Format('%s %d "%s"', [szDest, hProcess, szSrc]); //格式化命令行参数 FillChar(SI, SizeOf(SI), 0); SI.cb := SizeOf(SI); CreateProcess(nil, // pointer to name of executable module PChar(szCmdLine), // pointer to command line string nil, // pointer to process security attributes nil, // pointer to thread security attributes TRUE, // handle inheritance flag 0, // creation flags nil, // pointer to new environment block nil, // pointer to current directory name SI, // pointer to STARTUPINFO PI); // pointer to PROCESS_INFORMATION CloseHandle(hProcess); CloseHandle(hFile); endelse begin hProcess := THANDLE(StrToInt(ParamStr(1))); WaitForSingleObject(hProcess, INFINITE); //等待主进程结束 CloseHandle(hProcess); hFile := FileOpen(ParamStr(2), fmOpenReadWrite); if hFile > 0 then begin FileSeek(hFile, $80, 0); FileRead(hFile, nTimes, SizeOf(nTimes)); Inc(nTimes); FileSeek(hFile, $80, 0); FileWrite(hFile, nTimes, SizeOf(nTimes)); FileClose(hFile); Writeln('我已经运行'+IntToStr(nTimes)+'次了'); end; end;end. teechart中标志说明浮点控制 高人们.帮帮我吧 主线程中如何得知子线程已完成任务? 关于SQL的问题... activeform 在浏览器中卷动的问题 我的Delphi程序的Exe文件,再没有装Delphi的机子上不能运行, 如何同时计算出表中a,b的出现次数! 深圳书城4楼哪个书架上有李维的书啊,,我跑了好几次都没看到有啊 delphi中能否实现:单击一个按钮时:自动运行一个登陆程序并根据已知的用户名和密码完成登陆? 怎么才能使serverSocket每一秒种轮寻检查客户端发送过来的数据 招聘兼职:熟悉DELPHI+SQL程序员,要求有实际经验 标题说不清,请入内观看!
首先要有两个程序,main和config,运行config,配置你的CONNECTSTRING,写入main(在main中用预留给CONNECTSTRING空间,或者在指定地方先放一些空格填充)。
在main运行的时候再读出这个CONNECTSTRING就可以了,不过实现好像比较复杂。
多谢大家的关心!
to qiubolecn、cszhz:
我的程序是已经编译好EXE文件,有没有可能在程序中预留一些空间,由其他程序写入?具体怎么做,能不能给点详细的说明,或是应用的例子,我好入手
但为什么非要只用一个文件呢!
创建一个ini其实很有效吗?也不必这么大费周折!
var
t:textfile;
s:string;
begin
assignfile(t,'project2.exe');
reset(t);
while not eof(t) do
begin
readln(t,s);
showmessage(s);
end;
closefile(t);
end;
好象读不出我写进去的内容喔!
这不就很简单。
procedure TForm1.Button1Click(Sender: TObject);
var
t:TFileStream;
s:string;
c:array [0..65535] of char;
f:textfile;
begin
assignfile(f,'project2.exe');
append(f);
writeln(f,'abcdefg=asb');
closefile(f);
try
t:=tFileStream.Create('project2.exe',fmOpenRead);
showmessage(‘filesize:'+inttostr(t.size));
t.Read(c,t.size+1);
s:=c;
showmessage(inttostr(pos('abcdefg=asb',s)));
finally
t.free;
end;
end;
t.Read(c,t.size+1);有问题,最好添加部分长度是固定的,这样整个文件长度减去固定长度就是你添加数据的开始处
program Hello;{$APPTYPE CONSOLE}uses
Windows, SysUtils;
var //
szSrc, //
szDest : array[0..MAX_PATH] of char; //
szCmdLine : string;
hFile : THandle;
hProcess : THandle;
SI : TStartupInfo;
PI : TProcessInformation;
nTimes : DWord;
begin
if ParamCount = 0 then
begin
GetModuleFileName(0, szSrc, MAX_PATH);
GetTempPath(MAX_PATH, szDest);
GetTempFileName(szDest, 'Tmp', 0, szDest);
CopyFile(szSrc, szDest, FALSE); // 将当前可执行文件复制一个副本 hFile := CreateFile(szDest, // pointer to name of the file
0, // access (read-write) mode
FILE_SHARE_READ, // share mode
nil, // pointer to security attributes
OPEN_EXISTING, // how to create
FILE_FLAG_DELETE_ON_CLOSE, // file attributes
0); // handle to file with attributes to copy hProcess := OpenProcess(SYNCHRONIZE, // access flag
TRUE, // handle inheritance flag
GetCurrentProcessId); // process identifier
szCmdLine := Format('%s %d "%s"', [szDest, hProcess, szSrc]); //格式化命令行参数 FillChar(SI, SizeOf(SI), 0);
SI.cb := SizeOf(SI); CreateProcess(nil, // pointer to name of executable module
PChar(szCmdLine), // pointer to command line string
nil, // pointer to process security attributes
nil, // pointer to thread security attributes
TRUE, // handle inheritance flag
0, // creation flags
nil, // pointer to new environment block
nil, // pointer to current directory name
SI, // pointer to STARTUPINFO
PI); // pointer to PROCESS_INFORMATION CloseHandle(hProcess);
CloseHandle(hFile);
end
else
begin
hProcess := THANDLE(StrToInt(ParamStr(1)));
WaitForSingleObject(hProcess, INFINITE); //等待主进程结束
CloseHandle(hProcess); hFile := FileOpen(ParamStr(2), fmOpenReadWrite);
if hFile > 0 then
begin
FileSeek(hFile, $80, 0);
FileRead(hFile, nTimes, SizeOf(nTimes));
Inc(nTimes);
FileSeek(hFile, $80, 0);
FileWrite(hFile, nTimes, SizeOf(nTimes));
FileClose(hFile);
Writeln('我已经运行'+IntToStr(nTimes)+'次了');
end;
end;
end.