วันจันทร์ที่ 30 มกราคม พ.ศ. 2555

If a demo DLL is stored as a resource using the RC file

DemoDLL RCDATA DemoDLL.dll

to load it from the resource, the next code can be used:

    var
      ms : TMemoryStream;
      rs : TResourceStream;
    begin
      if 0 <> FindResource(hInstance, 'DemoDLL', RT_RCDATA) then
      begin
        rs := TResourceStream.Create(hInstance, 'DemoDLL', RT_RCDATA);
        ms := TMemoryStream.Create;
        try
          ms.LoadFromStream(rs);

          ms.Position := 0;
          m_DllDataSize := ms.Size;
          mp_DllData := GetMemory(m_DllDataSize);

          ms.Read(mp_DllData^, m_DllDataSize);
        finally
          ms.Free;
          rs.Free;
        end;
      end;
    end;

Next, when you have the DLL loaded from a resource into memory, you can call its procedures:

    var
      btMM: PBTMemoryModule;
    begin
      btMM := BTMemoryLoadLibary(mp_DllData, m_DllDataSize);
      try
        if btMM = nil then Abort;
        @m_TestCallstd := BTMemoryGetProcAddress(btMM, 'TestCallstd');
        if @m_TestCallstd = nil then Abort;
        m_TestCallstd('This is a Dll Memory call!');
      except
        Showmessage('An error occoured while loading the dll: ' + BTMemoryGetLastError);
      end;
      if Assigned(btMM) then BTMemoryFreeLibrary(btMM);
    end;

ไม่มีความคิดเห็น:

แสดงความคิดเห็น