$PBExportHeader$uo_reghelper.sru
forward
global type uo_reghelper from nonvisualobject
end type
type filetime from structure within uo_reghelper
end type
type systemtime from structure within uo_reghelper
end type
end forward

type filetime from structure
	long		dwlowdatetime
	long		dwhighdatetime
end type

type systemtime from structure
	integer		wyear
	integer		wmonth
	integer		wdayofweek
	integer		wday
	integer		whour
	integer		wminute
	integer		wsecond
	integer		wmilliseconds
end type

global type uo_reghelper from nonvisualobject
end type
global uo_reghelper uo_reghelper

type prototypes
Function long GetFullPathName (String lpFileName ,Long nBufferLength,ref string lpBuffer,ref long lpFilePart) library "kernel32" ALIAS FOR "GetFullPathNameA;Ansi"
Function long CreateDirectory (string lpNewDirectory ,ref SECURITY_ATTRIBUTES lpSecurityAttributes ) library "kernel32"   Alias for "CreateDirectoryA;Ansi"
Function long GetFileTime (Long hFile ,ref filetime  lpCreationTime ,ref filetime  lpLastAccessTime ,ref filetime  lpLastWriteTime )  library  "kernel32" Alias for "GetFileTime;Ansi" 
Function long lopen (string lpPathName , long iReadWrite )  library "kernel32" Alias for "_lopen;Ansi" 
Function long lclose (long hFile)  library "kernel32" Alias for "_lclose" 
Function long FileTimeToSystemTime  (ref  FILETIME lpFileTime ,ref SYSTEMTIME lpSystemTime )  library  "kernel32" Alias for "FileTimeToSystemTime;Ansi"
Function long CopyFile  (String lpExistingFileName , String lpNewFileName , long bFailIfExists ) library  "kernel32" Alias for  "CopyFileA;Ansi"
Subroutine Sleep(ulong dwMilliseconds) LIBRARY "kernel32.dll"
Function long GetWindowsDirectory  (ref string lpBuffer,long nSize) Library "kernel32" Alias for "GetWindowsDirectoryA;Ansi"
Function long GetSystemDirectory  (ref string lpBuffer,long nSize) Library "kernel32" Alias for "GetSystemDirectoryA;Ansi"
Function long SetFileAttributes  (String lpFileName , long dwFileAttributes ) Library  "kernel32" Alias for "SetFileAttributesA;Ansi"
end prototypes

type variables
public boolean ins_win32 = false
end variables

forward prototypes
private function integer f_filetoblob (string arg_filepathname, ref blob arg_blobdata, ref string arg_msg)
private function integer f_blobtofile (string arg_filepathname, blob arg_blobdata, ref string arg_msg)
private function string f_clsid (string arg_progid)
private function string f_codebase (string arg_progid)
private function integer f_ifexist (string arg_regkey)
private subroutine f_test ()
private subroutine f_test1 ()
private function integer reg32dll (string arg_filepath, ref string arg_msg)
private function integer regdll (string arg_filepath, ref string arg_msg)
public function integer unregdll (string arg_filepath, ref string arg_msg)
public function integer connecttonewobject (string arg_progid, string arg_filepath, ref oleobject arg_ole, ref string arg_msg)
public function datetime pf_getfilecreatetime (string arg_filefullpathname)
public function integer tryreg (string arg_progid, string arg_filepath, ref string arg_msg, boolean arg_if_delete)
public function integer tryreg (string arg_progid, string arg_filepath, ref string arg_msg)
private function integer unreg32dll (string arg_filepath, ref string arg_msg)
public function string pf_getfullpathname (string arg_filename)
end prototypes

private function integer f_filetoblob (string arg_filepathname, ref blob arg_blobdata, ref string arg_msg);//读入数据
long flen,loops,ls_i,bytes_read,new_pos,rslt=1,li_FileNum
blob b_dtdata_p,tot_b

flen = FileLength(arg_filepathname)
li_FileNum = FileOpen(arg_filepathname, StreamMode!, Read!, LockRead!)
if li_FileNum=-1 then
		rslt=0
		arg_msg='打开文件'+arg_filepathname+'操作失败!'
		fileclose(li_FileNum)
		goto ext
	end if
IF flen > 32765 THEN
	IF Mod(flen, 32765) = 0 THEN
		loops = flen/32765
	ELSE
		loops = (flen/32765) + 1
	END IF
