Simple example of importing com methods 
Author Message
 Simple example of importing com methods

I was wondering if C# has a way to import type libraries so that they can be
used in my program.

I can't get the samples to load from my .net CD's because it keeps asking
for Disk 3 and well, it is in there.

Anyway, I would like to do something as simple as import the typelibrary for
Microsoft Outlook and get access to the Outlook Application Object.

Any quick samples on this subject?

Paul



Sun, 21 Mar 2004 23:09:15 GMT  
 Simple example of importing com methods


Quote:
> I was wondering if C# has a way to import type libraries so that they can be
> used in my program.

> I can't get the samples to load from my .net CD's because it keeps asking
> for Disk 3 and well, it is in there.

> Anyway, I would like to do something as simple as import the typelibrary for
> Microsoft Outlook and get access to the Outlook Application Object.

> Any quick samples on this subject?

Using VS.NET, you can simply add a reference to a COM object and VS.NET
will run the "tlbimp" (Type Library Import) utility to generate a .NET
wrapper for COM.

If you don't have VS.NET, you can use tlbimp from the command line to
generate the wrapper.

--
Patrick Steele



Mon, 22 Mar 2004 01:45:37 GMT  
 Simple example of importing com methods

Quote:
> If you don't have VS.NET, you can use tlbimp from the command line to
> generate the wrapper.

Thanks,
     After I generate a wrapper for Outlook, I get an outlook.dll.  Now, how
do I "Use" Outlook.dll in my code,  I would think that it would be as simple
as
using Outlook.dll
or
using Outlook

However, it doesn't recognize the namespace.  I need to tell the source
about the imported dll somehow?  Where do I find information about that?

Thanks,

Paul



Mon, 22 Mar 2004 02:19:03 GMT  
 Simple example of importing com methods
When you import the type libary you also need to  create the namespace:

tlbimp Outlook.dll /namespace:MyOutlook

Now just use the namespace,

using MyOutlook;

Kirk


Quote:

> > If you don't have VS.NET, you can use tlbimp from the command line to
> > generate the wrapper.

> Thanks,
>      After I generate a wrapper for Outlook, I get an outlook.dll.  Now,
how
> do I "Use" Outlook.dll in my code,  I would think that it would be as
simple
> as
> using Outlook.dll
> or
> using Outlook

> However, it doesn't recognize the namespace.  I need to tell the source
> about the imported dll somehow?  Where do I find information about that?

> Thanks,

> Paul



Mon, 22 Mar 2004 02:48:32 GMT  
 Simple example of importing com methods
Well, that sounds simple enough..  But when I compile in the ide, I get the
following message.
The type or namespace name 'Outlook' could not be found (are you missing a
using directive or an assembly reference?)
<<

the generated dll is in the same directory.  should it put it somewhere
else??

Paul


Quote:
> When you import the type libary you also need to  create the namespace:

> tlbimp Outlook.dll /namespace:MyOutlook

> Now just use the namespace,

> using MyOutlook;

> Kirk



Mon, 22 Mar 2004 03:24:45 GMT  
 Simple example of importing com methods
Here is what I did.

C:\csharp\imports>tlbimp "C:\Program Files\Microsoft
Office\Office\MSOUTL9.OLB"
/namespace:Outlook
TlbImp - Type Library to .NET Assembly Converter Version 1.0.2914.16
Copyright (C) Microsoft Corp. 2001.  All rights reserved.

Type library imported to Outlook.dll
<<

then I after I copied the dll into the same dir that my exe is located, I
added

using Outlook;

and I get the error
C:\csharp\firsttest\WindowsApplication1\Form1.cs(6): The type or namespace
name 'Outlook' could not be found (are you missing a using directive or an
assembly reference?)

<<

Is it a matter of placement of the dll?  What else do I need to do.

I have been able to get this working by manually adding a reference to the
references, but the problem is that now it appears that I will have to
install my programs on a target machine in the exact directory structure
that I developed with.  I figured that it would locate my dll regardless of
where it was as long as it is available in the path.

Paul


Quote:
> When you import the type libary you also need to  create the namespace:

> tlbimp Outlook.dll /namespace:MyOutlook

> Now just use the namespace,

> using MyOutlook;

> Kirk



Mon, 22 Mar 2004 03:37:29 GMT  
 Simple example of importing com methods


Quote:

> > If you don't have VS.NET, you can use tlbimp from the command line to
> > generate the wrapper.

> Thanks,
>      After I generate a wrapper for Outlook, I get an outlook.dll.  Now, how
> do I "Use" Outlook.dll in my code,  I would think that it would be as simple
> as
> using Outlook.dll
> or
> using Outlook

> However, it doesn't recognize the namespace.  I need to tell the source
> about the imported dll somehow?  Where do I find information about that?

Add a reference to it in your project.  Or, if you're using the command-
line compiler, include the reference when compiling:

csc /r:outlook.dll ...

--
Patrick Steele



Mon, 22 Mar 2004 19:56:27 GMT  
 Simple example of importing com methods
Patrick, Thanks!

Is there any way to make the executable use the dll in any place other than
the executable directory?  It seems to defeat the purpose of a dll if I
can't simply make the DLL available in the path somewhere??  I would think
it would be silly to have several directories that had the same dll in them.
However because of custom namespace names etc, I can also see why this would
be a problem.

Quote:
> Add a reference to it in your project.  Or, if you're using the command-
> line compiler, include the reference when compiling:

> csc /r:outlook.dll ...

> --
> Patrick Steele



Mon, 22 Mar 2004 22:05:28 GMT  
 Simple example of importing com methods


Quote:
> Patrick, Thanks!

> Is there any way to make the executable use the dll in any place other than
> the executable directory?

I don't think so.  The DLL is either in the same directory as the EXE or
it's a shared EXE that sits in the Global Assembly Cache (GAC).  By
keeping all of the files together in one directory, XCOPY deployment is
supposed to be possible.

Quote:
> It seems to defeat the purpose of a dll if I
> can't simply make the DLL available in the path somewhere??  I would think
> it would be silly to have several directories that had the same dll in them.

Yes, but disk space is pretty cheap these days.  :)

And it allows you to delete an app simply by deleting the entire
directory (including the DLL) and no other apps break since you've
removed the DLL.  And you also don't need to worry about{*filter*} on to a
DLL "just in case" some other app might use it.

--
Patrick Steele



Mon, 22 Mar 2004 23:27:46 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Simple ATL Com object problem on simplest examples

2. a simple question on raw method with #import Mylib.tlb

3. simple example of com in c++

4. simple ole.com sample requested / no methods exposed

5. Can't import com interface using #import

6. IE4 Preview2 COM interface != VC++ 5.0 native COM (#import compile error)

7. COM/ATL novice:passing object pointers through methods on a COM interface

8. Return a pointer to a nested COM object from a COM object method

9. Enable COM clients call methods inside COM events

10. COM client crashes calling COM method

11. File Import & graphic Display Example

12. File Import file & DrawCli Example

 

 
Powered by phpBB® Forum Software