🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #11776

Re: CoInitializeEx

#11776
From: Vladimir Afanasyev
Date: Mon, 26 Jan 2004 11:26
62 lines
2869 bytes
Hi Nick,

"nicolasr" <nicolasrNOSPAMATSIGNgmx.net> wrote:
>Hi,
>
>I'm writing a vcl component that needs to initialize COM for multi-threading
>to talk to WMI. I call CoInitializeEx in its constructor like this (only at
>runtime, though!)
>
>CoInitializeEx(0, COINIT_MULTITHREADED);
>
>When I use a simple test project, drag the component on the blank form and
>run it I get this error:
>RPC_E_CHANGED_MODE
>with message string
>"Cannot change thread mode after it is set"
>
>Why? I used a blank project and I don't assume that COM is initialized by
>default?
>

You can try follow solution:
Add comobj.hpp in project .cpp file:

...
#include <comobj.hpp>
#include <vcl.h>
...


Then add variable CoInitFlags in the same cpp:

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
//It will work only for NT/2000/XP but you should set it because WMI as
//a rule need in CoInitializeEx(0, COINIT_MULTITHREADED)
CoInitFlags=COINIT_MULTITHREADED;

Now you can use it in another units:
hres = CoInitializeEx(0, COINIT_MULTITHREADED); //Initialize COM.
if (FAILED(hres))
   { ShowMessage("Failed to initialize COM library. Error code = "+IntToHex((int)hres,8));
     Application->Terminate();
     return;
   }

Just this way I do it in my WMI samples:
http://codecentral.borland.com/codecentral/ccweb.exe/listing?id=19443


Small explanation and comments you can find in VCL comobj.pas:
{ CoInitFlags determines the COM threading model of the application or current
  thread. This bitflag value is passed to CoInitializeEx in ComServ initialization.
  Assign COINIT_APARTMENTTHREADED or COINIT_MULTITHREADED to this variable before
  Application.Initialize is called by the project source file to select a
  threading model.  Other CoInitializeEx flags (such as COINIT_SPEED_OVER_MEMORY)
  can be OR'd in also.  }
var
  CoInitFlags: Integer = -1;  // defaults to no threading model, call CoInitialize()

Vladimir.
Technical Details
Message-ID: <40150765$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.tpinternet.pl!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4012ffb1$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,415206d9c16e00b1
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2004-01-26 04:39:41 PST
From: "Vladimir Afanasyev" <avv@systema.kiev.ua>
Sender: "Vladimir Afanasyev" <avv@systema.kiev.ua>
Reply-To: "Vladimir Afanasyev" <avv@systema.kiev.ua>
Subject: Re: CoInitializeEx
Newsgroups: borland.public.cppbuilder.nativeapi
X-User-Info: 212.82.216.226     
X-Forwarded-For: 192.168.0.147
References:  <4012ffb1$1@newsgroups.borland.com>
NNTP-Posting-Host: 207.105.83.66
Message-ID: <40150765$1@newsgroups.borland.com>
Date: 26 Jan 2004 04:26:13 -0700
X-Trace: newsgroups.borland.com 1075119973 207.105.83.66 (26 Jan 2004 04:26:13 -0700)
Lines: 62
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.tpinternet.pl!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail