两点关键:
1检测是Windows关闭引起的QueryUnload事件。
2改写Software\Microsoft\Windows\CurrentVersion\RunOnce
声明:
DeclareFunctionRegCloseKeyLib”advapi32.dll”Alias”RegCloseKey”(ByValhKeyAsLong)AsLong
DeclareFunctionRegCreateKeyLib”advapi32.dll”Alias”RegCreateKeyA”(ByValhKeyAsLong,ByVallpSubKeyAsString,phkResultAsLong)AsLong
DeclareFunctionRegSetValueExLib”advapi32.dll”Alias”RegSetValueExA”(ByValhKeyAsLong,ByVallpValueNameAsString,ByValReservedAsLong,ByValdwTypeAsLong,lpDataAsAny,ByValcbDataAsLong)AsLongNotethatifyoudeclarethelpDataparameterasString,youmustpassitByValue.
在主Form中增加:
PublicConstREG_SZ=1
PublicConstHKEY_CURRENT_USER=&H80000001
PrivateSubForm_QueryUnload(CancelasInteger,UnloadModeasInteger)
DimhKeyAsLong
DimstrRunCmdAsString
IfUnloadMode=vbAppWindowsThen
strRunCmd=App.Path&”\”&App.EXEName&”.EXE”
CallRegCreateKey(HKEY_CURRENT_USER,”Software\Microsoft\Windows\CurrentVersion\RunOnce”,hKey)
CallRegSetValueEx(hKey,”MyApp”,0&,REG_SZ,ByValstrRunCmd,Len(strRunCmd) 1)
CallRegCloseKey(hKey)
Endif
EndSub->