Solved

Use Variant in Delphi Code

Posted on 2008-10-30
15
526 Views
Last Modified: 2013-11-23
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 !
0
Comment
Question by:KCTeam
  • 7
  • 5
  • 3
15 Comments
 
LVL 28

Accepted Solution

by:
2266180 earned 500 total points
ID: 22839257
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
 

Author Comment

by:KCTeam
ID: 22839392
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
 
LVL 28

Expert Comment

by:2266180
ID: 22839791
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 27

Expert Comment

by:BigRat
ID: 22849252
>>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
 

Author Comment

by:KCTeam
ID: 22850128
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
 
LVL 28

Expert Comment

by:2266180
ID: 22850219
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
 
LVL 27

Expert Comment

by:BigRat
ID: 22850291
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
 
LVL 27

Expert Comment

by:BigRat
ID: 22850306
>> ree..

.. to release the global memory.

Sorry, my bad.
0
 

Author Comment

by:KCTeam
ID: 22850350
Unfortunatly not yet, but I asked the ActiveX provider to send me compiled code to prove that his ActiveX may work,
0
 

Author Closing Comment

by:KCTeam
ID: 31511573
Even if the method doesnt work , ciuly answered me .Thanks !
0
 
LVL 28

Expert Comment

by:2266180
ID: 22850444
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
 

Author Comment

by:KCTeam
ID: 22850618
I would like to reopen question to give you A grade but how I can do that
0
 
LVL 28

Expert Comment

by:2266180
ID: 22851391
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
 

Author Comment

by:KCTeam
ID: 22851472
Ok I will post the conclusion when I will know it
0
 

Author Comment

by:KCTeam
ID: 22884075
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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
I made this because I wanted to get e-mail with a attached csv file so I'd would be able to import user input into a MS Excel template, but I also wanted to register/save all inputs from each day in a file on the server. 1st - It creates a temp C…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

815 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now