C++ 如何调试Excel';正在加载COM服务器吗?

C++ 如何调试Excel';正在加载COM服务器吗?,c++,excel,windows,dll,com,C++,Excel,Windows,Dll,Com,我已经成功地编写了一个32位代码。这是在Excel fine中加载的,在单元格中键入=RTD(…)。但是,我的同一代码的64位构建不起作用,我想知道如何调试它。我找不到任何相关文件 我有一个64位RTD服务器DLL。Dependency walker没有显示任何缺少DLL依赖项的问题,事实上,如果我链接一个玩具可执行文件,我可以很好地调用这个DLL DLL已成功注册到regsvr32 Excel甚至会看到它,但会将其列为“非活动”。在过去,在开发32位版本时,这通常是由于缺少DLL依赖项(但没有

我已经成功地编写了一个32位代码。这是在Excel fine中加载的,在单元格中键入
=RTD(…)
。但是,我的同一代码的64位构建不起作用,我想知道如何调试它。我找不到任何相关文件

我有一个64位RTD服务器DLL。Dependency walker没有显示任何缺少DLL依赖项的问题,事实上,如果我链接一个玩具可执行文件,我可以很好地调用这个DLL

DLL已成功注册到regsvr32

Excel甚至会看到它,但会将其列为“非活动”。在过去,在开发32位版本时,这通常是由于缺少DLL依赖项(但没有错误消息)而发生的。如前所述,我可以链接到DLL,依赖项walker不会显示任何问题

我还可以做些什么来调试这个问题?如果它是开源的,我会查看Excel源代码并尝试做它正在做的事情,但显然这不是一个选项


相同的代码生成32位DLL,32位Excel可以正常运行。但是64位Excel似乎无法使用64位DLL。

问题在于DLL的注册表项。为了回答我自己的问题(并且在了解了比我想了解的更多的COM之后),最好的方法是编写一个COM客户机应用程序,并尝试以几种方式实例化COM服务器。对于RTD,下面是我使用的一个示例客户端应用程序。如果有人有类似的问题,我建议首先尝试使用
CoCreateInstance
,然后看看是否可以从ProgId获取CLSID,然后使用ProgId创建实例,因为Excel就是这样做的。相应地替换UUID和
“VCRTDServer.RTDFunctions”
,无论您的程序ID是什么。守则:

/**
   Small test program to check that the COM DLL was properly
   registered
 */

#include    <objbase.h>

#ifndef RTD_ARCH
#    define RTD_ARCH 32
#endif

//
//Here we do a #import on the DLL ,you can also do a #import on the .TLB
//The #import directive generates two files in the output folders.
//
#import  "bin\\VCRTDServer.dll"

#include <iostream>
#include <stdexcept>
#include <string>
#include <tchar.h>
#include "IRTDServer_h.h"

using namespace std;


#define PROGID _T("VCRTDServer.RTDFunctions")
#define CLSID_STRING _T("{8D2EEA35-CBEB-49b1-8F3E-68C8F50F38D8}")

const CLSID CLSID_RTD = {0x8D2EEA35, 0xCBEB, 0x49B1,
                         {0x8F, 0x3E, 0x68, 0xC8, 0xF5, 0x0F, 0x38, 0xD8}};

const CLSID IID_RTD_UpdateEvent = {0xa43788c1, 0xd91b, 0x11d3,
                                   0x8f, 0x39, 0x00, 0xc0, 0x4f, 0x36, 0x51, 0xb8};

const CLSID IID_RTD = {0xec0e6191, 0xdb41, 0x11d3,
                       0x8f, 0xe3, 0x00, 0xc0, 0x4f, 0x36, 0x51, 0xb8};

static string GetLastErrorAsString();
static void run();

int main() {
    try {
        run();
        CoUninitialize();
        return 0;
    } catch(const exception& ex) {
        cerr << "Error: " << ex.what() << endl;
        CoUninitialize();
        return 1;
    }
}

