将 Cardinal 转换为 IUnknow
我现在正在致力于将 Visual Basic 代码采用到 Delphi 代码中。
我所拥有的:
// prepare query
with oleClipboardFormat do
begin
cfFormat := CF_FileContents;
ptd := nil;
dwAspect := DVASPECT_CONTENT;
lindex := Index;
tymed := TYMED_ISTREAM or TYMED_ISTORAGE;
end;
// query data
data.GetData(oleClipboardFormat, oleMedium)
字段oleMedium.hGlobal
(变量类型为Cardinal
)包含对IStorage
接口的引用。
我应该如何将 oleMedium.hGlobal
转换为 IStorage
?
我现在正在翻译的 VB 源代码中有某种黑魔法。代码的作者使用以下 Visual Basic 函数将指针转换为接口......
Private Function ResolvePointer(ByVal PtrObj As Long) As stdole.IUnknown
Dim oUnk As stdole.IUnknown
' Get an uncounted reference
' to the IUnknown interface
MoveMemory oUnk, PtrObj, 4&
' Get a counted reference
Set ResolvePointer = oUnk
' Release the uncounted reference
MoveMemory oUnk, 0&, 4&
End Function
I am working on adoption of Visual Basic code into Delphi code right now.
What I have:
// prepare query
with oleClipboardFormat do
begin
cfFormat := CF_FileContents;
ptd := nil;
dwAspect := DVASPECT_CONTENT;
lindex := Index;
tymed := TYMED_ISTREAM or TYMED_ISTORAGE;
end;
// query data
data.GetData(oleClipboardFormat, oleMedium)
The field oleMedium.hGlobal
(type of variable is Cardinal
) contains a reference to IStorage
interface.
How should I cast oleMedium.hGlobal
to IStorage
?
There is some kind of black magic in the VB sources I am translating right now. The author of the code uses following visual basic function to cast pointers to interfaces...
Private Function ResolvePointer(ByVal PtrObj As Long) As stdole.IUnknown
Dim oUnk As stdole.IUnknown
' Get an uncounted reference
' to the IUnknown interface
MoveMemory oUnk, PtrObj, 4&
' Get a counted reference
Set ResolvePointer = oUnk
' Release the uncounted reference
MoveMemory oUnk, 0&, 4&
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
oleMedium
变量被声明为TStgMedium
。它有一个hGlobal
字段,但它也有一个stg
字段,其类型为Pointer
。使用该字段,并在需要使用该接口时将其类型转换为IStorage
:您的 VB 作者没有可供使用的类型转换,因此代码从一个变量复制内存到另一个。
Your
oleMedium
variable is declared as aTStgMedium
. It has anhGlobal
field, but it also has astg
field, which is of typePointer
. Use that field, and type-cast it toIStorage
when you need to use the interface:Your VB author didn't have type-casting at his or her disposal, so the code copied memory from one variable to another.