Creating a COM interop using VS.NET 2005?

G

Guest

I am trying to create and use a COM object with C#.NET 2005.

The assembly is set to "Register for COM interop" but when I am trying to
call it from VB on Word 2003 I am getting this error:

Run-time error '-2147024894 (80070002)':
File or assembly name COMTest3, or one of its dependencies, was not found.

The code for the COM:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace COMTest3
{
[Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")]
public interface ComNameITInterface
{
[DispId(1)]
string GetMyName();
}

[Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ComNameITEvents
{
}

[Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ComNameITEvents))]

public class NameIT : ComNameITInterface
{
public string GetMyName()
{
return "ABCDEF";
}
}
}


The code for the VB to call the COM:

Private Sub Document_New()
Dim x As New COMTest3.NameIT
MsgBox x.GetMyName()
Set x = Nothing
End Sub

Thanks for any help,
Asaf
 
P

Peter Huang [MSFT]

Hi AG70,

Thanks for your update!
As I said in your previous post, the ComVisible = false by default, so even
if you has check the "Register for COM interop", the COM client will not
see the interface.
Please try the code below to build your .NET class library and expose to
com.
NOTE: I have set the ComVisible attribute to true in each class and
interface I need to expose.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;


namespace COMTest3
{
[Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch),ComVisible(true)]
public interface ComNameITInterface
{
[DispId(1)]
string GetMyName();
}


[Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),


InterfaceType(ComInterfaceType.InterfaceIsIDispatch),ComVisible(true)]
public interface ComNameITEvents
{
}


[Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ComNameITEvents)),ComVisible(true)]
public class NameIT : ComNameITInterface
{
public string GetMyName()
{
return "ABCDEF";
}
}



}



Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hello Peter,

Thanks you very much for your help.
I have posted this topic again because from some reason I haven't seen your
reply with Outlook express news reader and I haven't received a notification
for your reply to my email.

I will check your solution and will update you by news groups.

Thanks again for your help,
Asaf
 
G

Guest

Hello Peter,

I have created a new COM Interop object with C#.NET 2005 with
ComVisible(true) but stiil getting the error:

---------------------------
Microsoft Visual Basic
---------------------------
Run-time error '-2147024894 (80070002)':
File or assembly name COMTest5, or one of its dependencies, was not found.

My C# Code is:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace COMTest5
{
[Guid("E6750599-3B77-4606-970E-E05D590DD0AC"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch), ComVisible(true)]
public interface ComNameITInterface
{
[DispId(1)]
string GetMyName();
}

[Guid("BA9896AE-0386-4d3c-BCBF-2B0F7594495A"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch), ComVisible(true)]
public interface ComNameITEvents
{
}

[Guid("9DB718A0-629E-4e27-91AD-D47FF5160464"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ComNameITEvents)), ComVisible(true)]

public class NameIT : ComNameITInterface
{
public string GetMyName()
{
return "ABCDEF";
}
}
}

My VB Code is:

Private Sub Document_New()
Dim x As New COMTest5.NameIT
MsgBox x.GetMyName()
Set x = Nothing
End Sub


Regards,
Asaf





Asaf said:
Hello Peter,

Thanks you very much for your help.
I have posted this topic again because from some reason I haven't seen your
reply with Outlook express news reader and I haven't received a notification
for your reply to my email.

I will check your solution and will update you by news groups.

Thanks again for your help,
Asaf


"Peter Huang" said:
Hi AG70,

Thanks for your update!
As I said in your previous post, the ComVisible = false by default, so even
if you has check the "Register for COM interop", the COM client will not
see the interface.
Please try the code below to build your .NET class library and expose to
com.
NOTE: I have set the ComVisible attribute to true in each class and
interface I need to expose.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;


namespace COMTest3
{
[Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch),ComVisible(true)]
public interface ComNameITInterface
{
[DispId(1)]
string GetMyName();
}


[Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),


InterfaceType(ComInterfaceType.InterfaceIsIDispatch),ComVisible(true)]
public interface ComNameITEvents
{
}


[Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ComNameITEvents)),ComVisible(true)]
public class NameIT : ComNameITInterface
{
public string GetMyName()
{
return "ABCDEF";
}
}



}



Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Willy Denoyette [MVP]

Peter,

Note that the ComVisibleAttribute is not false by default, the attribute
default is still "true" , it's only set to 'false' in the VS generated
AssemblyInfo.cs file. Someone who's not using VS or (like me) who uses a
different template for the assemblyinfo.cs file might remove this setting.

Willy.
 
P

Peter Huang [MSFT]

Hi,

As Willy said, the ComVisibleAttribute is true by default, but because we
compiled the .NET library in IDE, there is an assemblyinfo.cs which will
set the ComVisibleAttribute to false in the assembly level, because
commonly we did not want to expose all the types in the whole assembly by
default.