void run() {
    cout << "CoInitializing" << endl;
    CoInitialize(nullptr);

    // if CoCreateInstance doesn't work, nothing else will
    // cout << "Checking CoCreateInstance" << endl;
    // IRtdServer *obj;
    // const auto hr = CoCreateInstance(CLSID_RTD,
    //                                  nullptr,
    //                                  CLSCTX_INPROC_SERVER,
    //                                  IID_RTD_UpdateEvent,
    //                                  (void**)&obj);
    // if(hr != S_OK)
    //     throw runtime_error("CoCreateInstance failed: " + GetLastErrorAsString());

    cout << "Converting prog id to clsid" << endl;
    CLSID clsid;
    const auto ptoc_res = CLSIDFromProgID(L"VCRTDServer.RTDFunctions", &clsid);
    if(ptoc_res != S_OK)
        throw runtime_error("CLSID error: " +  GetLastErrorAsString());
    cout << "Printing out class ID" << endl;
    cout << "Class ID: " << *((int*)&clsid) << endl;

    LPOLESTR progId;
    const auto progIdResult = ProgIDFromCLSID(
        CLSID_RTD, &progId);
    if(progIdResult != S_OK)
        throw runtime_error("Prog ID error: " +  GetLastErrorAsString());

     char buf[40];
     WideCharToMultiByte(CP_ACP, NULL, progId, -1, buf, 40, NULL, NULL);
     cout << "prog id is '" << buf << "'" << endl;

    cout << "Creating instance" << endl;
    RTDServerLib::IRtdServerPtr rtdServer;
    if(rtdServer.CreateInstance(CLSID_RTD) != S_OK)
        throw runtime_error("Could not create instance: " +
                            GetLastErrorAsString());

    cout << "Starting RTD server" << endl;
    const auto startResult = rtdServer->ServerStart(nullptr);
    cout << "Start result was: " << startResult << endl;
}



