如果我自己导入 SHParseDisplayName,为什么会出现访问冲突?
当尝试从 Delphi 中的路径获取 pidl 时,我遇到访问冲突,并且返回的 pidl 为零。这是我的代码:
type
// TParseDisplayName = function(pszPath: PChar; pbc: pointer; var pidl: PItemIDList; sfgaoIn: LongWord; var psfgaoOut: LongWord): LongInt;
TParseDisplayName = function(pszPath: PWideChar; pbc: IBindCtx; var pidl: PItemIDList; sfgaoIn: ULong; var psfgaoOut: ULong): HResult;
var
SHParseDisplayName: TParseDisplayName;
SHELL32DLLHandle : THandle;
procedure test();
var
ws : WideString;
tmpLongWord: ULong;
lpItemID: PItemIDList;
begin
//ws := 'Mes documents';
CoInitialize(nil);
// path to test
ws := 'C:\inetsdk\Nouveau Document WordPad.doc';
if (SHParseDisplayName(PWideChar(ws), nil, lpItemID, 0, tmpLongWord) = S_OK) then
if not assigned(lpItemID) then
s := SysErrorMessage(getLastError);
CoUnInitialize();
end;
initialization
SHELL32DLLHandle := LoadLibraryW('shell32.dll');
@SHParseDisplayName := GetProcAddress(SHELL32DLLHandle, 'SHParseDisplayName');
I get an access violation when trying to get a pidl form a path in Delphi, and the returned pidl is nil. This is my code:
type
// TParseDisplayName = function(pszPath: PChar; pbc: pointer; var pidl: PItemIDList; sfgaoIn: LongWord; var psfgaoOut: LongWord): LongInt;
TParseDisplayName = function(pszPath: PWideChar; pbc: IBindCtx; var pidl: PItemIDList; sfgaoIn: ULong; var psfgaoOut: ULong): HResult;
var
SHParseDisplayName: TParseDisplayName;
SHELL32DLLHandle : THandle;
procedure test();
var
ws : WideString;
tmpLongWord: ULong;
lpItemID: PItemIDList;
begin
//ws := 'Mes documents';
CoInitialize(nil);
// path to test
ws := 'C:\inetsdk\Nouveau Document WordPad.doc';
if (SHParseDisplayName(PWideChar(ws), nil, lpItemID, 0, tmpLongWord) = S_OK) then
if not assigned(lpItemID) then
s := SysErrorMessage(getLastError);
CoUnInitialize();
end;
initialization
SHELL32DLLHandle := LoadLibraryW('shell32.dll');
@SHParseDisplayName := GetProcAddress(SHELL32DLLHandle, 'SHParseDisplayName');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TParseDisplayName
的声明省略了调用约定。您需要包含stdcall
。当您未指定调用约定时,将使用默认调用约定。默认调用约定是
register
。这对于参数传递和清理具有不同的语义,这会导致您遇到的运行时错误类型。几乎所有 Windows API 函数都使用stdcall
。The declaration of
TParseDisplayName
omits the calling convention. You need to includestdcall
.When you don't specify a calling convention the default calling convention is used. The default calling convention is
register
. This has different semantics for parameter passing and clean up which leads to the type of runtime error you have experienced. Practically all Windows API functions usedstdcall
.