Also for your scenario, it is strange because the code I post is exactly
what I have tested on my machine and it works.

So far to troubleshoot the problem can you help to perform the steps below?
1. Use the OleView tool to show the tlb definition for the assembly you are
generating. If we check the register for Com Interop, there will be a tlb
file generated by IDE which is located in the same directorty of the
generated .NET assembly.
the tool is located in the path below.
C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\Bin
oleview COMTest.tlb
And copy/paste all the information

2.Commonly in the simple test sample, it should use the .NET buildin
assembly without any customize DLL. However we still can use the Assembly
Binding Log Viewer tool to see if there is any binding error.
http://msdn2.microsoft.com/en-us/library/e74a18c4.aspx

3. You may try to use the Filemon and Regmon to see if there is any access
denied or not found error.
http://www.sysinternals.com/utilities/filemon.html
http://www.sysinternals.com/Utilities/Regmon.html


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hello Peter,

Here is the TypeLib details:

// Generated .IDL file (by the OLE/COM Object Viewer)
//
// typelib filename: COMTest5.tlb

[
uuid(7B0F6BE1-FC1D-42E1-B818-B5F2045B0FFA),
version(1.0),
custom(90883F05-3D28-11D2-8F17-00A0C9A6186D, COMTest5, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null)

]
library COMTest5
{
// TLib : // TLib : mscorlib.dll :
{BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}
importlib("mscorlib.tlb");
// TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
importlib("stdole2.tlb");

// Forward declare all types defined in this typelib
dispinterface ComNameITInterface;
dispinterface ComNameITEvents;

[
uuid(E6750599-3B77-4606-970E-E05D590DD0AC),
version(1.0),
custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9,
COMTest5.ComNameITInterface)

]
dispinterface ComNameITInterface {
properties:
methods:
[id(0x00000001)]
BSTR GetMyName();
};

[
uuid(BA9896AE-0386-4D3C-BCBF-2B0F7594495A),
version(1.0),
custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, COMTest5.ComNameITEvents)


]
dispinterface ComNameITEvents {
properties:
methods:
};

[
uuid(9DB718A0-629E-4E27-91AD-D47FF5160464),
version(1.0),
custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, COMTest5.NameIT)
]
coclass NameIT {
interface _Object;
[default] dispinterface ComNameITInterface;
[default, source] dispinterface ComNameITEvents;
};
};


Regards,
Asaf
 
P

Peter Huang [MSFT]

Hi AG70,

The tlb file seems to be OK.
Have you tried another two suggestions in my last post?
Also please try the sample below to see if that works for you.
COM Interop Sample: COM Client and .NET Server
http://msdn2.microsoft.com/en-us/library/2w30w8zx.aspx

If the problem persists, I guess there is something wrong with .NET Com
interop layer, I suggest you try reinstall .NET framework 2.0 even the
whole vs.net 2005.

Thanks for your understanding!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hello Peter,

I have reinstall .NET Framework 2.0 and VS.NET 2005 Pro and still I have the
same problem.
Do I need to generate an <Assembly: AssemblyKeyFile("sample.snk")> ?

Regards,
Asaf
 
P

Peter Huang [MSFT]

Hi

The AssemblyKeyFile is not necessary for a test COM interop program.
Also have you tried the Fuslogvw.exe tool and the filemon,regmon, which may
tell us what library is missing?

To do further troubleshooting, I think you may need to contact MSPSS which
will do live debugging or dump analysis.
http://support.microsoft.com

Thanks for your understanding!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hello Peter,

From some reason if I read the Filemon log correctly the application is
looking for files in the .NET 1.1 instead of .NET 2.0 when I try to run my
simple COM interop from Word 2003.
Both VS.NET 2003 & VS.NET 2005 are installed on my machine.

Here is the log for Filemon (Tab delimiter)

