【问题标题】:How to call a Delphi DLL from VB6如何从 VB6 调用 Delphi DLL
【发布时间】:2008-11-17 14:01:37
【问题描述】:

鉴于以下 Delphil DLL 声明

function csd_HandleData(aBuf: PChar; aLen: integer): integer; stdcall;

使用它的 VB6 声明是什么?

我尝试了各种声明,例如

Declare Function csd_HandleData Lib "chsdet.dll" (ByVal aBuf As String, ByVal aLen As Integer)
Declare Function csd_HandleData Lib "chsdet.dll" (aBuf As Long, ByVal aLen As Integer)
Declare Function csd_HandleData Lib "chsdet.dll" (aBuf As Byte, ByVal aLen As Integer)

使用适合参数的相关代码,但似乎没有任何效果,即 Delphi 调试器说我在 aLen 中有一个太大的值,在 aBuf 中有一个空字符串。

我正在努力使用 TypeLib 来驱动连接,但正在使用 Declares 进行原型设计。

【问题讨论】:

  • 这里是 Delphi 函数的等效 C 原型: int csd_HandleData(char* aBuf, int aLen);没有什么惊天动地的,但也许它会得到一个因为不熟悉德尔福而害怕贡献的人的答案。

标签: delphi dll vb6 declare


【解决方案1】:

试试

Declare Function csd_HandleData Lib "chsdet.dll" (ByVal aBuf As String, 
ByVal aLen As Integer) As Integer

您似乎忘记了返回值。

【讨论】:

  • 是的,我有。哦,尴尬(脸红)。
  • 这似乎有效。这确实意味着我放入字符串中的任何 Unicode 在进入 DLL 时都会被 ANSI 化,但只有 Typelib 会阻止这种情况,但无论如何,Typelib 是目标。感谢史蒂夫和 mj2008。
  • 在制作原型时,您可以声明宽字符串“As Long”,然后使用 StrPtr 传递您的字符串。
【解决方案2】:

VB整数数据类型是16bit,所以你应该声明它为long,相当于Delphi和其他语言中的整数。

Declare Function csd_HandleData Lib "chsdet.dll" (ByVal aBuf As String, ByVal aLen As long) as long

