VB 调用时装载DLL声明如下:
Public Declare Function CheckData& Lib "chk100.dll" Alias "chk100" _
    (ByVal ProgressbarFunctionAddress As Long, ByRef MyDepth As Single, ByRef rtA1 As Single, ByRef rtA7 As Single, ByRef rtA8 As Single)
VB 调用如下:
CheckData AddressOf ProgressBar1, Depth1(1), rtA2(1), rtA7(1), rtA8(1)
其中:
  Progressbar1为一个函数 其它都是数组
Public Function ProgressBar1(ByRef I As Long) As Long
    
    If I > frmild.ProgressBar1.Max Then
        frmild.ProgressBar1.Value = frmild.ProgressBar1.Max
    Else
        frmild.ProgressBar1.Value = I
    End If
    
    If blnCancelSelected Then
        I = 200
    End If
    DoEvents
    
End Function
动态连接库chk100.dll是用C++写的
请问其中Delphi中如何声明和调用C++编写的DLL中的函数,类型该怎么声明呢?

解决方案 »

  1.   

    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);  private
        { Private declarations }
      public
        { Public declarations }
      end;
       Tprint_jz = Function ProgressBar1(ByRef I As Long)Long;stdcall;procedure TForm1.Button1Click(Sender: TObject);
    var
        OneHandle : THandle; //定义一个句柄变量
        flag : long;
        print_jz: TFarproc;
    begin
        OneHandle := LoadLibrary('chk100.dll'); //动态载入DLL,并返回其句柄
        try
            if OneHandle <> 0 then
            begin
                //如果载入成功则获取ShowCalendar函数的地址
                @print_jz := GetProcAddress(OneHandle, 'ProgressBar1');
                if not (@print_jz = nil) then
                begin
                    //如果找到该函数则调用DLL中的输出函数
                    flag := @print_jz(200);
                    ShowMessage(flag);
                end
                else
                begin
                    RaiseLastWin32Error;
                    ShowMessage('函数没有找到');
                end;
            end
            else
                ShowMessage('dll没有找到');
        finally
            FreeLibrary(OneHandle); //调用完毕收回DLL占用的资源
        end;end;
      

  2.   

    以下给你一个例子,你可以借鉴以下,这是从一个已经完全调通的程序在录下来的。 希望对你有帮助。 ----------------------------------------------------------------------------- VC++的定义: // MYProcess.cpp: 一定是 .CPP文件!! #include "stdafx.h" 
    #include "myProcess.h" #ifdef _DEBUG 
    #undef THIS_FILE 
    static char THIS_FILE[]=__FILE__; 
    #define new DEBUG_NEW 
    #endif extern "C" __declspec(dllexport) DWORD CDWORDProcess() 

    return .......; 

    extern "C" __declspec(dllexport) BOOL CBOOLProcess() 

    CASmoothProcess* pASmoothProcess = new CASmoothProcess; 
    return ......; 
    } extern "C" __declspec(dllexport) void CvoidProcess() 

    return; 
    } 以下是Delphi的引入单元: 
    unit MYUnit; {$ALIGN ON} 
    {$MINENUMSIZE 1} interface Uses Windows,SysUtils; Type TCDWORDProcess=Function(...) Dword; Cdecl; //注意与VC++中对应函数的参数说明一致 
    TCBOOLProcess=Function(...) Bool;Cdecl;         //注意参数次序、类型对应 
    TCvoidProcess=Procedure(...);Cdecl;             //对应“过程” implementation Var CDWORDProcess: TCDWORDProcess; 
    CBOOLProcess: TCBOOLProcess; 
    CvoidProcess: TCvoidProcess; Var DLL_Handle: THandle; Function LoadDLL: Bool; //动态连接库加载 
    Var Loadok: Bool; //动态连接库加载路径 
    SysDLLPath: String; 
    Begin 
    Loadok:=True; 
    LoadDLLsOk:=False; 
    SysDLLPath:=.... //DLL所在的目录; 
    DLL_Handle:=LoadLibrary(PChar(SysDLLPath)); //安给定的路径加载 
    If DLL_Handle>0 Then //若加载成功, 
    Begin //则依次定位三个函数 
    Try 
    LoadDLLsOk:=True; 
    @CDWORDProcess:=GetProcAddress(DLL_Handle,'CDWORDProcess');//致意注意大小写! 
    @CBOOLProcess:=GetProcAddress(DLL_Handle,'CBOOLProcess'); 
    @CvoidProcess:=GetProcAddress(DLL_Handle,'CvoidProcess'); 
    Except 
    FreeLibrary(DLL_Handle); 
    Loadok:=False; 
    End 
    End 
    Else 
    Loadok:=False; 
    Result:=Loadok; 
    End; initialization //初始化处理 
    LoadDLLs; 
    finalization 
    If DLL_Handle>0 Then //退出处理 
    FreeLibrary(DLL_Handle); end. 回答来自:
    http://expert.csdn.net/Expert/topic/2295/2295952.xml?temp=.8363764
      

  3.   


    推荐你看下列个贴:http://expert.csdn.net/Expert/topic/2293/2293020.xml?temp=.173443http://expert.csdn.net/Expert/topic/2325/2325715.xml?temp=.5663568http://expert.csdn.net/Expert/topic/2237/2237804.xml?temp=.393902http://expert.csdn.net/Expert/topic/2373/2373586.xml?temp=.6391718
      

  4.   

    其实DLL的调用方式是标准的,无所谓C的或D的。
    可能需要注意的一点就是C是区别大小写而D是不区分的。