1 10:56:35 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Microsoft
Office\OFFICE11\1037\srintl.dll NOT FOUND Attributes: Error
2 10:56:35 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Common
Files\Microsoft Shared\office11\1037\srintl.dll NOT FOUND Attributes: Error
3 10:56:35 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Microsoft
Office\OFFICE11\1037\srintl.dll NOT FOUND Attributes: Error
4 10:56:35 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Common
Files\Microsoft Shared\office11\1037\srintl.dll NOT FOUND Attributes: Error
5 10:56:35 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Microsoft
Office\OFFICE11\1037\srintl.dll NOT FOUND Attributes: Error
6 10:56:35 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Common
Files\Microsoft Shared\office11\1037\srintl.dll NOT FOUND Attributes: Error
1017 10:56:38 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Microsoft
Office\OFFICE11\mscoree.dll NOT FOUND Attributes: Error
1021 10:56:38 WINWORD.EXE:1472 QUERY
INFORMATION C:\WINDOWS\system32\mscoree.dll.local NOT FOUND Attributes: Error
1022 10:56:38 WINWORD.EXE:1472 OPEN C:\Program Files\Microsoft
Office\OFFICE11\WINWORD.EXE.config NOT FOUND Options: Open Access: All
1190 10:56:38 WINWORD.EXE:1472 QUERY
INFORMATION C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\fusion.localgac NOT
FOUND Attributes: Error
1191 10:56:38 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Microsoft
Office\OFFICE11\URLMON.DLL NOT FOUND Attributes: Error
1192 10:56:38 WINWORD.EXE:1472 QUERY INFORMATION C:\Documents and
Settings\asafgo\My Documents\URLMON.DLL NOT FOUND Attributes: Error
1290 10:56:38 WINWORD.EXE:1472 READ C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\config\machine.config END OF FILE Offset: 211287 Length: 16365
1292 10:56:38 WINWORD.EXE:1472 OPEN C:\Program Files\Microsoft
Office\OFFICE11\WINWORD.EXE.config NOT FOUND Options: Open Access: All
1354 10:56:38 WINWORD.EXE:1472 DIRECTORY C:\windows\microsoft.net\framework\v1.1.4322\ NO SUCH FILE FileBothDirectoryInformation: mscorlib.INI
1397 10:56:38 WINWORD.EXE:1472 UNLOCK C:\WINDOWS\assembly\NativeImages1_v1.1.4322\mscorlib\1.0.5000.0__b77a5c561934e089_9441f284\__AssemblyInfo__.ini RANGE NOT LOCKED Offset: 0 Length: -1
1403 10:56:38 WINWORD.EXE:1472 UNLOCK C:\WINDOWS\assembly\NativeImages1_v1.1.4322\mscorlib\1.0.5000.0__b77a5c561934e089_9441f284\__AssemblyInfo__.ini RANGE NOT LOCKED Offset: 0 Length: -1
1409 10:56:38 WINWORD.EXE:1472 UNLOCK C:\WINDOWS\assembly\NativeImages1_v1.1.4322\mscorlib\1.0.5000.0__b77a5c561934e089_9441f284\__AssemblyInfo__.ini RANGE NOT LOCKED Offset: 0 Length: -1
1469 10:56:38 WINWORD.EXE:1472 DIRECTORY C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\ NO SUCH FILE FileBothDirectoryInformation: mscoree.dll
1503 10:56:38 WINWORD.EXE:1472 QUERY
INFORMATION C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\ole32.dll NOT
FOUND Attributes: Error
1504 10:56:38 WINWORD.EXE:1472 QUERY
INFORMATION C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\ole32.dll NOT
FOUND Attributes: Error
1514 10:56:38 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Microsoft
Office\OFFICE11\WINWORD.EXE.config NOT FOUND Attributes: Error
1518 10:56:38 WINWORD.EXE:1472 OPEN C:\WINDOWS\assembly\GAC\PublisherPolicy.tme NOT FOUND Options: Open Access: All
1523 10:56:38 WINWORD.EXE:1472 QUERY INFORMATION C:\Documents and
Settings\asafgo\Local Settings\Application
Data\ApplicationHistory\WINWORD.EXE.1ca0ce52.ini NOT FOUND Attributes: Error
1524 10:56:38 WINWORD.EXE:1472 OPEN C:\Documents and Settings\asafgo\Local
Settings\Application Data\ApplicationHistory\WINWORD.EXE.1ca0ce52.ini NOT
FOUND Options: Open Access: All
1525 10:56:38 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Microsoft
Office\OFFICE11\COMTest5.DLL NOT FOUND Attributes: Error
1526 10:56:38 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Microsoft
Office\OFFICE11\COMTest5\COMTest5.DLL PATH NOT FOUND Attributes: Error
1527 10:56:38 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Microsoft
Office\OFFICE11\COMTest5.EXE NOT FOUND Attributes: Error
1528 10:56:38 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Microsoft
Office\OFFICE11\COMTest5\COMTest5.EXE PATH NOT FOUND Attributes: Error
1541 10:56:38 WINWORD.EXE:1472 QUERY
INFORMATION C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\en-US\MSCORRC.DLL PATH NOT FOUND Attributes: Error
1542 10:56:38 WINWORD.EXE:1472 QUERY
INFORMATION C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\en-US\MSCORRC.DLL PATH NOT FOUND Attributes: Error
1543 10:56:38 WINWORD.EXE:1472 QUERY
INFORMATION C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\en-US\MSCORRC.DLL.DLL PATH NOT FOUND Attributes: Error
1544 10:56:38 WINWORD.EXE:1472 QUERY
INFORMATION C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\en\MSCORRC.DLL PATH
NOT FOUND Attributes: Error
1545 10:56:38 WINWORD.EXE:1472 QUERY
INFORMATION C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\en\MSCORRC.DLL PATH
NOT FOUND Attributes: Error
1546 10:56:38 WINWORD.EXE:1472 QUERY
INFORMATION C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\en\MSCORRC.DLL.DLL PATH NOT FOUND Attributes: Error
1634 10:56:38 WINWORD.EXE:1472 QUERY
INFORMATION C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\OLEAUT32.dll NOT
FOUND Attributes: Error
1635 10:56:38 WINWORD.EXE:1472 QUERY
INFORMATION C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\OLEAUT32.dll NOT
FOUND Attributes: Error
1648 10:56:38 WINWORD.EXE:1472 OPEN C:\WINDOWS\WINHELP.INI NOT
FOUND Options: Open Access: All
1649 10:56:38 WINWORD.EXE:1472 OPEN C:\WINDOWS\WINHELP.INI NOT
FOUND Options: Open Access: All
2659 10:56:40 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Microsoft
Office\OFFICE11\1037\srintl.dll NOT FOUND Attributes: Error
2660 10:56:40 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Common
Files\Microsoft Shared\office11\1037\srintl.dll NOT FOUND Attributes: Error
2661 10:56:40 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Microsoft
Office\OFFICE11\1037\srintl.dll NOT FOUND Attributes: Error
2662 10:56:40 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Common
Files\Microsoft Shared\office11\1037\srintl.dll NOT FOUND Attributes: Error
2663 10:56:40 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Microsoft
Office\OFFICE11\1037\srintl.dll NOT FOUND Attributes: Error
2664 10:56:40 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Common
Files\Microsoft Shared\office11\1037\srintl.dll NOT FOUND Attributes: Error
2669 10:56:41 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Microsoft
Office\OFFICE11\1037\srintl.dll NOT FOUND Attributes: Error
2670 10:56:41 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Common
Files\Microsoft Shared\office11\1037\srintl.dll NOT FOUND Attributes: Error
2671 10:56:41 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Microsoft
Office\OFFICE11\1037\srintl.dll NOT FOUND Attributes: Error
2672 10:56:41 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Common
Files\Microsoft Shared\office11\1037\srintl.dll NOT FOUND Attributes: Error
2673 10:56:41 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Microsoft
Office\OFFICE11\1037\srintl.dll NOT FOUND Attributes: Error
2674 10:56:41 WINWORD.EXE:1472 QUERY INFORMATION C:\Program Files\Common
Files\Microsoft Shared\office11\1037\srintl.dll NOT FOUND Attributes: Error
 
