【问题标题】:Are Visual Studio 6 typelibs handled differently in Visual Studio 2008 under Vista and 7?Visual Studio 6 类型库在 Vista 和 7 下的 Visual Studio 2008 中的处理方式是否不同?
【发布时间】:2010-10-26 03:56:53
【问题描述】:

我在 Visual Studio 6 C++ 中编写了一个标准 DLL。我还编写了一个 typelib 来配合它,以便它可以直接在 VB6 中使用,而不是通过 Declare。

它在 Windows XP 下的 VB6 中运行良好。

当我将 DLL 和 TLB 带入 Vista 和 Windows7 时不起作用。 .TLB 可以很好地使用REGTLIB 注册,但在 Visual Studio 2008 中唯一可见的符号是Attribution 常量。

我试图模仿的技术位于How To Make C DLL More Accessible to VB with a Type Library。是不是这种技术不再适用???

(缩写的)ODL 代码复制如下。知道发生了什么吗?

// This is the type library for BOBDE.dll
[
    // Use GUIDGEN.EXE to create the UUID that uniquely identifies
    // this library on the user's system. NOTE: This must be done!!
    uuid(EE090BD0-AB6C-454c-A3D7-44CA46B1289F),
    // This helpstring defines how the library will appear in the
    // References dialog of VB.
    helpstring("BOBDE TypeLib"),
    // Assume standard English locale.  
    lcid(0x0409),
    // Assign a version number to keep track of changes.
    version(1.0)
]
library BOBDE
{
    // Now define the module that will "declare" your C functions.
[helpstring("Functions in BOBDE.DLL"), version(1.0),dllname("BOBDE.dll")]   
    module BOBDEFunctions
    {
[helpstring("Blowfish Encode ASCII for ANSI"), entry("BEA_A")] 
    void __stdcall BEA_A( [in] BSTR p1, [in] BSTR p2, [out,retval] BSTR* res );
    // other very similar functions removed for the sake of brevity
const LPSTR Attribution = "This product includes cryptographic software written by Eric Young (eay@cryptsoft.com)"; 
    } // End of Module
}; // End of Library

【问题讨论】:

    标签: visual-studio-2008 vb6 midl


    【解决方案1】:

    这里的问题是您不仅更改了操作系统,还更改了开发工具。如果您在 Win7 上运行 VB6,它应该仍然可以工作。但 Visual Studio 2008 支持 VB.NET,这是一种与 VB6 非常不同的语言。它只支持 COM 使用的“真正”类型库。

    从 DLL 调用导出的函数需要使用 .NET 中内置的 P/Invoke 编组器。查看 MSDN 库中的 DllImportAttribute 和 VB.NET Declare 语句。该函数的声明应该类似于:

    <DllImport("bobde.dll")> _
    Function BEA_A( _
          <MarshalAs(UnmanagedType.BStr)> ByVal p1 As String, _
          <MarshalAs(UnmanagedType.BStr)> ByVal p2 As String) _
        As <MarshalAs(UnmanagedType.BStr)> String
    End Function
    

    无需为此注册类型库。用 C++/CLI 语言编写托管类包装器将是另一种方法。

    【讨论】:

    • “如果你在 Win7 上运行 VB6,它应该仍然可以工作”在我的情况下可能是真正的问题。一个同事有Win7。我有 Server2008 和一些带有 XP 和 Ubuntu 的 VirtualBox VM。
    • 我真的不清楚您使用的是什么实际工具。我发布的声明仅适用于 VB.NET,不适用于 VB6。
    • +1 顺便说一句,Server2008 和 Win7 支持 VB6 运行时。如果您使用的是 VB.Net,则需要 Hans 发布的 P/Invoke 标头。您可以使用 Microsoft CLR 互操作团队的 P/Invoke Assistant 从 C++ 标头自动创建此 P/Invoke 代码
    【解决方案2】:

    您创建类型库而不只是在 VB6 中声明函数的任何原因?放

    Private Declare Function BEA_A Lib "bobde.dll" _
    (ByVal p1 As String, ByVal p2 As String) As String 
    

    模块顶部看起来要简单得多。

    【讨论】:

    • 简单答案:智能感知。添加了答案:通过 Declare 转到 DLL 的字符串被转换为 ANSI。我希望能够通过 BSTR。 typelib 方法使这成为可能。
    • 这是一个小麻烦,但如果你想要 wchar* 你可以 varptr() 字符串。对我来说,任何我可以避免注册和弄乱注册表和卸载程序的东西都更好。
    • 实际上,StrPtr 比 VarPtr 更适合字符串...嗯,至少在我的 FWIW 经验中。
    猜你喜欢
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    相关资源
    最近更新 更多