将 Cardinal 转换为 IUnknow

发布于 2024-10-05 04:17:25 字数 969 浏览 0 评论 0原文

我现在正在致力于将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2024-10-12 04:17:25

您的 oleMedium 变量被声明为 TStgMedium。它有一个 hGlobal 字段,但它有一个 stg 字段,其类型为 Pointer。使用该字段,并在需要使用该接口时将其类型转换为 IStorage

IStorage(oleMedium.stg)

您的 VB 作者没有可供使用的类型转换,因此代码从一个变量复制内存到另一个。

Your oleMedium variable is declared as a TStgMedium. It has an hGlobal field, but it also has a stg field, which is of type Pointer. Use that field, and type-cast it to IStorage when you need to use the interface:

IStorage(oleMedium.stg)

Your VB author didn't have type-casting at his or her disposal, so the code copied memory from one variable to another.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文