• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2429
  • Last Modified:

OleVariant typecast in Delphi

How to assign (or typecast) LongWord variable to OleVariant? I have following code:

var
  dwID: DWORD; // = LongWord = Cardinal - unsigned 32Bit
  var1, var2: OleVariant;
  hMenu: HMENU; // = LongWord = Cardinal - unsigned 32Bit
....  

When I try to assign:
  var1 := hMenu;
  var2 := dwID;
 
compiler (Delphi 5) gives warning: Constant expression violates subrange bounds.


This code is Based on C/C++ sample:
http://msdn.microsoft.com/en-us/library/aa770042(VS.85).aspx#wbc_ctxmenus

DWORD dwID;
CComVariant var1, var2;  
HMENU hMenu;
...
 
   V_VT(&var1) = VT_INT_PTR;
   V_BYREF(&var1) = hMenu;

   V_VT(&var2) = VT_I4;
   V_I4(&var2) = dwID;

   hr = spCT->Exec(&CGID_ShellDocView, SHDVID_ADDMENUEXTENSIONS, 0, &var1, &var2);

************************

Also when I call Exec function in Delphi it does not execute properly:
  hr := OleCommandTarget.Exec(@CGID_ShellDocView, CAddMenuExtensionsCommandID, OLECMDEXECOPT_DODEFAULT, var1, var2);
ALWAYS hr <> S_OK                  

Where:                        
  OleCommandTarget: IOleCommandTarget;
 
Exec declaration:
  function Exec(CmdGroup: PGUID; nCmdID, nCmdexecopt: DWORD;
    const vaIn: OleVariant; var vaOut: OleVariant): HResult; stdcall;

const
  CAddMenuExtensionsCommandID = 37;
  OLECMDEXECOPT_DODEFAULT = 0;

 
I would be very gratefull for any suggestions.
0
Bart_Michael
Asked:
Bart_Michael
1 Solution
 
Russell LibbySoftware Engineer, Advisory Commented:
The typecasts would be as follows:

const
  VT_INT_PTR        =  37;

var
  dwID:             DWORD;
  var1, var2:       OleVariant;
  h_Menu:           HMENU;
begin

  FillChar(var1, SizeOf(TVariantArg), 0);
  FillChar(var2, SizeOf(TVariantArg), 0);

  TVariantArg(var1).vt:=VT_INT_PTR;
  TVariantArg(var1).byRef:=Pointer(h_Menu);

  TVariantArg(var1).vt:=VT_I4;
  TVariantArg(var1).lVal:=LongInt(dwID);

As to the Exec not working, what is the actual HRESULT value from the return? (saying it <> S_OK does not help much)

Regards,
Russell

0
 
Bart_MichaelAuthor Commented:
Thank You VARY MUCH Russell! You are brilliant :-) I'm using Your NamedPipes and they are great!

Now I'm much closer to add an item to TWebBrowser's context menu.

I will post next question about it. So your knowledge will be invited :-)

Thanks and best regards!!!
Bart
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now