//Returns the last Win32 error, in string format. Returns an empty string if there is no error.
std::string GetLastErrorAsString() {
    //Get the error message, if any.
    const auto errorMessageID = ::GetLastError();
    if(errorMessageID == 0)
        return {}; //No error message has been recorded

    LPSTR messageBuffer = nullptr;
    size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                                 NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, nullptr);

    std::string message(messageBuffer, size);

    //Free the buffer.
    LocalFree(messageBuffer);

    return message;
}
/**
用于检查COM DLL是否正确运行的小测试程序
登记
*/
#包括
#ifndef RTD_拱
#定义RTD_拱32
#恩迪夫
//
//这里我们在DLL上进行导入,您也可以在.TLB上进行导入
//#import指令在输出文件夹中生成两个文件。
//
#导入“bin\\VCRTDServer.dll”
#包括
#包括
#包括
#包括
#包括“IRTDServer_h.h”
使用名称空间std;
#定义PROGID_T(“VCRTDServer.RTDFunctions”)
#定义CLSID_字符串(“{8D2EEA35-CBEB-49b1-8F3E-68C8F50F38D8}”)
常数CLSID CLSID_RTD={0x8D2EEA35,0xCBEB,0x49B1,
{0x8F,0x3E,0x68,0xC8,0xF5,0x0F,0x38,0xD8};
常量CLSID IID_RTD_UpdateEvent={0xa43788c1、0xd91b、0x11d3,
0x8f、0x39、0x00、0xc0、0x4f、0x36、0x51、0xb8};
常数CLSID IID_RTD={0xec0e6191,0xdb41,0x11d3,
0x8f、0xe3、0x00、0xc0、0x4f、0x36、0x51、0xb8};
静态字符串GetLastErrorAsString();
静态无效运行();
int main(){
试一试{
run();
coninitialize();
返回0;
}捕获(常量异常和ex){

cerr问题是DLL的注册表项。回答我自己的问题(并且在了解了比我想了解的更多的COM之后),最好的方法是编写一个COM客户端应用程序,并尝试以几种方式实例化COM服务器。对于RTD,下面是我使用的一个示例客户端应用程序。如果有人有类似的问题,我建议首先尝试使用
CoCreateInstance
,然后查看是否可以从ProgId获取CLSID,然后使用ProgId、sinc创建实例e Excel就是这样做的。相应地替换UUID和
“VCRTDServer.RTDFunctions”
,使用您的程序ID。代码:

/**
   Small test program to check that the COM DLL was properly
   registered
 */

#include    <objbase.h>

#ifndef RTD_ARCH
#    define RTD_ARCH 32
#endif

//
//Here we do a #import on the DLL ,you can also do a #import on the .TLB
//The #import directive generates two files in the output folders.
//
#import  "bin\\VCRTDServer.dll"

#include <iostream>
#include <stdexcept>
#include <string>
#include <tchar.h>
#include "IRTDServer_h.h"

using namespace std;


#define PROGID _T("VCRTDServer.RTDFunctions")
#define CLSID_STRING _T("{8D2EEA35-CBEB-49b1-8F3E-68C8F50F38D8}")

const CLSID CLSID_RTD = {0x8D2EEA35, 0xCBEB, 0x49B1,
                         {0x8F, 0x3E, 0x68, 0xC8, 0xF5, 0x0F, 0x38, 0xD8}};

const CLSID IID_RTD_UpdateEvent = {0xa43788c1, 0xd91b, 0x11d3,
                                   0x8f, 0x39, 0x00, 0xc0, 0x4f, 0x36, 0x51, 0xb8};

const CLSID IID_RTD = {0xec0e6191, 0xdb41, 0x11d3,
                       0x8f, 0xe3, 0x00, 0xc0, 0x4f, 0x36, 0x51, 0xb8};

static string GetLastErrorAsString();
static void run();

int main() {
    try {
        run();
        CoUninitialize();
        return 0;
    } catch(const exception& ex) {
        cerr << "Error: " << ex.what() << endl;
        CoUninitialize();
        return 1;
    }
}

void run() {
    cout << "CoInitializing" << endl;
    CoInitialize(nullptr);

    // if CoCreateInstance doesn't work, nothing else will
    // cout << "Checking CoCreateInstance" << endl;
    // IRtdServer *obj;
    // const auto hr = CoCreateInstance(CLSID_RTD,
    //                                  nullptr,
    //                                  CLSCTX_INPROC_SERVER,
    //                                  IID_RTD_UpdateEvent,
    //                                  (void**)&obj);
    // if(hr != S_OK)
    //     throw runtime_error("CoCreateInstance failed: " + GetLastErrorAsString());

    cout << "Converting prog id to clsid" << endl;
    CLSID clsid;
    const auto ptoc_res = CLSIDFromProgID(L"VCRTDServer.RTDFunctions", &clsid);
    if(ptoc_res != S_OK)
        throw runtime_error("CLSID error: " +  GetLastErrorAsString());
    cout << "Printing out class ID" << endl;
    cout << "Class ID: " << *((int*)&clsid) << endl;

    LPOLESTR progId;
    const auto progIdResult = ProgIDFromCLSID(
        CLSID_RTD, &progId);
    if(progIdResult != S_OK)
        throw runtime_error("Prog ID error: " +  GetLastErrorAsString());

     char buf[40];
     WideCharToMultiByte(CP_ACP, NULL, progId, -1, buf, 40, NULL, NULL);
     cout << "prog id is '" << buf << "'" << endl;

    cout << "Creating instance" << endl;
    RTDServerLib::IRtdServerPtr rtdServer;
    if(rtdServer.CreateInstance(CLSID_RTD) != S_OK)
        throw runtime_error("Could not create instance: " +
                            GetLastErrorAsString());

    cout << "Starting RTD server" << endl;
    const auto startResult = rtdServer->ServerStart(nullptr);
    cout << "Start result was: " << startResult << endl;
}



//Returns the last Win32 error, in string format. Returns an empty string if there is no error.
std::string GetLastErrorAsString() {
    //Get the error message, if any.
    const auto errorMessageID = ::GetLastError();
    if(errorMessageID == 0)
        return {}; //No error message has been recorded

    LPSTR messageBuffer = nullptr;
    size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                                 NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, nullptr);

    std::string message(messageBuffer, size);

    //Free the buffer.
    LocalFree(messageBuffer);

    return message;
}
/**
用于检查COM DLL是否正确运行的小测试程序
登记
*/
#包括
#ifndef RTD_拱
#定义RTD_拱32
#恩迪夫
//
//这里我们在DLL上进行导入,您也可以在.TLB上进行导入
//#import指令在输出文件夹中生成两个文件。
//
#导入“bin\\VCRTDServer.dll”
#包括
#包括
#包括
#包括
#包括“IRTDServer_h.h”
使用名称空间std;
#定义PROGID_T(“VCRTDServer.RTDFunctions”)
#定义CLSID_字符串(“{8D2EEA35-CBEB-49b1-8F3E-68C8F50F38D8}”)
常数CLSID CLSID_RTD={0x8D2EEA35,0xCBEB,0x49B1,
{0x8F,0x3E,0x68,0xC8,0xF5,0x0F,0x38,0xD8};
常量CLSID IID_RTD_UpdateEvent={0xa43788c1、0xd91b、0x11d3,
0x8f、0x39、0x00、0xc0、0x4f、0x36、0x51、0xb8};
常数CLSID IID_RTD={0xec0e6191,0xdb41,0x11d3,
0x8f、0xe3、0x00、0xc0、0x4f、0x36、0x51、0xb8};
静态字符串GetLastErrorAsString();
静态无效运行();
int main(){
试一试{
run();
coninitialize();
返回0;
}捕获(常量异常和ex){

cerr您确定它是用64位版本的regsvr32注册的吗?我以前遇到过一些问题,在编写64位COM服务器时,没有安装在注册表的正确部分尝试在调试器下运行DLL,将项目首选项中的目标可执行文件设置为Excels您确定它是用64位版本的reg注册的吗svr32?我以前遇到过一些问题,在编写64位COM服务器时,没有安装在注册表的正确部分尝试在调试器下运行DLL,将项目首选项中的目标可执行文件设置为Excel