【讨论】:

    【解决方案3】:

    对于那些感兴趣的人,这里是CHSDET 的类型库的最终 IDL。让我印象深刻的(在重新发现 Matt Curland 的 EditTLB 工具之后)是我可以将结构放入类型库中,并且 VB 处理它们就像我在源代码中声明它们一样。

    我已经写信给 ChsDet 的作者,也许这最终会成为标准发行版的一部分。

    // Generated .IDL file (by the OLE/COM Object Viewer)
    // 
    // typelib filename: chsdet.tlb
    
    [
      uuid(316A83D7-8BF4-490E-BDDE-75EBC332C355),
      version(1.0),
      helpstring("Charset Detector - as the name says - is a stand alone executable module for automatic charset detection of a given text.\r\n\t\r\nIt can be useful for internationalisation support in multilingual applications such as web-script editors or Unicode editors.\r\n\t\r\nGiven input buffer will be analysed to guess used encoding. The result can be used as control parameter for charset conversation procedure.\r\n\t\r\nCharset Detector can be compiled (and hopefully used) for MS Windows (as dll - dynamic link library) or Linux.\r\n\t\r\nBased on Mozilla's i18n component - http://www.mozilla.org/projects/intl/. \r\n\r\nCharset Detector is open source project and distributed under Lesser GPL.\r\nSee the GNU Lesser General Public License for more details - http://www.opensource.org/licenses/lgpl-license.php\r\n\r\nNikolaj Yakowlew \xFFFFFFA9 2006-2008 \r\nTypeLib by Bruce M. Axtens, 2008.")
    ]
    library CHSDET
    {
        // TLib :     // Forward declare all types defined in this typelib
    
        [
          dllname("CHSDET.dll"),
          version(1.0),
          helpstring("Functions in CHSDET.DLL")
        ]
        module CHSDETFunctions {
        [entry(0x60000000), helpstring("Returns rAbout record (qv)")]
        void _stdcall GetAbout([in, out] rAbout* AboutRec);
        [entry(0x60000001), helpstring("Reset detector. Prepares for new analysis.")]
        void _stdcall Reset();
        [entry(0x60000002), helpstring("Analyse given buffer of specified length.
    
    Return value is of eHandleDataErrors, either 
    NS_ERROR_OUT_OF_MEMORY (Unable to create internal objects) or NS_OK.
    
    Function can be called more that one time to continue guessing. Charset Detector remembers last state until Reset called.")]
        void _stdcall HandleData(
                [in] BSTR aBuf, 
                [in] short aLen, 
                [out, retval] short* retVal);
        [entry(0x60000003), helpstring("Returns either TRUE (Charset Detector is sure about text encoding.) or FALSE.
    
    NB: If input buffer is smaller then 1K, Charset Detector returns FALSE.")]
        void _stdcall IsDone([out, retval] short* retVal);
        [entry(0x60000004), helpstring("Signal data end. If Charset Detector hasn't sure result (IsDone = FALSE) the best guessed encoding will be set as result.")]
        void _stdcall DataEnd();
        [entry(0x60000005), helpstring("Returns guessed charset as rCharsetInfo record")]
        void _stdcall GetDetectedCharset([out, retval] rCharsetInfo* retVal);
        [entry(0x60000006), helpstring("Returns all supported charsets in form "0x0A Name - CodePage"")]
        void _stdcall GetKnownCharsets(
                [in, out] long* sList, 
                [out, retval] long* retVal);
        [entry(0x60000007), helpstring("Return eBOMKind value matching byte order mark (if any) of input data.")]
        void _stdcall GetDetectedBOM([out, retval] eBOMKind* retVal);
        [entry(0x60000008), helpstring("Remove CodePage from consideration as a possible match")]
        void _stdcall DisableCharsetCP([in] long CodePage);
        };
    
        typedef [uuid(91694067-30AB-44A9-A210-F5602935475F)]
        struct tagrAbout {
    
    long lMajor;
    
    long lMinor;
    
    long lRelease;
    
    long sAbout;
        } rAbout;
    
        typedef [uuid(3C8B7420-D40B-458B-8DE8-9B3D28607396)]
        enum {
        BOM_Not_Found = 0,
        BOM_UCS4_BE = 1,
        BOM_UCS4_LE = 2,
        BOM_UCS4_2143 = 3,
        BOM_UCS4_3412 = 4,
        BOM_UTF16_BE = 5,
        BOM_UTF16_LE = 6,
        BOM_UTF8 = 7
        } eBOMKind;
    
        typedef [uuid(9B231DEF-93FB-440D-B06B-D760AECE09D0)]
        struct tagrCharsetInfo {
    
    long Name;
    
    short CodePage;
    
    long Language;
        } rCharsetInfo;
    
        typedef enum {
        NS_OK = 0,
        NS_ERROR_OUT_OF_MEMORY = -2147024882
        } eHandleDataErrors;
    };
    

    【讨论】:

      【解决方案4】:

      我不知道 Delphi 中的 PChar 是什么,它只是一个字符吗? ASCII??统一码?

      在 VB6 中 Integer 是 16 位,您必须将 aLen 声明为 Long,它可以容纳 32 位。

      您还必须声明函数的返回类型,在这种情况下,您也需要返回一个 Long 值。

      这可能会起作用:

      Declare Function csd_HandleData Lib "chsdet.dll" (aBuf As Byte, ByVal aLen As Long) As Long
      

      【讨论】:

      • 不,PChar 是一个指向 Char 的指针。从 2009 年的 Delphi 开始,这种区别非常显着,其中 SizeOf(Char)=2,由于 Unicode。
      【解决方案5】:

      我不知道 Vb 是如何工作的,但 PChar 是一个指针,所以尝试获取引用而不是值。

      Declare Function csd_HandleData Lib "chsdet.dll" (**ByReference <--guessing here :D** aBuf As String, ByVal aLen As Integer)
      

      【讨论】:

        猜你喜欢
        • 2011-12-19
        • 1970-01-01
        • 2012-03-04
        • 2023-03-17
        • 2010-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-17
        相关资源
        最近更新 更多