gpt4 book ai didi

delphi - 使用 ifileoperation copyitem 时提升 uac

转载 作者:行者123 更新时间:2023-12-01 23:11:03 27 4
gpt4 key购买 nike

我在 Windows 7 中使用下面的代码进行文件复制操作,但它不起作用,并且在运行或调试时没有任何错误,当应用程序调用 CoGetObject 时,它会崩溃并且无法运行返回任何 Hresult 错误,我只知道错误发生在这一行 ->result := CoGetObject(pWideString(MonikerName), @BindOpts,
ShlObj.IFileOperation, @iFileOperation);
任何人都可以在这段代码中发现任何问题吗? `

type  PBindOpts3 = ^TBindOpts3;{$EXTERNALSYM tagBIND_OPTS3}  tagBIND_OPTS3 = record    cbStruct: DWORD;    grfFlags: DWORD;    grfMode: DWORD;    dwTickCountDeadline: DWORD;    dwTrackFlags: DWORD;    dwClassContext: DWORD;    locale: LCID;    pServerInfo: Pointer;     hwnd: hwnd;  end;  TBindOpts3 = tagBIND_OPTS3;{$EXTERNALSYM BIND_OPTS3}  BIND_OPTS3 = TBindOpts3;

function CopyItem(const aSrcItem, aDest, aNewName: string): HResult;const CLSID_FileOp: TGUID = '{3ad05575-8857-4850-9277-11b85bdb8e09}';

var lFileOperation: ShlObj.IFileOperation; psiFrom: IShellItem; psiTo: IShellItem; myFile: TextFile; BindOpts: TBindOpts3; MonikerName: WideString; Res: HResult;

begin try

begin

结果 := CoInitialize(nil); 如果成功(结果)则 开始

    ZeroMemory(@BindOpts, Sizeof(TBindOpts3));
BindOpts.cbStruct := Sizeof(TBindOpts3);
BindOpts.hwnd := 0;
BindOpts.dwClassContext := CLSCTX_LOCAL_SERVER;

MonikerName := 'Elevation:Administrator!new:' + GUIDToString
(CLSID_FileOp);
result := CoGetObject(pWideString(MonikerName), @BindOpts,
ShlObj.IFileOperation, @lFileOperation);

if Succeeded(result) then

result := CoCreateInstance(CLSID_FileOp, nil,
CLSCTX_LOCAL_SERVER + CLSCTX_INPROC_SERVER + CLSCTX_INPROC_HANDLER,
IFileOperation, lFileOperation);
if Succeeded(result) then
begin
result := lFileOperation.SetOperationFlags
(FOFX_SHOWELEVATIONPROMPT + FOFX_NOCOPYHOOKS +
FOFX_REQUIREELEVATION);
if Succeeded(result) then
begin
result := SHCreateItemFromParsingName
(pchar(aSrcItem), nil, IShellItem, psiFrom);
if Succeeded(result) then
begin
if aDest <> '' then
begin

result := SHCreateItemFromParsingName
(pchar(aDest), nil, IShellItem, psiTo);
end;

if Succeeded(result) then
begin

result := lFileOperation.CopyItem
(psiFrom, psiTo, pchar(aNewName), nil);

psiTo := nil;
end;

psiFrom := nil;
end;

if Succeeded(result) then
begin

result := lFileOperation.PerformOperations;
end;
end;

lFileOperation := nil;
end;

CoUninitialize;
end;
end;

除了 on d: 异常(exception) 开始

 showmessage(d.tostring());
end;

结束;

结束;

`

最佳答案

CoGetObject() 实际返回什么 HRESULT 值?

调用 COM 函数时,不要将 WideString 强制转换为 pWideString。按原样传递它,让 Delphi 为您处理编码细节。

并且调用CoGetObject()之后不需要再调用CoCreateInstance()。 CoGetObject() 已将所需的 COM 对象返回给您。按原样使用它。

试试这个:

function CopyItem(const aSrcItem, aDest, aNewName: string): HResult;
const
CLSID_FileOp: TGUID = '{3ad05575-8857-4850-9277-11b85bdb8e09}';
var
lFileOperation: ShlObj.IFileOperation;
psiFrom: IShellItem;
psiTo: IShellItem;
BindOpts: TBindOpts3;
MonikerName: WideString;
begin
try
Result := CoInitialize(nil);
OleCheck(Result);
try
ZeroMemory(@BindOpts, Sizeof(TBindOpts3));
BindOpts.cbStruct := Sizeof(TBindOpts3);
BindOpts.hwnd := 0;
BindOpts.dwClassContext := CLSCTX_LOCAL_SERVER;

MonikerName := 'Elevation:Administrator!new:' + GUIDToString(CLSID_FileOp);
Result := CoGetObject(MonikerName, @BindOpts, ShlObj.IFileOperation, lFileOperation);
OleCheck(Result);

Result := lFileOperation.SetOperationFlags(FOFX_SHOWELEVATIONPROMPT or FOFX_NOCOPYHOOKS or FOFX_REQUIREELEVATION);
OleCheck(Result);

Result := SHCreateItemFromParsingName(PChar(aSrcItem), nil, IShellItem, psiFrom);
OleCheck(Result);

if aDest <> '' then
begin
Result := SHCreateItemFromParsingName(PChar(aDest), nil, IShellItem, psiTo);
OleCheck(Result);
end;

Result := lFileOperation.CopyItem(psiFrom, psiTo, PChar(aNewName), nil);
OleCheck(Result);

Result := lFileOperation.PerformOperations;
OleCheck(Result);
finally
CoUninitialize;
end;
except
on E: Exception do ShowMessage(d.ToString());
end;
end;

关于delphi - 使用 ifileoperation copyitem 时提升 uac,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3271537/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com