Solved

Delphi Multithread error

Posted on 2013-05-22
4
743 Views
Last Modified: 2013-06-07
I use a free Opc client: Simple OPC Client.
I used a part of it in  TThreadbe (it needs to check if a parameter was changed in the background), but there is an error message when writing to the server after "while not Terminated do"  

The error: " Project xx.exe raised exception class EIntfCastError with message 'Interface not supported' "

Code snippet:
  IOPCSyncIO = interface(IUnknown)
    ['{39C13A52-011E-11D0-9675-0020AFD8ADB3}']
    function Read(
            dwSource:                   OPCDATASOURCE;
            dwCount:                    DWORD;
            phServer:                   POPCHANDLEARRAY; {was ptr mgl}
      out   ppItemValues:               POPCITEMSTATEARRAY;
      out   ppErrors:                   PResultList): HResult; stdcall;
    function Write(
            dwCount:                    DWORD;
            phServer:                   POPCHANDLEARRAY; {was ptr mgl}
            pItemValues:                POleVariantArray;
      out   ppErrors:                   PResultList): HResult; stdcall;
  end;
0
Comment
Question by:starhu
  • 2
4 Comments
 
LVL 26

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 250 total points
ID: 39189296
In Threads OnExecute event you must call OleInitialize or CoInitialize to get things work:

uses ... ActiveX;
...
procedure TYourThread.Execute;
begin
  ActiveX.CoInitializeEx(nil, COINIT_APARTMENTTHREADED or COINIT_DISABLE_OLE1DDE);
  try

    ....
  except
  end;
  ActiveX.CoUninitialize;
end;

Open in new window

0
 

Author Comment

by:starhu
ID: 39190574
Hello,

We tried it, but unfortunately the error is the same.

I can send you more code in private if that helps...
0
 
LVL 6

Accepted Solution

by:
House_of_Dexter earned 250 total points
ID: 39209474
There is a bug in Delphi since Delphi5...

procedure MyFoo;
var
    Foo: IFoo;
begin
    Foo:= CoFoo.Create;//_addref automatically called
    Foo:= Foo as IFoo; {<--this recast causes the compiler call _release then _addref again...booom}
end;

Open in new window


procedure MyFoo;
var
    Foo, Foo2: IFoo;
begin
    Foo:= CoFoo.Create;
    Foo2:= Foo as IFoo;
end;

Open in new window


Note you really haven't given us enough info to tell us where your problem is...
Is your thread in a DLL?  If so you should not call CoInitialize in your dll...

What apartment model are you using?  Single or Multi?

Are you sure your not calling on an interface that has been ref counted down to 0 by mixing Interfaces with Objects?

Is the Com object properly Synchronized when you call on outside resources?  Is the ComObject Thread safe?

http://msdn.microsoft.com/en-us/library/ms809971.aspx
0
 

Author Closing Comment

by:starhu
ID: 39228508
Thank you for your answers!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 174
Delphi XE10, MySQL Query 4 127
Delphi - replicating a form 8 66
drawing animated level bar based on numbers 3 88
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

864 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

25 Experts available now in Live!

Get 1:1 Help Now