ELSE
	loops = 1
END IF

new_pos = 1
FOR ls_i = 1 to loops
	bytes_read = FileRead(li_FileNum, b_dtdata_p)
	if bytes_read=0 then
		rslt=0
		arg_msg='读取文件'+arg_filepathname+'操作失败!'
		fileclose(li_FileNum)
		goto ext
	end if
	tot_b = tot_b + b_dtdata_p
NEXT

fileclose(li_FileNum)

arg_blobdata=tot_b
ext:
return rslt
end function

private function integer f_blobtofile (string arg_filepathname, blob arg_blobdata, ref string arg_msg);//blob转文件
long rslt=1,fend,fend_t
blob b_dtdata, b_dtdata_p
long li_FileNum,ls_i,ls_jh
string tmpfilepathname

tmpfilepathname=arg_filepathname
b_dtdata=arg_blobdata

fend=len(b_dtdata)
	if fend<=32765 then
		fend_t=1
	else
		fend_t=fend/32765
		if mod(fend,32765)<>0 then
			fend_t=fend_t+1
		end if
	end if
	li_FileNum = FileOpen( tmpfilepathname,StreamMode!, Write!, LockWrite!)
	if li_FileNum<=0 then
		arg_msg="打开文件"+tmpfilepathname+"失败"
		rslt=0
		fileclose(li_FileNum)
		goto ext
	end if
	for ls_i = 1 to fend_t
		b_dtdata_p=blobmid(b_dtdata,1+(ls_i - 1 )*32765,32765)

		ls_jh=FileWrite(li_FileNum, b_dtdata_p)
		if ls_jh<=0 then
			arg_msg="写文件"+tmpfilepathname+"失败"
			rslt=0
			fileclose(li_FileNum)
			goto ext
		end if
	next
	fileclose(li_FileNum)

ext:
return rslt
end function

private function string f_clsid (string arg_progid);String ls_key
ls_key = "HKEY_CLASSES_ROOT\" + arg_progid + "\CLSID"

String ls_clsid

IF RegistryGet(ls_key, "", RegString!, ls_clsid) <> 1 THEN
	SetNull(ls_clsid)
END IF

RETURN ls_clsid

end function

private function string f_codebase (string arg_progid);String ls_clsid
ls_clsid = f_clsid(arg_progid)

String ls_codebase

IF IsNull(ls_clsid) THEN
	SetNull(ls_codebase)
	GOTO ext
END IF

String ls_key
ls_key = "HKEY_CLASSES_ROOT\CLSID\" + ls_clsid + "\InprocServer32"
String ls_name
IF ins_win32 THEN
	ls_name = ""
ELSE
	ls_name = "CodeBase"
END IF

IF RegistryGet(ls_key, ls_name, RegString!, ls_codebase) <> 1 THEN
	SetNull(ls_codebase)
	GOTO ext
END IF

IF Pos(ls_codebase, 'file:///') > 0 THEN
	ls_codebase = Mid(ls_codebase, 9)
END IF

Long ll_pos

