Use Variant in Delphi Code

Hello,

I need to use ActiveX component from a another software provider
The method get as parameter an OleVariant but this is an array of byte , this method is used to send a buffer over the network

The sample source that provides the ActiveX manufacturer is in C++

char *lpwStr = new char[7];
  VARIANT v;  
  VariantInit(&v);
  lpwStr[0] = 0xff;
  lpwStr[1] = 0x01;
  lpwStr[2] = 0x00;
  lpwStr[3] = 0x08;
  lpwStr[4] = 0x00;
  lpwStr[5] = 0x20;
  lpwStr[6] = 0x29;
  v.pcVal = lpwStr;
  m_ctrlActiveX.SendTransparent( v, 7);

When I tried to use in Delphi first I did

 var Data:OleVariant;
 myarray:pchar;
begin
  VariantInit(Data);
  GetMem(myArray,7);
  Data.pcval:=myArray;
 
end;

There is a compilation error for the line Data.pcval:=myArray;

What is wrong with this ?

NB I already tried to use VaArrayCreate but the method doesnt work correctly with this initialisation:

Please help !
KCTeamAsked:
Who is Participating?
 
2266180Connect With a Mentor Commented:
here is what I am usually using and works fine:

var v:olevariant;
    p:pchar;
begin
  V := VarArrayCreate([0, nSize-1], varByte);
  p := pchar(VarArrayLock(V));
  try
    for i:=1 to nSize do
      p[i-1]:=whatever;
  finally
    VarArrayUnlock(V);
  end;
  send V using active function.
end;

Open in new window

0
 
KCTeamAuthor Commented:
Thanks a lot for your answer but it doesnt help in this ActiveX execution, same error.

By the way ,my compiler told me compilation error
the boucle for must be replaced by
for
 i
:=
1
 to nSize 
do
begin

  p




:=
whatever
;
  inc(p);


end;

;)

Open in new window

0
 
2266180Commented:
I copy pasted the code and modified where appropriate. I am using a stream and apointer instead of an array and a pchar :)

anyway, back to your problem.

can you paste the function header for SendTransparent?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
BigRatCommented:
>>What is wrong with this ?

ciuly has posted the correct technique for handling arrays with ActiveX controls.

Your code does two things wrong. First it does not set which variant option is to be taken and second it gets local memory and not COM memory, so even if there were no compilation errors it would crash at run time.

If you have taken ciuly's code, it would be a good idea to paste it here.
0
 
KCTeamAuthor Commented:
Many thanks for your help

Here is the prototype of the COM method

DlmXSendTransparent(ulPort: Integer; pchData: OleVariant;
                                                      ulLength: Integer): Integer;

Here is the code i just tried after  ciuly  post his comment

Data := VarArrayCreate([0, 6], varByte);
  p := pbyte(VarArrayLock(Data));
  try
    p^:=255;inc(p);
    p^:=1;inc(p);
    p^:=0;inc(p);
    p^:=16;inc(p);
    p^:=0;inc(p);
    p^:=32;inc(p);
    p^:=49;
  finally
    VarArrayUnlock(Data);
  end;


  ret:=MyControl.DlmXSendTransparent(port,Data,7) ;


What is the difference between local memeory and COM memory  and did i code someting wrong here ?

Please explain.

Regards
0
 
2266180Commented:
correct translation of the values in your original post is:

Data := VarArrayCreate([0, 6], varByte);
 p := pbyte(VarArrayLock(Data));
 try
   p^:=255;inc(p);
   p^:=1;inc(p);
   p^:=0;inc(p);
   p^:=8{not 16};inc(p);
   p^:=0;inc(p);
   p^:=32;inc(p);
   p^:=41{not 49};
 finally
   VarArrayUnlock(Data);
 end;
 
or
 
Data := VarArrayCreate([0, 6], varByte);
 p := pbyte(VarArrayLock(Data));
 try
   p^:=$ff;inc(p);
   p^:=$01;inc(p);
   p^:=$00;inc(p);
   p^:=$08;inc(p);
   p^:=$00;inc(p);
   p^:=$20;inc(p);
   p^:=$29;
 finally
   VarArrayUnlock(Data);
 end;

Open in new window

0
 
BigRatCommented:
COM memory is global memory. For example all COM strings are BSTRs wich must be allocated by SysAllocBstr. The same applies for COM arrays, the Delphi function just calls the underlying Windows API. This is why you must lock your VariantArray and get a pointer to access the underlying data and Unlock it after you are ready.

cuily also declared p as PChar and not PByte. The former is treated as an array of characters so you can index them. Since you have declared it as pbyte you have to keep on incrementing the pointer after each access.

Don't forget to do a VarClear() after calling the ActiveX control's method to ree the global memory behind the array.

And is it working?

0
 
BigRatCommented:
>> ree..

.. to release the global memory.

Sorry, my bad.
0
 
KCTeamAuthor Commented:
Unfortunatly not yet, but I asked the ActiveX provider to send me compiled code to prove that his ActiveX may work,
0
 
KCTeamAuthor Commented:
Even if the method doesnt work , ciuly answered me .Thanks !
0
 
2266180Commented:
and you give me a B grade for a correct answer which is either because you are using it wrong OR the activex provider has given you bad info or even a bad activex?
are you sure about your decision? maybe you want to cancel the grade and reopen the question and wait to see what the activex provider has to say about this.
0
 
KCTeamAuthor Commented:
I would like to reopen question to give you A grade but how I can do that
0
 
2266180Commented:
no need to give me A grade if the proble is not solved. According to EE rules, you should not close a question just to get it closed. You can have your question open for 21 days with no activity and no consequences for you. After that the cleanup volunteers *might* stumble on it. If you don't want that to happen you can simply post a comment saying your still waiting for something.

anyway, all you need to do to reopen the question is to use the request attention link in the question body and ask a moderator to reopen the question.

keep in mind that at least I, am curious as to why it's not working. adn if it's that providers fault, I'd like to know ;)
0
 
KCTeamAuthor Commented:
Ok I will post the conclusion when I will know it
0
 
KCTeamAuthor Commented:
To close completly this topic, the ActiveX provider sent me some sample code and it works.
But impossible to initialize correctly this Variant in Delphi by affecting directly pcVal field;

I built a dll with C++ builder to do this initialization.

THANKS A LOT TO MICROSOFT FOR THIS MARVELLOUS TYPE : VARIANT !

Thanks to everyone tried to help me on this topic .
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.