返回> 网站首页
COM组件注册方法
yoours2013-01-10 16:21:21
简介一边听听音乐,一边写写文章。
COM组件注册方法
1. 创建安装程序
2. 使用VS创建安装程序,加入相关的文件和DLL。
3. 选中需要注册的DLL->属性->Register = "COM Self Register"
4. C#编程注册方法
[DllImport("Kernel32")]
public static extern IntPtr GetProcAddress(int handle, String funcname);
[DllImport("Kernel32")]
public static extern int LoadLibrary(String funcname);
[DllImport("Kernel32")]
public static extern int FreeLibrary(int handle);
public delegate int RegisterServerDelegate();
public delegate int UnRegisterServerDelegate();
private void RegisterDLL()
{
int hLib = LoadLibrary("UCap_CE50.dll");
if(hLib == 0)
{
return;
}
IntPtr fptr = GetProcAddress(hLib, "DllRegisterServer");
RegisterServerDelegate RegisterServer = (Delegate)Marshal.GetDelegateForFunctionPointer(fptr, typeof(RegisterServerDelegate));
RegisterServer();
FreeLibrary(hLib);
}
private void Unregister()
{
int hLib = LoadLibrary("UCap_CE50.dll");
if(hLib == 0)
{
return;
}
IntPtr fptr = GetProcAddress(hLib, "DllRegisterServer");
UnRegisterServerDelegate UnRegisterServer = (Delegate)Marshal.GetDelegateForFunctionPointer(fptr, typeof(UnRegisterServerDelegate));
UnRegisterServer();
FreeLibrary(hLib);
}
private static Delegate GetAddress(int dllModule, string functionname, Type t)
{
int addr = GetProcAddress(dllModule, functionname);
if (addr == 0)
return null;
else
return Marshal.GetDelegateForFunctionPointer(newIntPtr(addr), t);
}
文章评论
1514人参与,0条评论