| 发表于:2008-01-20 12:12:4217楼 得分:0 |
可是问题是 写出来后的exe 双击exe可以运行 但是用其他的exe调用就不能运行 // 有的exe可以调用 有的exe就不能调用 // 可能是你的问题了. 按你的描述,我能想到的原因是------------初始化路径(也就是快捷方式属性窗口里的那个"起始位置"). 拿createprocess这个api来举例.它的原型如下: - c/c++ code
bool createprocess(
lpctstr lpapplicationname,
// pointer to name of EXECutable module
lptstr lpcommandline, // pointer to command line string
lpsecurity_attributes lpprocessattributes, // process security attributes
lpsecurity_attributes lpthreadattributes, // thread security attributes
bool binherithandles, // handle inheritance flag
dword dwcreationflags, // creation flags
lpvoid lpenvironment, // pointer to new environment block
lpctstr lpcurrentdirectory, // pointer to current directory name
lpstartupinfo lpstartupinfo, // pointer to startupinfo
lpprocess_information lpprocessinformation // pointer to process_information
);
lpcurrentdirectory这个参数,就是指定了程序启动时的初始路径. 如果你的程序里使用了相对路径,而这个相对路径却没有使用app.path来指定的话,就会有这情况. 为此我写了一个小小的测试程序,如下: - vbscript code
'这个框里的代码所需要的条件:
'新建一个exe工程;
'在默认的窗体上添加两个按钮,名称不变,为command1与command2;
'然后把这里所有的代码复制到窗体的代码窗口里面去,并生成一个exe备用.我是生成了名为"testinitdir.exe"的文件.
option explicit
private sub command1_click()
'不使用定位
if dir("1.txt") <> "" then
msgbox "true" '找到文件,就弹出true
else
msgbox "false" '没找到,就弹出false
end if
end sub
private sub command2_click()
'使用自定位
if dir(addstrtostr(app.path, "\") & "1.txt") <> "" then
msgbox "true" '找到文件,就弹出true
else
msgbox "false" '没找到,就弹出false
end if
end sub
private function addstrtostr(byval str1 as string, byval str2 as string) as string
'自动添加字符串到目标字符串结尾
if lcase(right(str1, len(str2))) = lcase(str2) then
addstrtostr = str1
else
addstrtostr = str1 & str2
end if
end function
以上是一个工作程序,是模拟你的exe用的. 下面这个是模拟其它的exe调用你的exe的情况. - vbscript code
'这个框里的代码所需要的条件:
'新建一个exe工程;
'在默认的窗体上添加两个按钮,名称不变,为command1与command2;
'然后把这里所有的代码复制到窗体的代码窗口里面去.可以不生成exe而直接在ide里调用.
option explicit
const infinite = &hffff
const startf_useshowwindow = &h1
private enum ensw
sw_hide = 0
sw_normal = 1
sw_maximize = 3
sw_minimize = 6
end enum
private type process_information
hprocess as long
hthread as long
dwprocessid as long
dwthreadid as long
end type
private type startupinfo
cb as long
lpreserved as string
lpdesktop as string
lptitle as string
dwx as long
dwy as long
dwxsize as long
dwysize as long
dwxcountchars as long
dwycountchars as long
dwfillattribute as long
dwflags as long
wshowwindow as integer
cbreserved2 as integer
lpreserved2 as byte
hstdinput as long
hstdoutput as long
hstderror as long
end type
private type security_attributes
nlength as long
lpsecuritydescriptor as long
binherithandle as long
end type
private enum enpriority_class
normal_priority_class = &h20
idle_priority_class = &h40
high_priority_class = &h80
end enum
private declare function createprocess lib "kernel32" alias "createprocessa" (byval lpapplicationname as string, byval lpcommandline as string, lpprocessattributes as security_attributes, lpthreadattributes as security_attributes, byval binherithandles as long, byval dwcreationflags as long, lpenvironment as any, byval lpcurrentdriectory as string, lpstartupinfo as startupinfo, lpprocessinformation as process_information) as long
private declare function waitforsingleobject lib "kernel32" (byval hhandle as long, byval dwmilliseconds as long) as long
private function supershell(byval app as string, byval workdir as string, dwmilliseconds as long, byval start_size as ensw, byval priority_class as enpriority_class) as boolean
'增强的shell
dim pclass as long
dim sinfo as startupinfo
dim pinfo as process_information
dim sec1 as security_attributes
dim sec2 as security_attributes
sec1.nlength = len(sec1)
sec2.nlength = len(sec2)
with sinfo
.cb = len(sinfo)
.dwflags = startf_useshowwindow
.wshowwindow = start_size
end with
pclass = priority_class
if createprocess(vbnullstring, app, sec1, sec2, false, pclass, _
0&, workdir, sinfo, pinfo) then
waitforsingleobject pinfo.hprocess, dwmilliseconds
supershell = true
else
supershell = false
end if
end function
private sub command1_click()
'模拟"指定了错误的初始目录"的情况
supershell "d:\temp\testinitdir.exe", vbnullstring, -1, sw_normal, normal_priority_class
end sub
private sub command2_click()
'模拟"指定了正确的初始目录"的情况
supershell "d:\temp\testinitdir.exe", "d:\temp\", -1, sw_normal, normal_priority_class
end sub
以上工作完成后,按如下过程来测试: 首先在第一个exe也就是testinitdir.exe所在的目录下放一个1.txt,这个文件是模拟你exe所使用的一些文件; 手工双击这个exe,看看两个按钮点下去所得到的结果是不是都是true?如果是,那就继续: 现在关了那个exe,使用第二个工程来启动它. 当然,第二个工程的那几个路径你得改成你机器上testinitdir.exe实际的路径~~ 好. 使用第一个按钮来启动testinitdir.exe时,出现什么了? 你点点testinitdir.exe的第一个按钮?再点点第二个按钮? 关了这个testinitdir.exe,返回~~~ 然后用第二个按钮来启动testinitdir.exe再试下? 明白了没? 如果你的程序真的是这个原因造成的,那么解决就非常简单-------在需要使用一个相对的资源时,使用app.path来进行自定位就ok了. 本来没必要写这么多,但是本着"学习why而不是学习how"的想法,把原因也写了一下.....于是才啰嗦了这么大一堆:) 希望这些能帮到你. 当然也希望你的问题就是这个原因~~嘿嘿 不然就是牛头不对马嘴啦..... | | |
|