我想用Delphi写一个DLL,这个DLL里主要是想用来封装对数据库的操作(比如有:创建表,插入表记录等),然后再用Delphi写一个应用程序,通过传入表名来创建表或者插入数据等。以上是我想要的操作,可我没有思路,希望大家能给出完整的代码让我看看,谢谢!

解决方案 »

  1.   

    这个不是很难!!!你先创建一个DLL工程,然后在工程中增加一个新的单元,将你要实现的功能写在这个单元中,最后在工程文件中导出你写的函数。基本的模式就是这样的了,如果有问题可以看看帮助!!!
      

  2.   

    但我不知道DLL里的函数该怎么写呀?希望得到帮助
      

  3.   

    写法与一般的函数写法一样!!!没有任何的区别,只是在声明的地方,在对后加上StdCall关键字!!!例如:
    Function X( A : Integer ) : Boolean; Stdcall;
      

  4.   

    我就是不知道函数里面的写法。比如说:我是不是应该把SQL语句放在里面?如要写在里面,用什么去执行呀?
      

  5.   

    library KMedia;{ Important note about DLL memory management: ShareMem must be the
      first unit in your library's USES clause AND your project's (select
      Project-View Source) USES clause if your DLL exports any procedures or
      functions that pass strings as parameters or function results. This
      applies to all strings passed to and from your DLL--even those that
      are nested in records and classes. ShareMem is the interface unit to
      the BORLNDMM.DLL shared memory manager, which must be deployed along
      with your DLL. To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. }uses
      SysUtils,K,Classes,MmSystem,Windows,ActiveX;{$R *.res}
    Var
     DSound      : TDirectSound;
     AudioStatic : Array[0..99] of TAudioStatic;
     AudioStream : Array[0..99] of TAudioStream;
     SoundStream : Array[0..99] of TSoundStream;
     Video       : TVideo;
     AudioCD     : TAudioCD;
     I : Integer;function KM_Initialize(DeviceId : Integer; Hwnd : Integer; SpeakerConfig: Dword) : Boolean; stdcall;
    begin
      Result := DSound.Initialize(DeviceID,Hwnd,SpeakerConfig);
    end;function KM_Free : Boolean; stdcall;
    begin
      Result := DSound.Free;
    end;function KM_SetupOutput(SamplesPerSec : Integer; BitsPerSample : Integer; Stereo : Boolean) : Boolean; stdcall;
    begin
      Result := DSound.SetupOutput(SamplesPerSec,BitsPerSample,Stereo);
    end;function KM_GetDeviceList : TStrings; stdcall;
    begin
      Result := DSound.GetDeviceList;
    end;
    function KM_AStatic_CreateFromFile(ID : Integer; FileName : Pchar; UseDSP_3D : Boolean; DSP_Volume : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStatic[ID].CreateFromFile(Dsound.GetDSound,FileName,UseDSP_3D,DSP_Volume);
       end;
    end;function KM_AStatic_Free(ID : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStatic[ID].Free;
       end;
    end;function KM_AStatic_Play(ID : Integer; Loop : Boolean) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStatic[ID].Play(Loop);
       end;
    end;function KM_AStatic_Stop(ID : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStatic[ID].Stop;
       end;
    end;function KM_AStatic_IsPlaying(ID : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStatic[ID].IsPlaying;
       end;
    end;function KM_AStatic_IsStop(ID : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStatic[ID].IsStop;
       end;
    end;function KM_AStatic_IsLooping(ID : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStatic[ID].IsLooping;
       end;
    end;function KM_AStatic_SetVolume(ID : Integer; Volume: Integer): Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStatic[ID].SetVolume(Volume);
       end;
    end;function KM_AStatic_GetVolume(ID : Integer) : Integer; stdcall;
    begin
      Result := -1;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStatic[ID].GetVolume;
       end;
    end;function KM_AStatic_DefaultFrequency(ID : Integer) : Integer; stdcall;
    begin
      Result := -1;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStatic[ID].DefaultFrequency;
       end;
    end;function KM_AStatic_SetFrequency(ID : Integer; Frequency : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStatic[ID].SetFrequency(Frequency);
       end;
    end;function KM_AStatic_GetFrequency(ID : Integer) : Integer; stdcall;
    begin
      Result := -1;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStatic[ID].GetFrequency;
       end;
    end;function KM_AStatic_Set3DPosition(ID : Integer; X,Y,Z : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStatic[ID].Set3DPosition(X,Y,Z);
       end;
    end;function KM_AStatic_Get3DPosition(ID : Integer) : TD3DVector; stdcall;
    Var
     _Value : TD3DVector;
    begin
      _Value.X := 0;
      _Value.Y := 0;
      _Value.Z := 0;
      Result := _Value;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStatic[ID].Get3DPosition;
       end;
    end;{ KM_AStream_ }function KM_AStream_CreateFromFile(ID : Integer; FileName : Pchar; BufferLength : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStream[ID].CreateFromFile(Dsound.GetDSound,FileName,BufferLength);
       end;
    end;function KM_AStream_Free(ID : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStream[ID].Free;
       end;
    end;function KM_AStream_Play(ID : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStream[ID].Play;
       end;
    end;function KM_AStream_Stop(ID : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStream[ID].Stop;
       end;
    end;function KM_AStream_EnableLoop(ID : Integer; Loop: Boolean) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStream[ID].EnableLoop(Loop);
       end;
    end;function KM_AStream_EnableDSP_3D(ID : Integer; DSP_3D : Boolean; Volume : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStream[ID].EnableDSP_3D(DSP_3D,Volume);
       end;
    end;function KM_AStream_DefaultFrequency(ID : Integer) : Integer; stdcall;
    begin
      Result := -1;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStream[ID].DefaultFrequency;
       end;
    end;
      

  6.   

    function KM_AStream_SetFrequency(ID : Integer; Frequency : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStream[ID].SetFrequency(Frequency);
       end;
    end;function KM_AStream_GetFrequency(ID : Integer) : Integer; stdcall;
    begin
      Result := -1;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStream[ID].GetFrequency;
       end;
    end;function KM_AStream_SetVolume(ID : Integer; Volume : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStream[ID].SetVolume(Volume);
       end;
    end;function KM_AStream_GetVolume(ID : Integer) : Integer; stdcall;
    begin
      Result := -1;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStream[ID].GetVolume;
       end;
    end;function KM_AStream_Set3DPosition(ID : Integer; X,Y,Z : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStream[ID].Set3DPosition(X,Y,Z);
       end;
    end;function KM_AStream_Get3DPosition(ID : Integer) : TD3DVector; stdcall;
    Var
     _Value : TD3DVector;
    begin
      _Value.X := 0;
      _Value.Y := 0;
      _Value.Z := 0;
      Result := _Value;
      if (ID >=0) and (ID <99) then
       begin
        Result := AudioStream[ID].Get3DPosition;
       end;
    end;function KM_AStream_IsPlaying(ID : Integer) : Boolean; stdcall;
    begin
      Result := AudioStream[ID].IsPlaying ;
    end;function KM_AStream_IsStop(ID : Integer) : Boolean; stdcall;
    begin
      Result := AudioStream[ID].IsStop ;
    end; {Video}function KM_Video_CreateFromFile(Hwnd : Integer; FileName : Pchar) : Boolean; stdcall;
    begin
      Result := Video.CreateFromFile(Hwnd,FileName);
    end;function KM_Video_Free : Boolean; stdcall;
    begin
      Result := Video.Free;
    end;function KM_Video_IsVideoFile(FileName : Pchar) : Boolean; stdcall;
    begin
      Result := Video.IsVideoFile(FileName);
    end;function KM_Video_Play : Boolean; stdcall;
    begin
      Result := Video.Play;
    end;function KM_Video_Pause : Boolean; stdcall;
    begin
      Result := Video.Pause;
    end;function KM_Video_Stop : Boolean; stdcall;
    begin
      Result := Video.Stop;
    end;function KM_Video_IsPlaying : Boolean; stdcall;
    begin
      Result := Video.IsPlaying;
    end;function KM_Video_IsPause : Boolean; stdcall;
    begin
      Result := Video.IsPause;
    end;function KM_Video_IsStop : Boolean; stdcall;
    begin
      Result := Video.IsStop;
    end;function KM_Video_SetVolume(Volume : Integer) : Boolean; stdcall;
    begin
      Result := Video.SetVolume(Volume);
    end;function KM_Video_GetVolume : Integer; stdcall;
    begin
      Result := Video.GetVolume;
    end;function KM_Video_SetBalance(Balance : Integer) : Boolean; stdcall;
    begin
      Result := Video.SetBalance(Balance);
    end;function KM_Video_GetBalance : Integer; stdcall;
    begin
      Result := Video.GetBalance;
    end;function KM_Video_SetPositions(Positions : Int64) : Boolean; stdcall;
    begin
      Result := Video.SetPositions(Positions);
    end;function KM_Video_GetPositions : Int64; stdcall;
    begin
      Result := Video.GetPositions;
    end;
      

  7.   

    function KM_Video_SetRate(Rate : Double) : Boolean; stdcall;
    begin
      Result := Video.SetRate(Rate);
    end;function KM_Video_GetRate : Double; stdcall;
    begin
      Result := Video.GetRate;
    end;function KM_Video_GetDuration: Int64; stdcall;
    begin
      Result := Video.GetDuration;
    end;function KM_Video_SetWindowPosition(Left : Integer; Top : Integer; Width : Integer; Height : Integer) : Boolean; stdcall;
    begin
      Result := Video.SetWindowPosition(Left,Top,Width,Height);
    end;function KM_Video_EnableFullScreen(FullScreen : Boolean) : Boolean; stdcall;
    begin
      Result := Video.EnableFullScreen(FullScreen);
    end;function KM_Video_IsFullScreen : Boolean; stdcall;
    begin
      Result := Video.IsFullScreen;
    end;function KM_Video_HideCursor(Hide : Boolean) : Boolean; stdcall;
    begin
      Result := Video.HideCursor(Hide);
    end;function KM_Video_IsPlayComplete : Boolean; stdcall;
    begin
      Result := Video.IsPlayComplete;
    end; {AudioCD}function KM_AudioCD_Initialize : Boolean; stdcall;
    begin
      Result := AudioCD.Initialize;
    end;function KM_AudioCD_Free : Boolean; stdcall;
    begin
      Result := AudioCD.Free;
    end;function KM_AudioCD_Play(TrackID : Integer) : Boolean; stdcall;
    begin
      Result := AudioCD.Play(TrackID);
    end;function KM_AudioCD_Stop : Boolean; stdcall;
    begin
      Result := AudioCD.Stop;
    end;function KM_AudioCD_Pause : Boolean; stdcall;
    begin
      Result := AudioCD.Pause;
    end;function KM_AudioCD_Resume : Boolean; stdcall;
    begin
      Result := AudioCD.Resume;
    end;function KM_AudioCD_GetAudioDiskInfo : Integer; stdcall;
    begin
      Result := AudioCD.GetAudioDiskInfo;
    end;function KM_AudioCD_CloseDoor : Boolean; stdcall;
    begin
      Result := AudioCD.CloseDoor;
    end;function KM_AudioCD_EjectDoor : Boolean; stdcall;
    begin
      Result := AudioCD.EjectDoor;
    end;
     {SoundStream}function KM_SSound_CreateFromFile(ID : Integer; FileName : Pchar) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].CreateFromFile(FileName);
       end;
    end;function KM_SSound_Free(ID : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].Free;
       end;
    end;function KM_SSound_Play(ID : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].Play;
       end;
    end;function KM_SSound_Stop(ID : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].Stop;
       end;
    end;function KM_SSound_Pause(ID : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].Pause;
       end;
    end;function KM_SSound_SetBalance(ID : Integer; Balance : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].SetBalance(Balance);
       end;
    end;function KM_SSound_SetPositions(ID : Integer; Positions : Int64) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].SetPositions(Positions);
       end;
    end;function KM_SSound_SetRate(ID : Integer; Rate : Double) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].SetRate(Rate);
       end;
    end;function KM_SSound_SetVolume(ID : Integer; Volume : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].SetVolume(Volume);
       end;
    end;function KM_SSound_GetBalance(ID : Integer) : Integer; stdcall;
    begin
      Result := 0;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].GetBalance;
       end;
    end;function KM_SSound_GetDuration(ID : Integer) : Int64; stdcall;
    begin
      Result := -1;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].GetDuration ;
       end;
    end;function KM_SSound_GetPositions(ID : Integer) : Int64; stdcall;
    begin
      Result := -1;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].GetPositions;
       end;
    end;function KM_SSound_GetRate(ID : Integer) : Double; stdcall;
    begin
      Result := -1;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].GetRate;
       end;
    end;function KM_SSound_GetVolume(ID : Integer) : Integer; stdcall;
    begin
      Result := -1;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].GetVolume;
       end;
    end;function KM_SSound_IsPlaying(ID : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].IsPlaying;
       end;
    end;function KM_SSound_IsPause(ID : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].IsPause;
       end;
    end;function KM_SSound_IsStop(ID : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].IsStop;
       end;
    end;function KM_SSound_IsPlayComplete(ID : Integer) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].IsPlayComplete;
       end;
    end;function KM_SSound_IsValidFile(ID : Integer; FileName : Pchar) : Boolean; stdcall;
    begin
      Result := False;
      if (ID >=0) and (ID <99) then
       begin
         Result := SoundStream[ID].IsValidFile(FileName);
       end;
    end;exports
     KM_Initialize,
     KM_Free,
     KM_SetupOutput,
     KM_GetDeviceList,
     KM_AStatic_CreateFromFile,
     KM_AStatic_Free,
     KM_AStatic_Play,
     KM_AStatic_Stop,
     KM_AStatic_IsPlaying,
     KM_AStatic_IsStop,
     KM_AStatic_IsLooping,
     KM_AStatic_SetVolume,
     KM_AStatic_GetVolume,
     KM_AStatic_DefaultFrequency,
     KM_AStatic_SetFrequency,
     KM_AStatic_GetFrequency,
     KM_AStatic_Set3DPosition,
     KM_AStatic_Get3DPosition,
     KM_AStream_CreateFromFile,
     KM_AStream_Free,
     KM_AStream_Play,
     KM_AStream_Stop,
     KM_AStream_EnableLoop,
     KM_AStream_EnableDSP_3D,
     KM_AStream_DefaultFrequency,
     KM_AStream_SetFrequency,
     KM_AStream_GetFrequency,
     KM_AStream_SetVolume,
     KM_AStream_GetVolume,
     KM_AStream_Set3DPosition,
     KM_AStream_Get3DPosition,
     KM_AStream_IsPlaying,
     KM_AStream_IsStop,
     KM_Video_CreateFromFile,
     KM_Video_Free,
     KM_Video_IsVideoFile,
     KM_Video_Play,
     KM_Video_Pause,
     KM_Video_Stop,
     KM_Video_IsPlaying,
     KM_Video_IsPause,
     KM_Video_IsStop,
     KM_Video_SetVolume,
     KM_Video_GetVolume,
     KM_Video_SetBalance,
     KM_Video_GetBalance,
     KM_Video_SetPositions,
     KM_Video_GetPositions,
     KM_Video_SetRate,
     KM_Video_GetRate,
     KM_Video_GetDuration,
     KM_Video_SetWindowPosition,
     KM_Video_EnableFullScreen,
     KM_Video_IsFullScreen,
     KM_Video_HideCursor,
     KM_Video_IsPlayComplete,
     KM_AudioCD_Initialize,
     KM_AudioCD_Free,
     KM_AudioCD_Play,
     KM_AudioCD_Stop,
     KM_AudioCD_Pause,
     KM_AudioCD_Resume,
     KM_AudioCD_GetAudioDiskInfo,
     KM_AudioCD_CloseDoor,
     KM_AudioCD_EjectDoor,
     KM_SSound_CreateFromFile,
     KM_SSound_Free,
     KM_SSound_Play,
     KM_SSound_Stop,
     KM_SSound_Pause,
     KM_SSound_SetBalance,
     KM_SSound_SetPositions,
     KM_SSound_SetRate,
     KM_SSound_SetVolume,
     KM_SSound_GetBalance,
     KM_SSound_GetDuration,
     KM_SSound_GetPositions,
     KM_SSound_GetRate,
     KM_SSound_GetVolume,
     KM_SSound_IsPlaying,
     KM_SSound_IsPause,
     KM_SSound_IsStop,
     KM_SSound_IsPlayComplete,
     KM_SSound_IsValidFile;begin
     CoInitialize(nil);
     DSound := TDirectSound.Create;
     AudioCD := TAudioCD.Create;
     Video := TVideo.Create;
     for i := 0 to 99 do
      begin
       AudioStatic[i] := TAudioStatic.Create;
       AudioStream[i] := TAudioStream.Create;
       SoundStream[i] := TSoundStream.Create;
      end;
    end.
      

  8.   

    www.delphifans.com
    www.2ccc.com在这两个网站中搜索dll,例子很多的
      

  9.   

    to :yukileo()你在搞什么呀?你写那一大堆并不是我想要的哟。
      

  10.   

    你让我帮你写模块的话..干脆给我RMB好了..
      

  11.   

    不是吧,就一个封装操作,还要RMB吗?晕