ll_pos = Pos(ls_codebase, '/')
DO WHILE ll_pos > 0
	ls_codebase = Replace(ls_codebase, ll_pos, 1, '\')
	ll_pos = Pos(ls_codebase, '/')
LOOP

ext:
RETURN ls_codebase






end function

private function integer f_ifexist (string arg_regkey);String ls_value[]
IF RegistryValues(arg_regkey, ls_value) = 1 THEN
	RETURN 1
ELSE
	RETURN 0
END IF
end function

private subroutine f_test ();String ls_path
ls_path = "D:\workspace\__________test\MyTest\PBCOMTest\bin\Release\PBCOMTest.dll"

String ls_netversion
ls_netversion = "4.0"

String ls_regkey
ls_regkey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework"

String ls_valuename
ls_valuename = "InstallRoot"

String ls_frameworkpath
IF RegistryGet(ls_regkey, ls_valuename, RegString!, ls_frameworkpath) <> 1 THEN
	MessageBox("", "没有安装.net Framework " + ls_netversion)
	GOTO ext
END IF

String ls_exec
IF ls_netversion < "2.0" THEN
	ls_exec = ls_frameworkpath + "v1.1.4322\"
ELSEIF ls_netversion < "4.0" THEN
	ls_exec = ls_frameworkpath + "v2.0.50727\"
ELSE
	ls_exec = ls_frameworkpath + "v4.0.30319\"
END IF
ls_exec = ls_exec + "RegAsm.exe"

IF Not FileExists(ls_exec) THEN
	MessageBox("", ls_exec + "不存在")
	GOTO ext
END IF

string ls_logfile
ls_logfile= "tmp_reg.txt"

ls_exec = "~"" + ls_exec + "~" ~"" + ls_path + "~" /codebase > ~"" + ls_logfile + "~" 2>&1"

String ls_tmpfile
ls_tmpfile = "tmp_reg.cmd"

if fileexists(ls_tmpfile) then
	filedelete(ls_tmpfile)
end if

String arg_msg

IF f_blobtofile(ls_tmpfile, Blob(ls_exec,EncodingANSI!), arg_msg) <> 1 THEN
	MessageBox("", arg_msg)
	GOTO ext
END IF

IF Run(ls_tmpfile) <> 1 THEN
	MessageBox("", "注册失败")
	GOTO ext
END IF


sleep(1)
filedelete(ls_tmpfile)

Blob lb_msg
IF f_filetoblob(ls_logfile, lb_msg, arg_msg) <> 1 THEN
	MessageBox("", arg_msg)
	GOTO ext
END IF

filedelete(ls_logfile)

arg_msg = string(lb_msg, EncodingANSI!)

MessageBox('', arg_msg)

ext:



end subroutine

private subroutine f_test1 ();oleobject obj
obj = Create oleobject

IF obj.ConnectToNewObject("PBCOMTest.Class3") <> 0 THEN
	MessageBox('', '未连接')
	GOTO ext
END IF

String ls_msg
ls_msg = obj.HelloWorld("123")

MessageBox('', ls_msg)


ext:
obj.DisconnectObject()
Destroy obj


end subroutine

private function integer reg32dll (string arg_filepath, ref string arg_msg);Int rslt = 1

IF Not FileExists(arg_filepath) THEN
	rslt = 0
	arg_msg = arg_filepath + '不存在'
	GOTO ext
END IF

String ls_exec = ""
ls_exec = "C:\WINDOWS\system32\regsvr32.exe"

IF Not FileExists(ls_exec) THEN
	rslt = 0
	arg_msg =  ls_exec + "不存在"
	GOTO ext
END IF

String ls_logfile
ls_logfile = "tmp_reg.txt"

String ls_logfile1
ls_logfile1 = "tmp_tmp.log"

ls_exec = "~"" + ls_exec + "~" /s ~"" + arg_filepath + "~" > ~"" + ls_logfile1 + "~" 2>&1~r~nrename ~"" + ls_logfile1 + "~" ~"" + ls_logfile + "~""

String ls_tmpfile
ls_tmpfile = "tmp_reg.cmd"

IF FileExists(ls_tmpfile) THEN
	FileDelete(ls_tmpfile)
END IF

IF FileExists(ls_logfile) THEN
	FileDelete(ls_logfile)
END IF

IF f_blobtofile(ls_tmpfile, Blob(ls_exec,EncodingANSI!), arg_msg) <> 1 THEN
	rslt = 0
	GOTO ext
END IF

IF Run(ls_tmpfile) <> 1 THEN
	rslt = 0
	arg_msg = "执行脚本失败," + ls_tmpfile
	GOTO ext
END IF

DO WHILE Not FileExists(ls_logfile)
	sleep(1)
LOOP

FileDelete(ls_tmpfile)

Blob lb_msg
IF f_filetoblob(ls_logfile, lb_msg, arg_msg) <> 1 THEN
	rslt = 0
	GOTO ext
END IF

FileDelete(ls_logfile)

String ls_msg
ls_msg = String(lb_msg, EncodingANSI!)

arg_msg = ls_msg

ext:
RETURN rslt


end function

private function integer regdll (string arg_filepath, ref string arg_msg);//====================================================================
// 事件: uo_reghelper.regdll()
//--------------------------------------------------------------------
// 描述: 注册.net的DLL
//--------------------------------------------------------------------
// 参数:
// 	value    	string	arg_filepath	
// 	reference	string	arg_msg     	
//--------------------------------------------------------------------
// 返回:  integer
//--------------------------------------------------------------------
// 作者:	lwl		日期: 2013年03月23日
//--------------------------------------------------------------------
//	LONGJOE
//--------------------------------------------------------------------
// 修改历史:
//
//====================================================================

if ins_win32 then
	return reg32dll(arg_filepath, arg_msg)
end if

Int rslt = 1

IF Not FileExists(arg_filepath) THEN
	rslt = 0
	arg_msg = arg_filepath + '不存在'
	GOTO ext
END IF

String ls_regkey
ls_regkey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework"

String ls_valuename
ls_valuename = "InstallRoot"

String ls_frameworkpath
IF RegistryGet(ls_regkey, ls_valuename, RegString!, ls_frameworkpath) <> 1 THEN
	rslt = 0
	arg_msg = "没有安装.net Framework"
	GOTO ext
END IF

String ls_exec = ""
//IF DirectoryExists(ls_frameworkpath + "v4.0.30319\") THEN
//	ls_exec = ls_frameworkpath + "v4.0.30319\"
//ELSEIF  DirectoryExists(ls_frameworkpath + "v2.0.50727\") THEN
//	ls_exec = ls_frameworkpath + "v2.0.50727\"
//ELSEIF  DirectoryExists(ls_frameworkpath + "v1.1.4322\") THEN
//	ls_exec = ls_frameworkpath + "v1.1.4322\"
//END IF

string ls_dllname // 获取dll文件名
if LastPos(arg_filepath, '\') > 0 then
	ls_dllname = Mid(arg_filepath, LastPos(arg_filepath, '\') + 1)
else
	ls_dllname = arg_filepath
end if

long ll_row
string ls_dotnetver
datastore lds_dllmap
lds_dllmap = create datastore
lds_dllmap.DataObject = 'ds_dllmap'
ll_row = lds_dllmap.Find('dllname="' + ls_dllname + '"', 1, lds_dllmap.RowCount())
if (ll_row > 0) then
	ls_dotnetver = Trim(lds_dllmap.Object.dotnetver[ll_row])
	choose case ls_dotnetver
		case '4.0'
			IF DirectoryExists(ls_frameworkpath + "v4.0.30319\") THEN
				ls_exec = ls_frameworkpath + "v4.0.30319\"
			ELSE
				rslt = 0
			END IF
		case '2.0'
			IF  DirectoryExists(ls_frameworkpath + "v2.0.50727\") THEN
				ls_exec = ls_frameworkpath + "v2.0.50727\"
			ELSE
				rslt = 0
			END IF
		case '1.1'
			IF  DirectoryExists(ls_frameworkpath + "v1.1.4322\") THEN
				ls_exec = ls_frameworkpath + "v1.1.4322\"
			ELSE
				rslt = 0
			END IF
		case else
			rslt = 0
	end choose
else
	IF DirectoryExists(ls_frameworkpath + "v4.0.30319\") THEN
		ls_exec = ls_frameworkpath + "v4.0.30319\"
	ELSEIF  DirectoryExists(ls_frameworkpath + "v2.0.50727\") THEN
		ls_exec = ls_frameworkpath + "v2.0.50727\"
	ELSEIF  DirectoryExists(ls_frameworkpath + "v1.1.4322\") THEN
		ls_exec = ls_frameworkpath + "v1.1.4322\"
	ELSE
		rslt = 0
	END IF
end if

if (rslt = 0) then
	arg_msg = '本机没有安装.Net Framework ' + ls_dotnetver + '环境，请联系实施人员协助处理'
	goto ext
end if

ls_exec = ls_exec + "RegAsm.exe"

IF Not FileExists(ls_exec) THEN
	rslt = 0
	arg_msg =  ls_exec + "不存在"
	GOTO ext
END IF

String ls_logfile
ls_logfile = "tmp_reg.txt"

String ls_logfile1
ls_logfile1 = "tmp_tmp.log"

ls_exec = "~"" + ls_exec + "~" /tlb:~""+mid(arg_filepath, 1, len(arg_filepath) - 4)+".tlb~" ~"" + arg_filepath + "~" /codebase > ~"" + ls_logfile1 + "~" 2>&1~r~nrename ~"" + ls_logfile1 + "~" ~"" + ls_logfile + "~""

String ls_tmpfile
ls_tmpfile = "tmp_reg.cmd"

IF FileExists(ls_tmpfile) THEN
	FileDelete(ls_tmpfile)
END IF

IF FileExists(ls_logfile) THEN
	FileDelete(ls_logfile)
END IF

IF f_blobtofile(ls_tmpfile, Blob(ls_exec,EncodingANSI!), arg_msg) <> 1 THEN
	rslt = 0
	GOTO ext
END IF

IF Run(ls_tmpfile) <> 1 THEN
	rslt = 0
	arg_msg = "执行脚本失败," + ls_tmpfile
	GOTO ext
END IF

DO WHILE Not FileExists(ls_logfile)
	sleep(1)
LOOP

FileDelete(ls_tmpfile)

Blob lb_msg
IF f_filetoblob(ls_logfile, lb_msg, arg_msg) <> 1 THEN
	rslt = 0
	GOTO ext
END IF

FileDelete(ls_logfile)

String ls_msg
ls_msg = String(lb_msg, EncodingANSI!)

arg_msg = Lower(ls_msg)

IF Pos(arg_msg, 'error ra0000') > 0 And (Pos(arg_msg, '管理员') > 0 or Pos(arg_msg, 'admin') > 0) THEN
	rslt = 0
	arg_msg = '注册"' + arg_filepath + '"失败，请以管理员身份运行本程序。详细信息：~r~n~r~n' + arg_msg
ELSEIF Pos(arg_msg, 'success') <= 0 And Pos(arg_msg, '成功') <= 0 THEN
	rslt = 0
END IF

ext:
destroy lds_dllmap
RETURN rslt



end function

public function integer unregdll (string arg_filepath, ref string arg_msg);//====================================================================
// 事件: uo_reghelper.regdll()
//--------------------------------------------------------------------
// 描述: 注册.net的DLL
//--------------------------------------------------------------------
// 参数:
// 	value    	string	arg_filepath	
// 	reference	string	arg_msg     	
//--------------------------------------------------------------------
// 返回:  integer
//--------------------------------------------------------------------
// 作者:	lwl		日期: 2013年03月23日
//--------------------------------------------------------------------
//	LONGJOE
//--------------------------------------------------------------------
// 修改历史:
//
//====================================================================

if ins_win32 then
	return unreg32dll(arg_filepath, arg_msg)
end if

Int rslt = 1

IF Not FileExists(arg_filepath) THEN
	rslt = 0
	arg_msg = arg_filepath + '不存在'
	GOTO ext
END IF

String ls_regkey
ls_regkey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework"

String ls_valuename
ls_valuename = "InstallRoot"

String ls_frameworkpath
IF RegistryGet(ls_regkey, ls_valuename, RegString!, ls_frameworkpath) <> 1 THEN
	rslt = 0
	arg_msg = "没有安装.net Framework"
	GOTO ext
END IF

String ls_exec = "",ls_exec1
IF DirectoryExists(ls_frameworkpath + "v4.0.30319\") THEN
	ls_exec = ls_frameworkpath + "v4.0.30319\"
ELSEIF  DirectoryExists(ls_frameworkpath + "v2.0.50727\") THEN
	ls_exec = ls_frameworkpath + "v2.0.50727\"
ELSEIF  DirectoryExists(ls_frameworkpath + "v1.1.4322\") THEN
	ls_exec = ls_frameworkpath + "v1.1.4322\"
END IF

ls_exec = ls_exec + "RegAsm.exe"

IF Not FileExists(ls_exec) THEN
	rslt = 0
	arg_msg =  ls_exec + "不存在"
	GOTO ext
END IF

String ls_logfile
ls_logfile = "tmp_reg.txt"

String ls_logfile1
ls_logfile1 = "tmp_tmp.log"

ls_exec = "~"" + ls_exec + "~" ~"" + arg_filepath + "~" /unregister > ~"" + ls_logfile1 + "~" 2>&1~r~nrename ~"" + ls_logfile1 + "~" ~"" + ls_logfile + "~""
//ls_exec = "~"" + ls_exec + "~"" + " " + arg_filepath + + " /unregister" + "~"" +  "/codebase >" +  "~"" + ls_logfile1 + "~" 2>&1~r~nrename ~"" + ls_logfile1 + "~" ~"" + ls_logfile + "~""
String ls_tmpfile 
ls_tmpfile = "tmp_reg.cmd"

IF FileExists(ls_tmpfile) THEN
	FileDelete(ls_tmpfile)
END IF

IF FileExists(ls_logfile) THEN
	FileDelete(ls_logfile)
END IF

IF f_blobtofile(ls_tmpfile, Blob(ls_exec,EncodingANSI!), arg_msg) <> 1 THEN
	rslt = 0
	GOTO ext
END IF

IF Run(ls_tmpfile) <> 1 THEN
	rslt = 0
	arg_msg = "执行脚本失败," + ls_tmpfile
	GOTO ext
END IF

DO WHILE Not FileExists(ls_logfile)
	sleep(1)
LOOP

FileDelete(ls_tmpfile)

Blob lb_msg
IF f_filetoblob(ls_logfile, lb_msg, arg_msg) <> 1 THEN
	rslt = 0
	GOTO ext
END IF

FileDelete(ls_logfile)

String ls_msg
ls_msg = String(lb_msg, EncodingANSI!)

arg_msg = ls_msg

IF Pos(arg_msg, 'success') <= 0 And Pos(arg_msg, '成功') <= 0 THEN
	rslt = 0
END IF

ext:
RETURN rslt



end function

public function integer connecttonewobject (string arg_progid, string arg_filepath, ref oleobject arg_ole, ref string arg_msg);//====================================================================
// 事件: uo_reghelper.connecttonewobject()
//--------------------------------------------------------------------
// 描述: 
//--------------------------------------------------------------------
// 参数:
// 	value    	string   	arg_progid  		类名
// 	value    	string   	arg_filepath		DLL路径
// 	reference	oleobject	arg_ole     	要连接的oleobject
// 	reference	string   	arg_msg     	返回失败信息
//--------------------------------------------------------------------
// 返回:  integer
//--------------------------------------------------------------------
// 作者:	lwl		日期: 2013年03月23日
//--------------------------------------------------------------------
//	LONGJOE
//--------------------------------------------------------------------
// 修改历史:
//
//====================================================================


Int rslt = 1

//IF tryreg(arg_progid, arg_filepath, Ref arg_msg, False) <> 1 THEN
//	rslt = 0
//	GOTO ext
//END IF

IF arg_ole.ConnectToNewObject(arg_progid) <> 0 THEN
	rslt = 0
	arg_msg = '连接ole对象失败,' + arg_progid + ',' + arg_filepath
	GOTO ext
END IF

ext:
RETURN rslt







end function

public function datetime pf_getfilecreatetime (string arg_filefullpathname);//pf_getfilecreatetime
long rslt
datetime createtime,errdatetime
date ls_date
time ls_time
systemtime ls_systemtime
filetime lpCreationTime ,lpLastAccessTime ,lpLastWriteTime
long li_FileNum

//

li_FileNum = lOpen(arg_filefullpathname,0)
rslt = GetFileTime (li_FileNum ,lpCreationTime ,lpLastAccessTime ,lpLastWriteTime )
li_FileNum=lclose(li_FileNum)

if rslt=0 then return errdatetime

//rslt=FileTimeToSystemTime(lpCreationTime,ls_systemtime)//
rslt=FileTimeToSystemTime(lpLastWriteTime,ls_systemtime)

if rslt=0 then return errdatetime
	
ls_date=date(string(ls_systemtime.wYear)+'.'+&
				string(ls_systemtime.wmonth)+'.'+&
				string(ls_systemtime.wDay))

ls_time=time(string(ls_systemtime.wHour)+':'+&
				string(ls_systemtime.wMinute )+':'+&
				string(ls_systemtime.wSecond ))

createtime =datetime(ls_date,ls_time)

return createtime
end function

public function integer tryreg (string arg_progid, string arg_filepath, ref string arg_msg, boolean arg_if_delete);
Int rslt = 1

Long ll_pos
String ls_path
ls_path = arg_filepath
ll_pos = Pos(ls_path, '\')
IF ll_pos = 0 THEN
	
	IF sys_cur_path = '' THEN
		ls_path = pf_getfullpathname(arg_filepath)
	ELSE
		ls_path = sys_cur_path + arg_filepath
	END IF
	
	IF Not FileExists(ls_path) THEN
		ls_path = "C:\Windows\SysWOW64\" + arg_filepath
		IF Not FileExists(ls_path) THEN
			ls_path = "C:\Windows\System32\" + arg_filepath
		END IF
	END IF
END IF

IF Not FileExists(ls_path) THEN
	rslt = 0
	arg_msg = '文件不存在,' + ls_path
	GOTO ext
END IF

String ls_codebase
ls_codebase = f_codebase(arg_progid)
IF IsNull(ls_codebase) THEN
	GOTO reg
END IF

DateTime ld_org_date, ld_cur_date

IF Lower(ls_codebase) <> Lower(ls_path) THEN
	IF Lower(ls_codebase) <> "" THEN
		IF FileExists(ls_codebase) THEN
			// DONE: 如果原来的较新 GOTO con
			ld_org_date = pf_getfilecreatetime(ls_codebase)
			ld_cur_date = pf_getfilecreatetime(ls_path)
			IF ld_cur_date <= ld_org_date THEN
				GOTO con
			END IF
			unregdll(ls_codebase, arg_msg)
		END IF
	END IF
	GOTO reg
END IF

GOTO con

reg:
String ls_msg
IF regdll(ls_path, arg_msg) <> 1 THEN
	rslt = 0
	GOTO ext
END IF
ls_codebase = f_codebase(arg_progid)
IF IsNull(ls_codebase) THEN
	rslt = 0
	arg_msg = ls_path + '文件中不存在对象:' + arg_progid + ',请联系软件供应商获取最新文件版本!'
	IF arg_if_delete THEN
		unregdll(ls_path, ls_msg)
		FileDelete(ls_path)
		arg_msg += '旧文件已被删除'
	END IF
	GOTO ext
END IF
IF Lower(ls_codebase) <> Lower(ls_path) THEN
	rslt = 0
	arg_msg = ls_path + '文件注册失败'
	GOTO ext
END IF
con:

ext:
RETURN rslt























end function

public function integer tryreg (string arg_progid, string arg_filepath, ref string arg_msg);return tryreg(arg_progid, arg_filepath, ref arg_msg, false)
end function

private function integer unreg32dll (string arg_filepath, ref string arg_msg);Int rslt = 1

IF Not FileExists(arg_filepath) THEN
	rslt = 0
	arg_msg = arg_filepath + '不存在'
	GOTO ext
END IF

String ls_exec = ""
ls_exec = "C:\WINDOWS\system32\regsvr32.exe"

IF Not FileExists(ls_exec) THEN
	rslt = 0
	arg_msg =  ls_exec + "不存在"
	GOTO ext
END IF

String ls_logfile
ls_logfile = "tmp_reg.txt"

String ls_logfile1
ls_logfile1 = "tmp_tmp.log"

ls_exec = "~"" + ls_exec + "~" /s /u ~"" + arg_filepath + "~" > ~"" + ls_logfile1 + "~" 2>&1~r~nrename ~"" + ls_logfile1 + "~" ~"" + ls_logfile + "~""

String ls_tmpfile
ls_tmpfile = "tmp_reg.cmd"

IF FileExists(ls_tmpfile) THEN
	FileDelete(ls_tmpfile)
END IF

IF FileExists(ls_logfile) THEN
	FileDelete(ls_logfile)
END IF

IF f_blobtofile(ls_tmpfile, Blob(ls_exec,EncodingANSI!), arg_msg) <> 1 THEN
	rslt = 0
	GOTO ext
END IF

IF Run(ls_tmpfile) <> 1 THEN
	rslt = 0
	arg_msg = "执行脚本失败," + ls_tmpfile
	GOTO ext
END IF

DO WHILE Not FileExists(ls_logfile)
	sleep(1)
LOOP

FileDelete(ls_tmpfile)

Blob lb_msg
IF f_filetoblob(ls_logfile, lb_msg, arg_msg) <> 1 THEN
	rslt = 0
	GOTO ext
END IF

FileDelete(ls_logfile)

String ls_msg
ls_msg = String(lb_msg, EncodingANSI!)

arg_msg = ls_msg

ext:
RETURN rslt


end function

public function string pf_getfullpathname (string arg_filename);//f_init_syscurpath(arg_msg)
Long rslt = 1
String lpBuffer
lpBuffer = Fill('',200)
Long lpfilepart,li_FileNum

rslt = GetFullPathName(arg_filename,196,lpBuffer,lpfilepart)

IF rslt = 0 THEN
	return arg_filename
ELSE
	sys_cur_path = Left(lpBuffer,Len(lpBuffer)-Len(arg_filename))
	return lpBuffer
END IF

end function

on uo_reghelper.create
call super::create
TriggerEvent( this, "constructor" )
end on

on uo_reghelper.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on

