10

我们有一个 .NET 程序集(实际上是 Aspose.Words),我们希望客户端可以轻松地从 COM 客户端使用它。

  1. 因此,我们将 .TLB 与程序集一起提供,这样客户就可以从 C++ 或 Delphi 等语言中使用它,而不必费心自己提取 .TLB。

  2. 我们还随程序集提供了一个 .IDL,以便客户可以查看它,如果他们想查看枚举值(例如,如果他们在 ASP 中编程)。

  3. 我希望 .TLB 和 .IDL 由构建脚本生成。我可以生成.TLB 没有问题。但我无法在命令行中生成 .IDL(或将 .TLB 转换为 .IDL 的替代方法)。我使用 OLEVIEW.EXE 手动执行此操作,这并不好。

听说 Delphi 的 TLIBIMP.EXE 听起来可以做到这一点,但它似乎并没有单独提供。

所以问题是:

  1. 上面的任何内容听起来很愚蠢?

  2. 如何使用命令行从 .NET 程序集中获取 .IDL。

谢谢。

4

3 回答 3

5

我已经编写了这个快速而肮脏的 hack,用于从 TLB 中提取 IDL。它使用与 OLEVIEW.EXE 相同的(据我所知)COM 对象。

/*
   Extract the IDL from a tlb file and print it to stdout.

   The tlb file is analysed by a COM object of type CLSID_ITypeLibViewer and 
   the extracted IDL is shown in a window created by the same object.

   This program extracts the IDL from that window.

   Usually Microsoft's OLEViewer (a.k.a. OLE/COM Object Viewer) registers a 
   COM object of that CLSID_ITypeLibViewer (for example inside a dll called 
   IViewers.Dll), I've tested it with:
   IVIEWERS.DLL "OLE/COM Object Interface Viewer" Version 6.0.6000.16384

   This software is provided "as-is," without any express or implied warranty.
*/

#define _CRT_SECURE_NO_WARNINGS

#include <Windows.h>
#include <atlbase.h>
#include <comdef.h>

#include <iostream>
#include <string>
#include <ctime>

const char IDL_window_name[] = "ITypeLib Viewer";

const IID   IID_IInterfaceViewer = { 0xfc37e5ba, 0x4a8e, 0x11ce, { 0x87, 0x0b, 
                                     0x08, 0x00, 0x36, 0x8d, 0x23, 0x02 } };

const CLSID CLSID_ITypeLibViewer = { 0x57efbf49, 0x4a8b, 0x11ce, { 0x87, 0x0b, 
                                     0x08, 0x00, 0x36, 0x8d, 0x23, 0x02 } };

#define RICHEDIT_CONTROL_ID 0xE901 // found by Spy++
#define RICHEDIT_PARENT_ID  0xE900 // found by Spy++

#define CHECK(hr) if ( FAILED(hr) ) { std::wcerr << L"COM error: " << stringify_hr(hr) << L"\n"; return __LINE__; }

std::string myself;
std::string tlb;

// My installation of Microsoft Visual Studio 2010 has a sample folder where 
// I can find the source of OleView:
// C:\Program Files (x86)\Microsoft Visual Studio 10.0\Samples\1033\VC2010Samples.zip
//
// Unzipping the file gives me the solution for Visual Studio 2010
// C++\MFC\ole\oleview\oleview.sln
// 
// The following declaration comes from that sample.
// The sample has also the source for the CLSID_ITypeLibViewer object but 
// unfortunately the method View is not implemented (the code in my sample says
// "Implement interface viewer UI here.").
DECLARE_INTERFACE_(IInterfaceViewer, IUnknown)
{
    STDMETHOD(QueryInterface) (THIS_ REFIID riid, LPVOID FAR* ppvObj) PURE;
    STDMETHOD_(ULONG,AddRef) (THIS) PURE;
    STDMETHOD_(ULONG,Release) (THIS) PURE;

    STDMETHOD(View) (HWND hwndParent, REFIID riid, LPUNKNOWN punk) PURE;
};

std::string replace_all( const char* txt, const std::string& old_s, const std::string& new_s );
std::string adapt( const char* txt );
std::string now();
std::wstring stringify_hr( HRESULT hr );

