2014-11-25 161 views
0

我真的坚持了这一点,并花费数小时的时间与谷歌和这样的我要问这里是因为我的想法后......使用COM DLL失败REGDB_E_CLASSNOTREG

使用this比如我现在有这样的:

#include <stdio.h> 
#include <tchar.h> 
// get ATL's client templates 
#include <atlcomcli.h> 
// import the typelibrary direct from the dll 
#import "FibuSvrDLL.dll" raw_interfaces_only no_namespace 

int main(int argc, char* argv[]) 
{ 
    // declare com ptr 
    CComPtr<_Server> comInterfacePtr; 

    // cocreate an instance 
    HRESULT r = comInterfacePtr.CoCreateInstance(__uuidof(Server)); 
    // this always fails with REGDB_E_CLASSNOTREG 
} 

_Server似乎是正确的接口和Server是它的组件类。看到这里,从生成TLH文件的相应片段:

struct __declspec(uuid("5f3a9f5c-196d-4e4c-b339-6664c1c7f4f9")) 
/* dual interface */ _Server; 
struct /* coclass */ Server; 

如果我使用同一个DLL从C#中它的作品开箱:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using fnSrvDLL5r; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     public Program() 
     { 
      try { 
       Server server = new Server(); 
       server.set_Konfiguration("..."); 
       [...] 

的DLL是一个第三方应用程序,我的一部分没有控制权。我所知道的是它是用VB6编写的。

在我能够从上面的c#代码中使用DLL之前,我需要首先使用regsvr32。也许这有任何帮助。

的问候,马丁

+0

是否注册COM服务器? – sharptooth 2014-11-25 15:02:23

+0

使用SysInternals的进程监视器,您将看到您的程序搜索注册表并且找不到密钥。对于C#应用程序和C++应用程序都这样做,差异将很容易看到。 – 2014-11-25 15:11:53

+0

它可能是你的C++程序是64位的,但服务器是32位的? – Nikolay 2014-11-25 15:15:26

回答

2

您的代码,如图不叫CoInitialize()OleInitialize()。没有先打电话,你永远不会成功。

+0

谢谢!事实上,这失踪了。现在它工作了! – 2014-11-26 09:58:00