您好,我尝试将dll加载到内存中并从资源播放声音文件(Delphi2009)。在此示例中,我将DLL从HDD加载到内存(我计划将DLL从资源加载到内存),但在Button1Click之后出现错误

$ 76E2C41F的首次机会异常(exception)。异常类EAccessViolation,消息为“地址00000000上的访问冲突。读取地址00000000”。处理DemoApp.exe(3020)

声音根本不播放:/

我从这里使用的一些代码:http://www.cyberforum.ru/blogs/14360/blog1682.html#a_codemodez
但由于自定义单位strUtilz,MemModuleUnicode,我无法对其进行编译

BTMemoryModule v0.0.41包括BTMemoryModule和示例

http://code.google.com/p/memorymodule/downloads/list

BTMemoryModule v.1(可能很旧)(带有BTMemoryModule + BTMemoryModuleUnicode)

http://www.delphibasics.info/home/delphibasicssnippets/btmemorymodule

unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, BTMemoryModule, StdCtrls, xpman;

 const // Constants :::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::
 _s = '';
 _n=#13#10; // line break
 ver = '1.0 '; // Articles
 tit = 'Bass Memory App' + ver; // title - the name of the application
 msgYN=$04; msgERR=$10; msgINF=$40; // <-type codes posts
 res1='dll'; // resource name with dllkoy
 res2='snd'; // name of the resource with sound

 type // TYPES :::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::
 MemRes = record // structure for the projection of the resource in memory
 p: pointer; // pointer to the memory
 sz: int64; // size (length)
 rd: cardinal; // hResData
 ri: cardinal; // hResInfo
 end;

type
  TBASS_ChannelPlay = function (handle: cardinal; restart: bool): bool; stdcall;
  TBASS_StreamCreateFile = function (mem: bool; f: Pointer; offset, length: int64; flags: cardinal): cardinal; stdcall;
  TBASS_StreamFree = function (handle: cardinal): bool; stdcall;
  TBASS_Init = function (device: integer; freq, flags: cardinal; win: cardinal; clsid: pGUID): bool; stdcall;
  TBASS_Free = function: bool; stdcall;

  TForm1 = class(TForm)
    BtnFileCAll: TButton;
    BtnMemCall: TButton;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    mp_DllData : Pointer;
    m_DllDataSize : Integer;
    mp_MemoryModule: PBTMemoryModule;
    //m_DllHandle: Cardinal;

    m_BASS_ChannelPlay: TBASS_ChannelPlay;
    m_BASS_StreamCreateFile: TBASS_StreamCreateFile;
    //m_BASS_StreamFree: TBASS_StreamFree;
    m_BASS_Init: TBASS_Init;
    //m_BASS_Free: TBASS_Free;
  public
    { Public declarations }


  end;

var
 Form1: TForm1;
 wnd: cardinal; // window handle
 ss: cardinal; // handle audio stream
 snd: MemRes; // pointer to the audio file in memory
 dll: MemRes; // pointer to memory dllku
 bass: Pointer; // structure projection dll in memory
 stp: word; // execution step (for debug)
 st: boolean; // status of the audio stream
 th: cardinal; // handle the flow of replacement buttons
 ti: cardinal; // id flow
 ms : TMemoryStream;
 rs : TResourceStream;

implementation


{$R *.dfm}
{$R BassMem.RES}  // snd CookieJarLoop.ogg RCData

function Res2Mem(hInst:cardinal;res:string;rtype:pChar):MemRes;
begin
  result.p:=nil;
  result.ri:=FindResource(hInst,pchar(res),rtype);
  if result.ri=0 then exit;
  result.sz:=SizeOfResource(hInst,result.ri);
  if result.sz=0 then exit;
  result.rd:=LoadResource(hInst,result.ri);
  if result.rd=0 then exit;
  result.p:=LockResource(result.rd);
end;


procedure TForm1.FormCreate(Sender: TObject);
var
  MemoryStream: TMemoryStream;
begin
  Position := poScreenCenter;
  MemoryStream := TMemoryStream.Create;
  MemoryStream.LoadFromFile('bass.dll');
  MemoryStream.Position := 0;
  m_DllDataSize := MemoryStream.Size;
  mp_DllData := GetMemory(m_DllDataSize);
  MemoryStream.Read(mp_DllData^, m_DllDataSize);
  MemoryStream.Free;

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FreeMemory(mp_DllData);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  mp_MemoryModule := BTMemoryLoadLibary(mp_DllData, m_DllDataSize);
    try
    if mp_MemoryModule = nil then showmessage('err1');

    @m_BASS_Init := BTMemoryGetProcAddress(mp_MemoryModule, 'BASS_Init');
    if @m_BASS_Init = nil then showmessage('err2');

    @m_BASS_ChannelPlay := BTMemoryGetProcAddress(mp_MemoryModule, 'BASS_ChannelPlay');
    if @m_BASS_ChannelPlay = nil then showmessage('err3');

    m_BASS_Init(-1, 44100, 0, Handle, nil);

    snd:=Res2Mem(hInstance, res2 ,RT_RCDATA);

    ss:=m_BASS_StreamCreateFile(true,snd.p,0,snd.sz,4{=BASS_SAMPLE_LOOP});
    if ss=0 then showmessage('err ss=0');
    m_BASS_ChannelPlay(ss, false);

    except
    Showmessage('An error occoured while loading the dll: ' + BTMemoryGetLastError);
    end;
  if mp_MemoryModule <> nil then BTMemoryFreeLibrary(mp_MemoryModule);
end;

end.

最佳答案

您不初始化m_BASS_StreamCreateFile。因此,当您调用它时,其值为nil。解释错误信息。

您需要添加对BTMemoryGetProcAddress的调用以初始化m_BASS_StreamCreateFile

@m_BASS_StreamCreateFile := BTMemoryGetProcAddress(mp_MemoryModule,
  'BASS_StreamCreateFile');
if @m_BASS_StreamCreateFile = nil then ....

如果您在调试器下运行代码,这将很容易发现。调试器将捕获该异常,并且调用堆栈将导致对m_BASS_StreamCreateFile的调用。然后,您可以检查它的值以发现它是nil

关于delphi - Delphi:通过BTMemoryModule直接从内存中加载bass.dll并播放资源中的声音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21989981/

10-09 16:57