DWORD WINAPI scrape(LPVOID)
{
   HWND hwnd = 0;   
   int guard = 100;

   while( guard-- > 0 && (hwnd = ::FindWindowA(0,IDL_window_name)) == 0 ) {
      ::Sleep(100);
   }

   if ( guard <= 0 ) {
      std::cerr << "No window found with name '" << IDL_window_name << "'\n";
      return __LINE__;
   }

   guard = 100;
   const size_t sz = 100*1024;
   char text[sz];
   do {
      HWND parent_hwnd = ::GetDlgItem(hwnd,RICHEDIT_PARENT_ID);
      if ( parent_hwnd == 0 ) {
         ::SendMessage(hwnd,WM_CLOSE,0,0);
         std::cerr << "No parent with ID " << std::hex << "0x" 
                   << RICHEDIT_PARENT_ID << " found in window with name '" 
                   << IDL_window_name << "'\n";
         return __LINE__;
      }
      HWND richedit_hwnd = ::GetDlgItem(parent_hwnd,RICHEDIT_CONTROL_ID);
      if ( richedit_hwnd == 0 ) {
         ::SendMessage(hwnd,WM_CLOSE,0,0);
         std::cerr << "No richedit with ID " << std::hex << "0x" 
                   << RICHEDIT_CONTROL_ID << " found in window with name '" 
                   << IDL_window_name << "'\n";
         return __LINE__;
      }
      ::GetWindowTextA(richedit_hwnd,text,sz);
      ::Sleep(100);
   } while ( guard-- > 0 && strlen(text) == 0 );

   if ( guard <= 0 ) {
      ::SendMessage(hwnd,WM_CLOSE,0,0);
      std::cerr << "No IDL found in window with name '" 
                << IDL_window_name << "'\n";
      return __LINE__;
   }

   std::cout << "// IDL extracted from " << tlb << " by tlb2idl (" 
             << myself << ") on " << now() << "\n" << adapt(text);

   ::SendMessage(hwnd,WM_CLOSE,0,0);
   return 0;
}

int main(int argc, char* argv[])
{
   myself = argv[0];
   if ( argc <= 1 ) {
      std::cerr << "\nPrint to stdout the IDL extracted from file.tlb\n\nUsage:\n" 
                << myself << " file.tlb\n";
      return __LINE__;
   }

   DWORD exit_code = 0;
   CHECK( ::CoInitialize(0) );
   {
   CComPtr<ITypeLib> ptl;
   tlb = argv[1];
   CHECK( ::LoadTypeLib(std::wstring(tlb.begin(),tlb.end()).c_str(), &ptl) );

   CComPtr<IInterfaceViewer> piv;
   CHECK( ::CoCreateInstance( CLSID_ITypeLibViewer, NULL, CLSCTX_SERVER, 
                              IID_IInterfaceViewer, (void**)&piv ) );

   HANDLE th = ::CreateThread( 0, 0, scrape, 0, 0, 0 );
   CHECK( piv->View(0,IID_ITypeLib,ptl) );
   ::WaitForSingleObject(th,1000);
   ::GetExitCodeThread(th,&exit_code);
   }
   ::CoUninitialize();
   return exit_code;
}

std::string replace_all( const char* txt, const std::string& old_s, const std::string& new_s )
{
   std::string str(txt);
   size_t start_pos = 0;
   while((start_pos = str.find(old_s, start_pos)) != std::string::npos) {
      str.replace(start_pos, old_s.length(), new_s);
      start_pos += new_s.length(); 
   }
   return str;
}

std::string adapt( const char* txt )
{
   std::string str( replace_all(txt,"\r\n","\n") );
   return replace_all(str.c_str(),"\r","\n");
}

std::string now()
{
   time_t szClock;
   time( &szClock );
   struct tm *newTime = localtime( &szClock );
   return asctime( newTime ); 
}

std::wstring stringify_hr( HRESULT hr )
{
   return _com_error(hr).ErrorMessage();
}
于 2012-11-22T14:14:21.733 回答
2

没关系,只修复nowvs 2012 的功能:

std::string now()
{
   char buffer[32];
   errno_t errNum;
   _time32( &aclock );   
   _localtime32_s( &newtime, &aclock );   

   errNum = asctime_s(buffer, 32, &newtime);

   return buffer; 
}

必须确保您没有打开 OLEVIEW。

于 2012-11-30T10:56:56.723 回答
0

类型库足以让任何语言使用您的服务器。IDL 仅对生成您已经提供的类型库或生成 C++ 头文件(使用#import 指令更容易生成)有用。

如果您真的想这样做,这可能是一种方法,构建一个使用#import 的小型 C++ 程序,提供生成的 .tlh 文件。

于 2010-05-13T12:16:55.947 回答