|
You need a marshaller for your COM object - the COM runtime needs to be able to take the parameters for your call and move them to another thread, call the object, then move the return values back to the calling thread.
Firstly, check what sort of object it is. In the registry, under the object's CLSID key, under InprocServer32, you'll see a ThreadingModel value. If this value isn't present at all, the object is single-threaded - calls to it will always run on the first thread in your application, regardless of the thread on which you create the object. If it's 'Apartment', calls to the object will always run on the thread that created it. If it's 'Free', calls to the object will run on the same thread as the caller. If it's 'Both', the behaviour depends on the creating thread: if the creating thread is in a Single Threaded Apartment [STA], it behaves like 'Apartment', but if it's in the Multi-Threaded Apartment [MTA], it behaves like 'Free'.
You can control which apartment each thread lives in. By default in .NET all threads are in the MTA, unless the [STAThread] attribute is used on the thread's start method to force it into the STA. This is done by the WinForms project types on the Main function to make the initial thread an STA thread, for compatibility with ActiveX controls, which must be either not marked or marked 'Apartment'.
IIRC, you still need a marshaller with 'Free' model classes if the creating thread is an STA thread. This marshaller ensures that any events that the class raises (or any other callbacks it makes) come back to the creating thread.
Ideally you probably want to avoid marshalling. If you only want to access the object from a worker thread, create it as needed. Don't create it on the UI thread and keep it around. In fact, if you do create it on the UI thread and it's an Apartment model class, the calls will get marshalled back to the UI thread anyway and still block the UI.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
Thank you for help.
I'm still working on it.
Now I use [MTAThread] before main and everything works fine.
|
|
|
|
|
Hi,
Ok, this question may sound strange to you all, but here goes. I have an In Proc COM Server with ConnectionPoints implemented, created using ATL. Now, this COM Server must display a Window. Since the Window has a message loop of its own, I am creating the Window in a different thread. The Window has been created using WTL 7.1. There should be communication between the main COM thread and the window thread such that the COM Server can call the Window's functions and the Window should be able to call the COM Server in case of events so that the parent application can be notified through Connections points.
Can someone guide me as to how to achieve the above scenario which I have explained? The communication between the threads is killing me.
I haven't tried running the DLL in a Separate Process using DLL Surrogates because frankly, I haven't looked at them and more importantly, I do not have the time.
---
Hakuna-Matada
It means no worries for the rest of your days...
It's our problem free, Philosophy
|
|
|
|
|
OK, why can't your window run as a modeless window on the caller's thread? Why do you need your own message loop? Can you not use the caller's message loop, or is your caller a worker thread in the calling application?
If you really must run your window on a separate thread, I'd use window messages to send commands from the COM object to the window using SendMessage or PostMessage (depending on whether the COM object needs a reply before returning - generally I'd go with PostMessage). The other direction is more tricky - you really have to get COM to do the marshalling for you. What you'd need to do is get hold of the interface pointers registered with the connection point and marshal them across to the window's thread, then use the unmarshalled pointers to actually fire the events from the window. The simplest way is probably to use the Global Interface Table - see Creating the Global Interface Table[^] in MSDN. You'll need to modify the auto-generated FireXxx functions.
If for some reason you want to do the marshalling yourself, see the CoMarshalInterThreadInterfaceInStream and CoGetInterfaceAndReleaseStream APIs.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
Thank You for your answer.
---
Hakuna-Matada
It means no worries for the rest of your days...
It's our problem free, Philosophy
|
|
|
|
|
Hi, I have a COM object written in C++ that contains a method that must modify a variable passed to it via ASP. In my C++ come method, I delcare the variable as type VARIANT*. This ideas works ok in C# and VB but fails in ASP. Is there a special way that you have to set up a variable in ASP to pass it to a COM object method for modification. In VB you must pass it by ref, but I am not sure what happens in ASP. The ASP error suggests that there is a type mismatch on the variable I am passing to my COM Object method. This is a bit confusing.. Can anyone help me sort this out?
I really appreciate it.
Thanks
Dave
|
|
|
|
|
Hi,
I am new to ATL and COM, i am learning the ATL COM and doing
doing some example programs using the articles give in codeproject.
When i was executing the Example program, just i kept the
breakpoint at DllMain function, to know when it is getting
called. When i registered using regsvr32, this is not being hit
and even when i added in the reference of the VB client or when using the
property of the DLL, it is not being called.
It will be helpful to know whenit will get called or link or artcile
link will be useful.
thanks in advance for the Help
Kumar
Kumar
|
|
|
|
|
You have to run an executable in the debugger for a breakpoint to be hit (unless it's a hardcoded breakpoint, an __asm int 3 in the code). If you're making a DLL then it is loaded into the "host" process: either the process instantiating the COM object or, when it's being registered, regsvr32 . If you want to debug the DLL registration process you have to debug regsvr32 . The project settings/debug section can help you here.
Steve
|
|
|
|
|
Hi Steve,
Thanks a lot. It works.. It did not strike me before...
-Gilly
|
|
|
|
|
Hello friends,
in web page www.adwords.com there is IFrame.
i want to get controls/elements of this IFrame.
How can i do that.
i written code::::
here g_lpHTMLDocument2 is IHTMLDocument2 pointer .
It contains valid document pointer.
IHTMLFramesCollection2Ptr pFrameColl;
hr = g_lpHTMLDocument2->get_frames(&pFrameColl);
if(SUCCEEDED(hr))
{
IDispatch *pDisp = NULL;
long lFrames = 0;
pFrameColl->get_length(&lFrames);
for ( long i = 0; i < lFrames ; i++ )
{
VARIANT varIndex, varDispatch;
VariantInit(&varIndex);
VariantInit(&varDispatch);
varIndex.vt = VT_I4;
varIndex.lVal = i;
if(SUCCEEDED(pFrameColl->item(&varIndex, &varDispatch)))
{
if(varDispatch.vt == VT_DISPATCH)
{
CComPtr <ihtmlwindow2> pHTMLWnd2;
if(SUCCEEDED(hr = varDispatch.pdispVal->QueryInterface(IID_IHTMLWindow2,(LPVOID*)&pHTMLWnd2)))
{
IHTMLDocument2Ptr pHTMLDoc2;
hr = pHTMLWnd2->get_document(&pHTMLDoc2);
//HERE I M GETTING NULL PONTER ,ERROR IS ACCESS DENIED
IHTMLElementCollection* pElementColl = NULL;
pHTMLDoc2->get_all(&pElementColl);
}
pHTMLDoc2 DOES NOT CONTAIN VALID ADDRESS.
Can someone help
Harry
|
|
|
|
|
If the enclosing document's URL is in a different domain than the frame's URL, IE won't let you access the frame, for security reasons.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
VB > soccer
|
|
|
|
|
Thanks a lot Mike,
but i have seen same thing is happened in other softwares.
is there any way to break this security.
waiting 4 reply.
|
|
|
|
|
It wouldn't be secure if there were a way for any old page/script to bypass it. Read up on cross-site scripting attacks to see why this security measure is important.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
VB > soccer
|
|
|
|
|
Thanks Mike,
Means it is not possible to access the frame document.
Harry.
|
|
|
|
|
I just only forbid contextmenu in mshtml control. The HtmlDocumentClass containt the event of HTMLDocumentEvents2_Event_oncontextmenu,so I write the code below ------------------------------------------
doc.HTMLDocumentEvents2_Event_oncontextmenu+=new mshtml.HTMLDocumentEvents2_oncontextmenuEventHandler(doc_HTMLDocumentEvents2_Event_oncontextmenu);
private bool doc_HTMLDocumentEvents2_Event_oncontextmenu(mshtml.IHTMLEventObj pEvtObj)
{
return false;
}
//
------------------------------------------------------
But the editor,mshtml control can not edit when the property of designMode is 'On' using keyboard.
Can you tell me why? And how to deal with the error? Thank you very!
Note:The question has been in the C# subject.
|
|
|
|
|
Hi all
My task is getting data of UPS using Windows Management Instrumentation(WMI) Win32_UnInterruptedPowerSupply class.
I have tested sample applications using WMI classes like Win32_Process,Win32_OperatingSystem they are working fine.But when iam trying to access Ups data using Win32_UnInterruptedPowerSupply class ,iam notgetting the output. I think there is problem with configuring UPS .
Please suggest and help me in configuring UPS and accessing UPS data.
Thanks
abhi
|
|
|
|
|
Hi all
My task is getting data of UPS using Windows Management Instrumentation(WMI) Win32_UnInterruptedPowerSupply class.
I have tested sample applications using WMI classes like Win32_Process,Win32_OperatingSystem they are working fine.But when iam trying to access Ups data using Win32_UnInterruptedPowerSupply class ,iam notgetting the output. I think there is problem with configuring UPS .
Please suggest and help me in configuring UPS and accessing UPS data.
Thanks
abhi
|
|
|
|
|
Hi,
I had started a COM DLL project about a year back. When I did a research on DLL back then, I found this is one of the more advance methods of doing it. Before I started the project, I was expecting it to be cross-platform and cross-language but it did not seems to be the case after I finish it. I had managed to get my DLL working for .Net, VB6 (with a bit of difficult for array passing) and VC6++ (which need extensive coding). I am still trying to figure out how to use it in Java.
I am currently wondering if this is the latest way of writing a DLL (I use the ATL COM wizard). I found out that VC6++ also offers MFC DLL and ActiveX ocx. Can anyone tell me what is the different between these 3? It seems that using the OCX method will help with reducing the coding when used in VC6++.
Again my questions are:
1. What is the latest method of writing a DLL?
2. What is the different between creating a DLL/OCX with ATL COM AppWizard, MFC ActiveX Control Wizard and MFC AppWizard?
Any information will be appreicated. As I am very keen in learning more about the technology available.
Thanks in advance.
|
|
|
|
|
Try to figure out what you want. DLL and COM have little relationship. DLL is a format of library and COM is a component technology. Almost all Windows libraries are delivered as DLLs, but only small portion of them are COM compoenets. You may create DLLs without knowing COM and COM components can be delivered as DLLs as well.
Do you want to create DLLs or COM components?
Best,
Jun
|
|
|
|
|
I know what I want, maybe you should read what I had written again before answering... Pardon me for my rudeness as I do realise that you seems to be very experience... one puzzling issue is that you recent replies do not seems to match your experience.. if fact it seems like an amateur
I said that I had did a COM DLL project, a DLL written in a form of COM component.
Currently what I found out was that written DLL in COM component is one of the latest way of writing a DLL. I just need to if there were any newer way of writing a DLL.
-- modified at 22:09 Wednesday 21st June, 2006
|
|
|
|
|
Just tried to help and learn.
I have to disagree that COM is a DLL technology. It's not.
Best,
Jun
|
|
|
|
|
Yes, I do understand that COM is not a DLL technology.
But it can be used to create a DLL and it is one of the more advance method of writing a DLL.
I was just hoping to know if there is any other methods to write a DLL other than this and the older method that exports the function.
I apologise for my rude reply again.
|
|
|
|
|
you can have a:
* .lib file which is a static linking library which exports c functions.
* win32 dll which exports c functions.
* mfc extension dll which exports c++ classes.
* com dll which exports ( export is not the right term but exposes)component
classes.
* u can have an ocx which again exports/exposes componentclasses
* u can have a .net managed dll which exports managed types.
All these are valid ways of creating a dll or a dynamic/static linking libraries.'
cheeeeeeeeeeeeeers ...milton kb.
|
|
|
|
|
Hello again,
how can i split CComBSTR Strings?
Example:
<br />
CComBSTR myStr = "iwantto-splitthis";<br />
Is it possible to split it by a special character like - to get two separate strings "iwantto" and "splitthis"?
Maybe I have to convert the CComBSTR to a readable format first.
<br />
LPTSTR myLptStr = new TCHAR[SysStringLen(myStr)];<br />
lstrcpy(myLptStr, OLE2T(myStr));<br />
But then, i still want to split it by -
Thanks in advance
|
|
|
|
|
The code you gave has problems. Firstly SysStringLen does not include an extra count for a NULL terminating character. You allocation should look like this:
LPTSTR myLptStr = new TCHAR[SysStringLen(myStr)+1];
Steve
|
|
|
|