G

Guest

Hello Peter,

According to Filemon:

WINWORD.EXE:1472
QUERY INFORMATION
C:\Program Files\Microsoft Office\OFFICE11\COMTest5.DLL
NOT FOUND

I have placed the COMTest5.DLL in the C:\Program Files\Microsoft
Office\OFFICE11 directory and now when I try to run my code I receive this
new error:


Run-time error '-2146234105 (80131107)':
The format of the file 'COMTest5' is invalid.

According to the full log of Filemon the COM is try to operate from .NET
Framework 1.1 instead of version 2.0.

Regards,
Asaf
 
W

Willy Denoyette [MVP]

Register your assembly with regasm.exe /tlb /codebase comtest5.dll

But you should un-register them before you move the assembly to another
directory.

Willy.
 
G

Guest

Hello Willy,

I have unregister the COMTest5.dll, copied it to the office11 directory and
registered it with the TLB and still I am getting the error:

Run-time error '-2146234105 (80131107)'
The format of the file 'COMTest5' is invalid.

Regards,
Asaf
 
P

Peter Huang [MSFT]

Hi AG70,

As Willy said, you may try to regasm /codebase to register the assembly, so
that the full path will be registered.

Also according your description, your dll is working with .NET 1.1. in
Winword.

So we can put an app.config with <supportedRuntime> Element in the
winword.exe dir and change the config file as winword.exe.config.

How do I get a .NET 1.1 app to run on .NET 2.0
http://groups.google.com/group/microsoft.public.dotnet.framework.sdk/browse_
frm/thread/3c4286a1e2887e3b/a05c57869aa3d896?lnk=st&q=supportedRuntime+%22v-
phuang%22+winword.exe&rnum=3&hl=en#a05c57869aa3d896

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hello Peter, Hello Willy,

I have just installed VB6 on my machine and I am able to call & use my test
COM interop without any special modifications from VB6 application.

Thanks you both for your time and share of knowledge

Regards,
Asaf
 
P

Peter Huang [MSFT]

Hi,

Thanks for your quickly reply!
I just want to confirm if you still have any concern with the issue,
because the VBA and VB use the similar mechanism to call the .NET exposed
COM Object.
If you still have any concern, please feel free to post here.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top