开发者

Convert Cardinal to IUnknow

开发者 https://www.devze.com 2023-01-27 05:26 出处:网络
I am working on adoption of Visual Basic code into Delphi code right now. What I have: // prepare query

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开发者_如何学Python 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消