C# Com DLL 注册

DLL 注册

方式1:  regasm HWF_CLIENT_DLL.dll   【DLL要和 exe在同一个目录】

方式2:  regasm HWF_CLIENT_DLL.dll /regfile   在新机器上运行reg文件导入注册表   【DLL要和 exe在同一个目录】

for complile
regasm HWF_CLIENT_DLL.dll /tlb:HWF_CLIENT_DLL.tlb
copy "HWF_CLIENT_DLL.tlb" to the folder same as Wrapped_HWF_CLIENT_DLL.cpp i.e. ...\HWF_DLL\Wrapped_HWF_CLIENT_DLL\
Wrapped_HWF_CLIENT_DLL
for production
regasm HWF_CLIENT_DLL.dll /regfile

 

C++导入

#import "HWF_DLL.tlb" raw_interfaces_only
//#import "C:\TeamFoundationServer\CBNSIssuePC\srccode\HWF_DLL\HWF_DLL\bin\Debug\HWF_DLL.tlb" raw_interfaces_only
using namespace HWF_DLL;
#import "HWF_CLIENT_DLL.tlb" raw_interfaces_only
using namespace HWF_CLIENT_DLL;

C++使用

// Initialize COM.
    HRESULT hr = CoInitialize(NULL);
    IHWF_CLIENTPtr pHWF(__uuidof(HWF_CLIENT_Class));
    pHWF->GetHWFValue(str);   

 

 

 

How to register a C# or VB.Net DLL

After you created a C# or VB.Net DLL,  you need to register it on a target machine. You can use Microsoft's RegAsm.exe utility.

Regasm.exe is an Assembly Registration tool used to read the metadata within an assembly. It also adds the necessary entries to the  registry allowing a COM client (VB6 applications, or Microsoft VBA, eg. Access, Excel, etc) to create .NET Framework classes. Once a class is registered using Regasm.exe, a COM client can use  it as a COM component.

RegAsm.exe file comes with .Net framework installation and can be found in Microsoft.NET framework folder. There are different versions of RegAsm.exe. 

  1. .Net framework 2.0, 3.0, and 3.5 use the same RegAsm.exe which locates in the .Net framework V2.0 folder.

    C:\WINNT\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe

    Or

    C:\Windows\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe

     

  2. .Net framework 4.0 uses a new RegAsm.exe which locates in the .Net framework V4.0 folder.

    C:\WINNT\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe

    Or

    C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe

RegAsm.exe and DLL mapping

If you receive this error "RegAsm : error RA0000 : Failed to load 'c:\winnt\system32\YourDLLFile.dll' because it is not a valid .NET assembly", you are probably trying to use a .Net framework 2 RegAsm.exe to register a DLL that is created by using .Net framework 4. 

.Net FrameworkRegAsm.exe default installation pathYour DLL should be created by
.Net framework 2.0/3.0/3.5 C:\WINNT\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe .Net framework 2.0/3.0/3.5
.Net framework 4.0 C:\WINNT\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe .Net framework 4.0

So when registering DLL assemblies that are created by .Net framework 4, we must NOT use RegAsm.exe that comes in .Net framework 2.0/3.0/3.5 folder.

How to run RegAsm.exe

To execute RegAsm.exe, open a command prompt window, and navigate to the folder where RegAsm.exe is located and run it (otherwise you will get "RegAsm is not recognized as internal or external command, operable program or batch file" error message).

Assume I have already added my DLL to folder C:\WINNT\system32 then I can run the following command:

C:\WINNT\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe SimpleCalc.dll /codebase

Note that you don't need to specify C:\WINNT\system32 in the command as it's a system folder. RegAsm.exe will automatically look up SimpleCalc.dll in C:\WINNT\system32 directory. 

The /codebase parameter is an optional parameter that adds information about the DLL to the Windows registry which specifies the assembly's path on the disk.

Regasm can also be used to unregister a DLL.

If the DLL you got does not have type library file associated with it, one can be generated by using the Regasm utility and the /tlb option.

C:\WINNT\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe SimpleCalc.dll /tlb:SimpleCalc.tlb

Note that, to export a type library from the DLL, you need administrator privileges on the computer, or you will receive this type of error "RegAsm : error RA0000 : An error occurred while saving the exported type library: Access is denied..." because the account under which you run regasm.exe doesn't have rights to write to the folder.

Note that you can create environmental variables for the .Net framework RegAsm.exe to simplify DLL registration.

Happy Registering!
Copyright© GeeksEngine.com

posted @ 2017-02-04 15:10  高_山_流_水  阅读(652)  评论(0)    收藏  举报