如何在注册表中注册一个类? [英] How to register a class in the registry?

查看:201
本文介绍了如何在注册表中注册一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你的代码有什么问题我试图在注册表中注册类,在下面的方式,但我调试它显示类没有注册和应用程序崩溃。

Hi what's wrong with my code I am trying to register the class in registry in the following way but while I am debugging it shows that class is not registered and application crashes.

这段代码有什么问题请帮助我。

What is wrong in this code please Help me.

Test::IDiscover *pICalc = NULL;
HRESULT hRes=CoCreateInstance(Test::CLSID_SqlClass, NULL, 
  CLSCTX_INPROC_SERVER,Test::IID_IDiscover, reinterpret_cast<void**> (&pICalc)); 
if(hres<0) 
   cout<<"register failure"<<endl;
else
   cout<<"register success"<<endl;  // and i am not free the memory any where...

/ p>

And I also tried like:

IDiscoverPtr pt(__uuid(SqlClass));
HRESULT hRes=CoCreateInstance(Test::CLSID_SqlClass, NULL, 
  CLSCTX_INPROC_SERVER,Test::IID_IDiscover, reinterpret_cast<void**> (&pICalc));
if(hres<0)
   cout<<"register failure"<<endl;
else
   cout<<"register success"<<endl;

如果我这样做,当我调试这个

If I do like this when I debugging this at

IDiscoverPtr pt__uuid(SqlClass));

调试进入此函数并在内部显示COM错误。

The debugging goes to this function and shows COM error internally.

推荐答案

您正在尝试创建一个名为SqlClass的COM类的实例。你实际上并不试图注册该COM类。
要注册COM类,您需要执行

You are trying to create an instance of a COM class called SqlClass. You are not actually trying to register that COM class. To register the COM class, you would need to do

regsvr32 SqlClass.dll如果您的COM类是inprocess服务器

regsvr32 SqlClass.dll if your COM class is inprocess server

sqlclass.exe / regserver如果你的COM类是一个进程外的服务器。

sqlclass.exe /regserver if your COM class is a out-of-process server.

事实上,你使用COM智能指针告诉我你的SqlClass位于一个DLL,你正在使用#import来引用它。所以使用regsvr32 SqlClass.dll

The fact that you are using COM smart pointers tells me that your SqlClass lies in a DLL and you are using #import to refer to it. So use regsvr32 SqlClass.dll

另外一个方面是总是使用SUCCEEDED或FAILED宏来检查COM调用的hresult返回值。

An additional aspect is that always use the macros SUCCEEDED or FAILED to check the hresult return value of COM calls.

这篇关于如何在注册表中注册一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