?
Solved

Assign IDIspatch events with in/out parameters

Posted on 2003-03-24
8
Medium Priority
?
630 Views
Last Modified: 2007-12-19
Hi!

I really need to assign some events in my code through an IDisaptch interface.
At http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20418739.html I've found a class that solved almost all my problems. The events are triggered just fine, however, ALL my events have [in/out] parameters and that code has bugs, since it does not handles it.

I've tried to solve the problem using something like (using an hardcode solution):

function TComEvents.Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult;
var  a : TDISPParams absolute params;
begin
   result:=S_OK;
   a.rgvarg^[0].lval := 123;
 end;
end;


However, it didn't work... the value passed still wasn't changed. Can you please help me?

thanks

Joao Santos



0
Comment
Question by:jpcs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 9

Expert Comment

by:mocarts
ID: 8195543
it's because Params is of pointer type..
better use typecasting:
function TComEvents.Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult;
begin
  result:=S_OK;
  TDISPParams(a^).rgvarg[0].lval := 123;
end;
end;

wbr, mo.
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8195584
sorr :)
TDISPParams(Params^).rgvarg[0].lval := 123;

mo.
0
 
LVL 1

Author Comment

by:jpcs
ID: 8195604
I get the error Pointer type required just after TDISPParams(Params^)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:mocarts
ID: 8195841
uff.. I didn't observe ';' in the declaration so I thought it is a pointer type..
you must use Params without dereference char '^'
however I've tested both (TDISPParam(Params).. and var with absolute addressing) variations of code in simple procedure calls and they works well (changes value)
so you must look on the other side. how Params is passed and used?
wbr, mo.
0
 
LVL 1

Author Comment

by:jpcs
ID: 8195855
Can I send you my test application by email?

Can you tell me your email?
0
 
LVL 9

Accepted Solution

by:
mocarts earned 1000 total points
ID: 8196139
ok, I think that will be better. you can mail me to helmuts@microlink.lv
0
 
LVL 1

Author Comment

by:jpcs
ID: 8202897
Answer from mocarts :
--------------


Hi!
I can't currently connect to EE due to limitations of foreign country internet
traffic..
so, I solved the problem :)
the problem was in fact that OleVariant doesn't handle ByRef type..
you should check type of TDISPParams(Params).rgvarg^[0] by using vt member.
in long * case it is plVal (VT_BYREF or VT_I4 = $4003)
i.e.
TDISPParams(Params).rgvarg^[0]).plVal^ := 5;
assignment will work as it must work (changes value) in TComEvents.Invoke
method.
and you can't simply pass to event handler casted args as it invokes type
conversion from byref to simple type..
so here will be modified Invoke method of TComEvents:

function TComEvents.Invoke(DispID: Integer; const IID: TGUID; LocaleID:
Integer; Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer):
HResult;
var
  szEvent: String;
  index, count, i: Integer;
  evVar: Tvariantarray;
begin
 Result:=S_OK;
 // Make sure the OnEvent is assigned
 if not(Assigned(FEvent)) then exit;
 // Get the event name using the dispacth id. Exit if unable to find DispID
 index := FEventIDs.IndexOf(Pointer(DispID));
 if (index < 0) then exit
   else szEvent := FEvents[index];
 // Get parameter count
 count := TDispParams(Params).cArgs;
 // we must copy args to prevent from type change
 setLength(evVar, count);
 for i := 0 to count -1 do
   evVar[i] := OleVariant(TDispParams(Params).rgvarg^[i]);
 // calling event
 if Assigned(FEvent) then
    FEvent(FObj, szEvent, DispID, evVar);
 // copying back all values passed ByRef
 for i := 0 to count -1 do
  with TDISPParams(Params) do
   if rgvarg^[i].vt and VT_BYREF = VT_BYREF then begin
     with rgvarg^[i] do
     case vt of
      VT_BYREF or VT_UI1:      pbVal^ := evVar[i];
      VT_BYREF or VT_I2:       piVal^ := evVar[i];
      VT_BYREF or VT_I4:       plVal^ := evVar[i];
      VT_BYREF or VT_R4:       pfltVal^ := evVar[i];
      VT_BYREF or VT_R8:       pdblVal^ := evVar[i];
      VT_BYREF or VT_BOOL:     pbool^ := evVar[i];
      VT_BYREF or VT_DATE:     pdate^ := evVar[i];
      VT_BYREF or VT_BSTR:     pbstrVal^ := evVar[i];
      VT_BYREF or VT_UNKNOWN:  punkVal^ := evVar[i];
      VT_BYREF or VT_DISPATCH: pdispVal^ := evVar[i];
      VT_BYREF or VT_VARIANT:  pvarVal^ := evVar[i];
      VT_BYREF or VT_UI4:      pulVal^ := evVar[i];
      VT_BYREF or VT_INT:      pintVal^ := evVar[i];
      VT_BYREF or VT_UINT:     puintVal^ := evVar[i];
     end;
   end;
end;

good luck!
0
 
LVL 1

Author Comment

by:jpcs
ID: 8202901
Thanks a lot!
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question