By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,314 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,314 IT Pros & Developers. It's quick & easy.

mfc activex control in c# windows service

P: n/a
I keep getting this when trying to create an MFC activex control in a
c# windows service - anyone got any ideas what the missing module could
be???

Exception thrown : System.IO.FileNotFoundException: The specified
module could not be found.
at System.Windows.Forms.UnsafeNativeMethods.CoCreateI nstance(Guid&
clsid, Object punkOuter, Int32 context, Guid& iid)
at System.Windows.Forms.AxHost.CreateWithoutLicense()
at System.Windows.Forms.AxHost.CreateWithLicense(Stri ng license)
at System.Windows.Forms.AxHost.CreateInstance()
at System.Windows.Forms.AxHost.GetOcxCreate()
at System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)
at System.Windows.Forms.AxHost.CreateHandle()
at System.Windows.Forms.Control.CreateControl(Boolean
fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at MyService.MyService.Process() in s:\myservice\service1.cs:line 93

For more information, see Help and Support Center at
http://go.microsoft.com/fwlink/events.asp.
I have tried MANY different ways to use an MFC ActiveX control in my c#
windows service but I consistently come up against the above error
message. There can't be any missing modules since the ocx I am trying
to create is in the same directory as the #c service exe.

Many thanks for any help,
Andy

Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Andy,

1. Why do you use the AxHost class in a windows service? I am pretty sure
creating an interop assembly and instantiating the COM component instance
through the constructor exposed by its interop class is much more correct
solution.

2. The location of the OCX on disk is irrelevant. Given the OCX is properly
registered, it's location is stored in the registry so the system is able to
locate and load the .OCX file. If the OCX is not registered, no 'special'
location would allow the system to instantiate it with CoCreateInstance.

3. Can your OCX operate in non-visual mode? Windows services don't have any
GUI, they are even not allowed to interact with the desktop by default.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

<an*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I keep getting this when trying to create an MFC activex control in a
c# windows service - anyone got any ideas what the missing module could
be???

Exception thrown : System.IO.FileNotFoundException: The specified
module could not be found.
at System.Windows.Forms.UnsafeNativeMethods.CoCreateI nstance(Guid&
clsid, Object punkOuter, Int32 context, Guid& iid)
at System.Windows.Forms.AxHost.CreateWithoutLicense()
at System.Windows.Forms.AxHost.CreateWithLicense(Stri ng license)
at System.Windows.Forms.AxHost.CreateInstance()
at System.Windows.Forms.AxHost.GetOcxCreate()
at System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)
at System.Windows.Forms.AxHost.CreateHandle()
at System.Windows.Forms.Control.CreateControl(Boolean
fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at MyService.MyService.Process() in s:\myservice\service1.cs:line 93

For more information, see Help and Support Center at
http://go.microsoft.com/fwlink/events.asp.
I have tried MANY different ways to use an MFC ActiveX control in my c#
windows service but I consistently come up against the above error
message. There can't be any missing modules since the ocx I am trying
to create is in the same directory as the #c service exe.

Many thanks for any help,
Andy


Nov 16 '05 #2

P: n/a
Hi Dmitriy,
Thanks for your answers. I wanted to use an activeX so that I could
indirectly use a set of MFC dlls created for another application. The
ActiveX control links with these other dlls libraries and allows me to
use them. I thought it would be the easiest way to use these dlls from
a c# windows service...

You say I should create an interop assembly and instantiate the COM
component instance through the constructor exposed by its interop
class. Do you have any comments on how I can do this considering the
above requirement?

I thought I would have to alter the MFC dlls to be able to use them
directly in c#. I don't want to do this. I simply want to use the
functionality in thes dll's in a c# windows service.

Again, many thanks,
Andy

Nov 16 '05 #3

P: n/a
Andy,

My experience with MFC dates back to, I guess, something around 1999, so I
can miss some important points. Still, here goes.

If these DLLs are regular Win32 DLLs with declared exports, you can use
these DLLs as you would use Windows API dlls - through the DllImport
attribute.
Just make sure to specify correct calling convention - not sure this is
WINAPI for MFC dlls.

If you however want to use classes, going the MFC-based COM object is much
better as you cannot import C++ classes from a DLL in .NET.
In this case, make sure the COM is registered with regsvr32.exe, then, from
your C# project, go to the "Add Reference" dialog, switch to the "COM" tab
and choose your COM object from the list (or browse to the OCX). The IDE
will generate an interop assembly for you. You can also do the same with
command-line tlbimp.exe, if you want more control on namespaces and import
options.

Now that you have referenced the COM object and its interop asssembly has
been added to the list of references, launch Object Browser and examine the
namespace from the interop assembly.
You should see a class named something like MyObjectClass having a
parameterless constructor. This is the class you need to instantiate to
create an instance of your COM object. The class should have the same
interface your COM object has, so I think once you have an instance, the
rest should be simple.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

<an*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi Dmitriy,
Thanks for your answers. I wanted to use an activeX so that I could
indirectly use a set of MFC dlls created for another application. The
ActiveX control links with these other dlls libraries and allows me to
use them. I thought it would be the easiest way to use these dlls from
a c# windows service...

You say I should create an interop assembly and instantiate the COM
component instance through the constructor exposed by its interop
class. Do you have any comments on how I can do this considering the
above requirement?

I thought I would have to alter the MFC dlls to be able to use them
directly in c#. I don't want to do this. I simply want to use the
functionality in thes dll's in a c# windows service.

Again, many thanks,
Andy


Nov 16 '05 #4

P: n/a

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:%2***************@TK2MSFTNGP12.phx.gbl...
Andy,

My experience with MFC dates back to, I guess, something around 1999, so I
can miss some important points. Still, here goes.

If these DLLs are regular Win32 DLLs with declared exports, you can use
these DLLs as you would use Windows API dlls - through the DllImport
attribute.
Just make sure to specify correct calling convention - not sure this is
WINAPI for MFC dlls.

If you however want to use classes, going the MFC-based COM object is much
better as you cannot import C++ classes from a DLL in .NET.
In this case, make sure the COM is registered with regsvr32.exe, then,
from your C# project, go to the "Add Reference" dialog, switch to the
"COM" tab and choose your COM object from the list (or browse to the OCX).
The IDE will generate an interop assembly for you. You can also do the
same with command-line tlbimp.exe, if you want more control on namespaces
and import options.

Now that you have referenced the COM object and its interop asssembly has
been added to the list of references, launch Object Browser and examine
the namespace from the interop assembly.
You should see a class named something like MyObjectClass having a
parameterless constructor. This is the class you need to instantiate to
create an instance of your COM object. The class should have the same
interface your COM object has, so I think once you have an instance, the
rest should be simple.


Dmitriy,Andy

ActiveX controls can ONLY be hosted in a Windows Forms application and IE
(itself an ActiveX host), and only if they derive from
System.Windows.Forms.AxHost.

To achieve this you have to create a wrapper control using aximp.exe
file.dll or file.ocx. This wrapper control contains an instance of the
underlying ActiveX control. It knows how to communicate with the ActiveX
control, but it appears as a Windows Forms control. This generated control
hosts the ActiveX control and exposes its properties, methods, and events as
those of the generated control.

But here it comes, ActiveX controls (just like Windows.Forms) are not
designed to be used hosted in windows services, they need a STA to live in
and a thread that pumps messages. Unless they are based on a light weight
windowless control, they assume to run in a interactive user session, which
services by default do not.

So I my suggestion is - don't do this, it's a recipt for failure.

Willy.


Nov 16 '05 #5

P: n/a
Dmitriy / Willy,

Thanks for all your help - I've decided to go the DLLImport route after
all, seems the easiest way!

Cheers,
Andy

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.